Reputation: 7227
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:"
Upvotes: 2
Views: 1749
Reputation: 62686
I like @LuisCien's answer because the OP wants to avoid tags. But (a) the answer should show how to get from the button to the section no matter how deeply the button is found in the header view's hierarchy, and (b)the answer supplied conflates section 0 with the case where the header is not found (if the method is passed a view not contained within a header).
// LuisCien's good suggestion, with modified test and a NotFound return.
-(NSInteger)sectionNumberForView:(UIView*)view inTableView:(UITableView*)tableView {
NSInteger numberOfSections = [tableView numberOfSections];
for(NSInteger i=0; i < numberOfSections; ++i) {
UIView *headerView = [tableView headerViewForSection:i];
if ([view isDescendantOfView:headerView]) return i;
}
return NSNotFound;
}
No need to call this with the outlet's superview. Let the descendent check do that work.
NSInteger section = [self sectionNumberForView:sender inTableView:_yourTableView];
Upvotes: 3
Reputation: 6432
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!
Upvotes: 3
Reputation: 5681
When creating each headerView and adding the UIButton, you could set it's tag to the value of the section, and check for that tag in your action method for the buttons. Something like...
In your creation:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(10, 10, 20, 20);
[button setTag:section];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:button];
return view;
}
Then in your action method:
- (void)buttonPressed:(UIButton *)sender
{
int section = sender.tag;
// Do something based on the section
}
Upvotes: 1