从自定义Tableview的section header获取indexPath

5

我已经创建了一个自定义视图,并将其添加到tableView的部分标题中。

 func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        if section == 2 {

            if (headerView == nil) {
                headerView = CustomHeaderSection(frame: CGRectMake(0,0,self.view.bounds.width,38))
            }
            headerView!.delegate = self

            if isExpandableCell {

                headerView!.arrowImage.transform = CGAffineTransformIdentity

            }else {

                headerView!.arrowImage.transform = CGAffineTransformMakeRotation(CGFloat(M_PI))

            }
            return headerView

        }

        return nil
    }

我在tableview区域上添加了一个自定义视图,并在该视图上放置了一个按钮。现在,当我单击该按钮时,如何获取所在的indexPath?


只有TableView的单元格具有索引路径。但是可能有一种现实的方法来获取标题所属的部分编号。 - NSGangster
3个回答

1
在CustomHeader类中声明一个节号变量,并在viewForHeaderInSection()实例化时分配其值。
在同一类中实现目标按钮操作。因此,在点击时获取self.section并将其传递给ViewController。
void onButtonTap (id sender)
        {
            UIButton button = (UIButton)sender;

        this.sectionHeaderViewDelegate.buttonTapped (this.section);

        }

0
在您的函数中,您可以将标签ID添加到按钮上,使其与indexPath相同。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("FoldingCell", forIndexPath: indexPath) as! myTableViewCell
...
cell.myButton.tag = indexPath.row
...
}

-1

在编写 tableView(tableView: UITableView, viewForHeaderInSection section: Int) 方法时,您可以将您的区头视图的 tag 属性分配为该区域的数量。


但是我只初始化了一次我的headerView。所以如果我分配一个标签,它会被修改,所以我将无法通过它进行检查。 - Bhavuk Jain
你的表格视图中只有一个表头吗? - Alexander Doloz

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