swift 带有“显示更多”按钮的可扩展UITableView

rqqzpn5f  于 12个月前  发布在  Swift
关注(0)|答案(3)|浏览(126)

问题是我怎么能做到这一点?

,按下按钮后,必须如下:

vptzau2j

vptzau2j1#

要实现这种功能,最简单的方法是:
1.首先,只将那些需要显示的数据存储到数组(或集合)中(如初始或较少的信息)
1.第二,只需执行一个操作,当单击SHOW MORE时,只需存储所有需要显示的数据,然后[Tableview Reload]。
问题解决了!

y4ekin9u

y4ekin9u2#

你需要两种细胞。在最后一个单元格中只放标签。你还需要在视图控制器中设置一个标志,用来记住视图是否展开。
然后,您必须正确处理UITableViewController(didSelectRowAtIndexPath, cellForRowAtIndexPath, numberOfRowsInSection)的委托方法

abithluo

abithluo3#

要在tableview单元格中创建Viewmore和Viewless,可以尝试以下方法:
如果你只有一个部分,你可以创建一个简单的布尔属性。
var isSectionsExpanded = false
如果isSectionsExpanded为true,则在您的“numberOfRowsInSection”中将显示5个单元格,否则将显示2个单元格

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return isSectionsExpanded ? 5 : 2 }

现在创建一个footerView,它有一个按钮,如下所示

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let footerView = UIView(frame: CGRect(x: 0, y: 0, width: table.frame.width, height: 40))
    let showMoreOrLessButton = UIButton(frame: CGRect(x: (footerView.frame.width/2)-50, y: -1, width: 100, height: 20))
    showMoreOrLessButton.setTitle(isSectionsExpanded ? "View less" : "View more", for: .normal)
    showMoreOrLessButton.addTarget(self, action: #selector(viewMoreButtonTapped(_:)), for: .touchUpInside)
    footerView.addSubview(showMoreOrLessButton)
    showMoreOrLessButton.tag = section
    return footerView
}

现在为页脚按钮创建一个目标方法,如下所示

@objc internal func viewMoreButtonTapped(_ sender: UIButton) {
    isSectionsExpanded.toggle()
    tableView.reloadSections(IndexSet(integer: sender.tag), with: .automatic) // Reload only the tapped section to update its cell count
}

这就是它享受!!

相关问题