Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
902 views
in Technique[技术] by (71.8m points)

ios - How to identify the section number of a section header view

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?

Edit 1:

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
}

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];
}

Hope this helps!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...