TableViewHelper
Swift 结构,用于通过名称动态显示和隐藏 UITableView 行。这是使用 Swift 5 编写的。
包含示例应用。
安装
- Cocoapods
- 将 TableViewHelper.swift 添加到您的项目中
基础
使用引用到 tableView 的实例化类的副本
helper = TableViewHelper(tableView:tableView)
将可用于显示的所有单元格添加到 helper 中,给每个单元格一个名称(多个单元格可以具有相同的名称)
helper.addCell(section: 0, cell: tableView.dequeueReusableCellWithIdentifier("S0R0")! as UITableViewCell, name: "S0R0")
helper.addCell(section: 0, cell: tableView.dequeueReusableCellWithIdentifier("S0R1")! as UITableViewCell, name: "S0R1")
helper.addCell(section: 1, cell: tableView.dequeueReusableCellWithIdentifier("S1R0")! as UITableViewCell, name: "S1R0")
引用 helper 类进行相关 UITableView 调用
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return helper.numberOfSections()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return helper.numberOfRows(in: section)
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return helper.cellForRow(at: indexPath)
}
通过名称隐藏或显示单元格(如果多个单元格具有相同的名称,则所有具有该名称的单元格都将显示或隐藏)
helper.hideCell("S0R0")
helper.hideCell("S0R1")
helper.showCell("S1R0")
所有公开方法
init(tableView:UITableView)
func addCell(section:Int, cell:UITableViewCell, name:String, isInitiallyHidden: Bool = false)
func hideInitiallyHiddenCells()
func hideCell(_ name:String)
func showCell(_ name:String)
func cellName(at indexPath:NSIndexPath) -> String?
func indexPathForCell(_ name:String) -> NSIndexPath? // first matching cell
func indexPathsForCell(_ name:String) -> [NSIndexPath]
func visibleCells(_ name:String) -> [UITableViewCell]
func cellIsVisible(_ name:String) -> Bool // // returns true if ALL cells with that name are visible
func numberOfSections() -> Int
func numberOfRows(inSection section: Int) -> Int
func cellForRow(at indexPath: NSIndexPath) -> UITableViewCell