iOS Swift 两个 TableView 联动

import UIKit

class JLCascadeMenu: UIView {

    var leftArray: [String] = ["早餐时光", "营养粥", "特色点心", "佐餐小菜", "火锅", "冒菜", "巴奴毛肚", "外婆家", "李想大虾", "海底捞"]
    
    lazy var leftTableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        return tableView
    }()
    
    lazy var rightTableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        return tableView
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        // 默认选中左侧第一个, 不触发代理方法
        leftTableView.selectRow(at: IndexPath(row: 0, section: 0), animated: false, scrollPosition: .none)
        addSubview(leftTableView)
        addSubview(rightTableView)
        leftTableView.snp.makeConstraints { (make) in
            make.left.top.bottom.equalToSuperview()
            make.width.equalTo(JLScreen.width * 0.25)
        }
        rightTableView.snp.makeConstraints { (make) in
            make.right.top.bottom.equalToSuperview()
            make.left.equalTo(leftTableView.snp.right)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension JLCascadeMenu: UITableViewDelegate, UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        if tableView == leftTableView {
            return 1
        }
        return leftArray.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if tableView == leftTableView {
            return leftArray.count
        }
        return 5
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        if tableView == leftTableView {
            cell.selectionStyle = .blue
            cell.textLabel?.text = leftArray[indexPath.row]
        } else {
            cell.selectionStyle = .none
            cell.textLabel?.text = "\(indexPath.row)"
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if tableView == leftTableView {
            rightTableView.scrollToRow(at: IndexPath(row: 0, section: indexPath.row), at: .top, animated: true)
        } else {
            
        }
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if tableView == rightTableView {
            return 30
        }
        return 0
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if tableView == rightTableView {
            let header = UIView()
            header.backgroundColor = .white
            let label = UILabel()
            label.text = leftArray[section]
            header.addSubview(label)
            label.snp.makeConstraints { (make) in
                make.left.equalTo(15)
                make.right.equalTo(-15)
                make.top.bottom.equalToSuperview()
            }
            return header
        }
        return nil
    }
    
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        // 右侧tableView滚动时, 左侧tableView滚动到相应 Row
        if scrollView == rightTableView {
            // 只有用户拖拽才触发, 点击左侧不触发
            if scrollView.isDragging || scrollView.isTracking || scrollView.isDecelerating {
                if let topIndexPath = self.rightTableView.indexPathsForVisibleRows?.first {
                    let moveToIndexPath = IndexPath(row: topIndexPath.section, section: 0)
                    self.leftTableView.selectRow(at: moveToIndexPath, animated: true, scrollPosition: .middle)
                }
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值