How to determine the section number of a section title

There is a button in my UITableView header. Once the button is pressed inside, how can we find out which section the button belongs to? Since the table view is editable, set the button tag is not so good when some rows are deleted. I tried using indexPathForRowAtPoint: to get the index row of the first row in the section, but something strange happened. Is there a better approach?

Change 1:

Once a tag is used to identify the header section number, the tag will not be updated when some line has been deleted. From coz, you can reload the tableview to update the tag, but it doesn't seem to be that way.

For the strange behavior of indexPathForRowAtPoint: , I asked another question: The strange behavior of the UITableView method "indexPathForRowAtPoint:"

+2
source share
3 answers

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

 -(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 provided that the add-in of your button is a section header view:

 -(void)buttonPressed:(UIButton*)sender { int section = [self sectionNumberForView:sender.superview inTableView:_yourTableView]; } 

Hope this helps!

+3
source

I like @LuisCien because the OP wants to avoid tags. But (a) the answer should show how to move from the button to the section no matter how deep this button is found in the header view hierarchy, and (b) the provided answer associates section 0 with the case when the header is not found (if the view is passed to the method, not contained in the title).

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

You do not need to call this with a supervisor. Let the posterity check do the job.

 NSInteger section = [self sectionNumberForView:sender inTableView:_yourTableView]; 
+3
source

When creating each headerView and adding UIButton, you can set its tag to the value of the section and check this tag in the action method for buttons. Sort of...

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 } 
+1
source

Source: https://habr.com/ru/post/1484691/


All Articles