如何找到TableView的分区头索引路径?

4

我希望根据TableView的Section Header索引路径执行某种操作。是否有方法可以跟踪该节标题的索引路径?


2
你的意思是什么?一个部分的标题没有行,只有对该部分的引用。 - Mattias Farnemyhr
我们能否像检测行的indexpath一样检测它的indexpath? - user5034941
@sandesh,你找到做这件事的方法了吗? - user2363025
2个回答

2

简短回答

目前没有现成的方法。

解决方案

在您的自定义头部类中声明一个章节编号变量。

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

0

那么 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
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接