【问题标题】:Swift expand/collapse cell by cell button click快速展开/折叠单元格按钮单击
【发布时间】:2018-05-25 18:32:35
【问题描述】:

我有一个带有一个 UILabel 和 UIButton 的自定义单元格。我想要另一个细胞 通过点击父单元格的按钮展开/折叠,如果点击父单元格 UIButton 则子单元格应该展开/折叠,我们还需要父单元格代表和子单元格委托不同的操作。

谢谢。

【问题讨论】:

    标签: ios swift uitableview collapse expand


    【解决方案1】:

    您可以这样做并相应地修改它。不要将 Button 和 label 作为单元格添加为节标题。

    var data: [[String:Any]]!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        data = [["sectionHeader": "section1","isCollapsed":true,"items":["cell1","cell2","cell3","cell4","cell5"]],["sectionHeader": "section2","isCollapsed":true,"items":["cell1","cell2","cell3","cell4","cell5"]],["sectionHeader": "section3","isCollapsed":true,"items":["cell1","cell2","cell3","cell4","cell5"]],["sectionHeader": "section4","isCollapsed":true,"items":["cell1","cell2","cell3","cell4","cell5"]]]
        // Do any additional setup after loading the view, typically from a nib.
        tableView.reloadData()
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return data.count
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 40
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView.init(frame: CGRect.init(x: 0, y: 0, width: tableView.frame.size.width, height: 40))
        let label = UILabel.init(frame: CGRect.init(x: 16, y: 0, width: 100, height: 40))
        label.text = data[section]["sectionHeader"] as? String
        view.addSubview(label)
    
        let button = UIButton.init(frame: CGRect.init(x: tableView.frame.size.width - 60, y: 0, width: 50, height: 40))
        button.setTitle("Data", for: .normal)
        button.setTitleColor(UIColor.blue, for: .normal)
        button.addTarget(self, action: #selector(self.sectionButtonTapped(button:)), for: .touchUpInside)
        button.tag = section
        view.addSubview(button)
        return view
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let isCollapsed = data[section]["isCollapsed"] as! Bool
        let item = data[section]["items"] as! [String]
        return isCollapsed ? 0 : item.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        let item = data[indexPath.section]["items"] as! [String]
        cell.textLabel?.text = item[indexPath.row]
        return cell
    }
    
    @objc func sectionButtonTapped(button: UIButton) {
        let section = button.tag
        let isCollapsed = data[section]["isCollapsed"] as! Bool
        if isCollapsed {
            data[section]["isCollapsed"] = false
        }
        else {
            data[section]["isCollapsed"] = true
    
        }
        self.tableView.reloadData()
    }
    

    输出:

    https://media.giphy.com/media/l3mZhZfzXKD0Ugecw/giphy.gif

    【讨论】:

    • 非常感谢!我可以知道如何检测我点击标题的时间吗?是否可以获得Section index和cell index?
    • 抱歉@NahidRaihan 我没听懂你?你能解释一下你想要什么吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-21
    • 2019-08-26
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-09
    相关资源
    最近更新 更多