如何识别节标题视图的节号 [英] How to identify the section number of a section header view

查看:115
本文介绍了如何识别节标题视图的节号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的标题中有一个 UITableView 的按钮。一旦按下内部的按钮,我们怎么知道该按钮属于哪个部分?由于tableview是可编辑的,因此在删除某些行时设置按钮的标记并不好。我尝试使用 indexPathForRowAtPoint:来获取第一行的indexPath属于该部分,但发生了一些奇怪的事情。有没有更好的方法?

There is a button in my header of a UITableView. Once the button is touched up inside, how could we know which section does the button belong to? As the tableview is editable, set the tag of the button is not that good when some rows is removed. I have tried using indexPathForRowAtPoint: to get the indexPath of the first row belongs to the section, but something weird happened. Is there any better approach?

编辑1:

一旦标签用于识别节的编号标题,删除某行时标记不会更新。 。堂妹,你可以重新加载tableview中更新标签,但似乎并不好

Once the tag is used to identify the section number of a header, the tag will not update when the some row has been deleted. Of coz you can reload the tableview to update the tag, but it seems not that good.

有关 indexPathForRowAtPoint的怪异行为:,我有张贴其他问题: UITableView的方法"的怪异的行为; indexPathForRowAtPoint: "

For the weird behavior of indexPathForRowAtPoint:, I have post another question: Weird behavior of UITableView method "indexPathForRowAtPoint:"

推荐答案

上面的答案对我来说似乎已经足够了,但是,如果你不想使用标签你可以创建一个方法,返回特定视图所属的 UITableView 部分,如下所示:

The above answer seems good enough for me, however, if you don't want to use tags you could create a method that returns the section of a UITableView a particular view belongs to, like so:

-(int)sectionNumberForView:(UIView*)view inTableView:(UITableView*)tableView {

    int numberOfSections = [tableView numberOfSections];

    int i=0;
    for(; i < numberOfSections; ++i) {
        UIView *headerView = [tableView headerViewForSection:i];
        if (headerView == view) {
            break;
        }
    }

    return i;
}

然后在你的Target-Action方法中,并假设你的按钮的超级视图是节标题视图:

Then inside your Target-Action method and assuming the superview of your button is the section header view:

-(void)buttonPressed:(UIButton*)sender {

    int section = [self sectionNumberForView:sender.superview inTableView:_yourTableView];
}

希望这会有所帮助!

这篇关于如何识别节标题视图的节号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆