我希望根据TableView的Section Header索引路径执行某种操作。是否有方法可以跟踪该节标题的索引路径?
我希望根据TableView的Section Header索引路径执行某种操作。是否有方法可以跟踪该节标题的索引路径?
目前没有现成的方法。
在您的自定义头部类中声明一个章节编号变量。
class CustomHeader: UITableViewHeaderFooterView
{
//
// MARK :- PROPERTIES
//
var sectionNumber: Int!
//
// MARK :- METHODS
//
override func awakeFromNib()
{
super.awakeFromNib()
// Initialization code ...
}
func configureWith(_ section: Int)
{
self.sectionNumber = section
}
} // end of class
然后在您的视图控制器中,viewForHeaderInSection
内实例化自定义头部时分配部分值。
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "selectHeaderIdentifier") as! CustomHeader
header.configureWith(section)
return header
}
header.sectionNumber
那么 func headerViewForSection 怎么样?
override func tableView(tableView: UITableView, headerViewForSection section: Int) -> UITableViewHeaderFooterView {
let headerCell = tableView.dequeueReusableCellWithIdentifier("HeaderCell")
// do some stuf, e.g.
switch (section) {
case 0:
headerCell.backgroundColor = UIColor.cyanColor()
default:
headerCell.backgroundColor = UIColor.redColor()
}
return headerCell
}