问题是我怎么能做到这一点?,按下按钮后,必须如下:
vptzau2j1#
要实现这种功能,最简单的方法是:1.首先,只将那些需要显示的数据存储到数组(或集合)中(如初始或较少的信息)1.第二,只需执行一个操作,当单击SHOW MORE时,只需存储所有需要显示的数据,然后[Tableview Reload]。问题解决了!
y4ekin9u2#
你需要两种细胞。在最后一个单元格中只放标签。你还需要在视图控制器中设置一个标志,用来记住视图是否展开。然后,您必须正确处理UITableViewController(didSelectRowAtIndexPath, cellForRowAtIndexPath, numberOfRowsInSection)的委托方法
UITableViewController
(didSelectRowAtIndexPath, cellForRowAtIndexPath, numberOfRowsInSection)
abithluo3#
要在tableview单元格中创建Viewmore和Viewless,可以尝试以下方法:如果你只有一个部分,你可以创建一个简单的布尔属性。var isSectionsExpanded = false如果isSectionsExpanded为true,则在您的“numberOfRowsInSection”中将显示5个单元格,否则将显示2个单元格
var isSectionsExpanded = false
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 }
这就是它享受!!
3条答案
按热度按时间vptzau2j1#
要实现这种功能,最简单的方法是:
1.首先,只将那些需要显示的数据存储到数组(或集合)中(如初始或较少的信息)
1.第二,只需执行一个操作,当单击SHOW MORE时,只需存储所有需要显示的数据,然后[Tableview Reload]。
问题解决了!
y4ekin9u2#
你需要两种细胞。在最后一个单元格中只放标签。你还需要在视图控制器中设置一个标志,用来记住视图是否展开。
然后,您必须正确处理
UITableViewController
(didSelectRowAtIndexPath, cellForRowAtIndexPath, numberOfRowsInSection)
的委托方法abithluo3#
要在tableview单元格中创建Viewmore和Viewless,可以尝试以下方法:
如果你只有一个部分,你可以创建一个简单的布尔属性。
var isSectionsExpanded = false
如果isSectionsExpanded为true,则在您的“numberOfRowsInSection”中将显示5个单元格,否则将显示2个单元格
现在创建一个footerView,它有一个按钮,如下所示
现在为页脚按钮创建一个目标方法,如下所示
这就是它享受!!