Swift-單元格展開收起

UITableViewCell單元格點(diǎn)擊收起比較常見,簡(jiǎn)單的看一下效果圖:


FlyElephant_Cell.gif

通過Section的狀態(tài)來來控制Section中的Cell的高度來實(shí)現(xiàn),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> 數(shù)據(jù)初始化: <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: "簡(jiǎn)書", items: ["FlyElephant", "keso"]),
        ExpandSection(title: "編程語言", items: ["Swift", "Objective-C", "JavaScript", "Python"]),
        ExpandSection(title: "地區(qū)", items: ["北京", "河南", "江西"]),
    ]`</code></pre>

UITableView實(shí)現(xiàn):
<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點(diǎn)擊事件:
<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>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 概述在iOS開發(fā)中UITableView可以說是使用最廣泛的控件,我們平時(shí)使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,297評(píng)論 3 38
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,291評(píng)論 4 61
  • 作者:AppCoda,原文鏈接,原文日期:2015-11-16譯者:pmst;校對(duì):numbbbbb;定稿:num...
    梁杰_numbbbbb閱讀 2,455評(píng)論 1 14
  • 好難好難 被填空的真題難倒了:-( 繼續(xù)受虐吧!一天100題都還不夠??!
    orange1027閱讀 169評(píng)論 0 0
  • 題目 原題鏈接:A. Free Ice Cream 題意 最開始有m個(gè)冰淇淋,每次輸入一個(gè)+/-和數(shù)字,+代表進(jìn)貨...
    ss5smi閱讀 133評(píng)論 0 0

友情鏈接更多精彩內(nèi)容