Swift-单元格展开收起

作者: FlyElephant | 来源:发表于2017-01-20 00:37 被阅读85次

    UITableViewCell单元格点击收起比较常见,简单的看一下效果图:


    FlyElephant_Cell.gif

    通过Section的状态来来控制Section中的Cell的高度来实现,Section的Model定义如下:
    <pre><code>`struct ExpandSection {
    var title:String!
    var items:[String]!
    var expanded:Bool

    init(title:String,items:[String],expanded:Bool = true) {
        self.title = title
        self.items = items
        self.expanded = expanded
    }
    

    }</code></pre> 数据初始化: <pre><code> tableView = UITableView(frame: CGRect.init(x: 0, y: 64, width:self.view.bounds.width, height: self.view.bounds.height - 64), style: .plain)
    tableView?.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: "UITableViewCell")
    tableView?.delegate = self
    tableView?.dataSource = self

        self.view.addSubview(tableView!)
        
        sections = [
            ExpandSection(title: "简书", items: ["FlyElephant", "keso"]),
            ExpandSection(title: "编程语言", items: ["Swift", "Objective-C", "JavaScript", "Python"]),
            ExpandSection(title: "地区", items: ["北京", "河南", "江西"]),
        ]`</code></pre>
    

    UITableView实现:
    <pre><code>` func numberOfSections(in tableView: UITableView) -> Int {
    return sections.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.sections[section].items.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let tableViewCell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell")! as UITableViewCell 
        var content:String = ""
        if sections[indexPath.section].expanded {
            content = sections[indexPath.section].items[indexPath.row]
        }
        tableViewCell.textLabel?.text = content
        
        return tableViewCell
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view:UIView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: tableView.bounds.width, height: 44.0))
        view.backgroundColor =  UIColor(hex: 0x2E3944)
        
        let textlabel:UILabel = UILabel.init(frame: CGRect.init(x: 10, y: 0, width: view.bounds.width, height: 44.0))
        textlabel.text = sections[section].title
        textlabel.textColor = UIColor.white
        textlabel.font = UIFont.systemFont(ofSize: 15.0)
        view.addSubview(textlabel)
        
        let tapGesture:UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(tapSectionHeader(sender:)))
        view.tag = section
        view.addGestureRecognizer(tapGesture)
        
        return view
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 44.0
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.0
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return sections[indexPath.section].expanded ? 44 : 0
    }`</code></pre>
    

    SectionHeader点击事件:
    <pre><code>` func tapSectionHeader(sender:UITapGestureRecognizer) {
    let section:Int = (sender.view?.tag)!
    sections[section].expanded = !sections[section].expanded

        for i in 0..<sections[section].items.count {
            tableView?.reloadRows(at: [IndexPath(row: i, section: section)], with: .automatic)
        }
    }`</code></pre>

    相关文章

      网友评论

        本文标题:Swift-单元格展开收起

        本文链接:https://www.haomeiwen.com/subject/hdlwbttx.html