Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewController select header for section

I have a UITableView with multiple sections. Each section has a section header (a custom view) is there an easy way to detect when someone selects the section header? (Just like didSelectRowAtIndexPath, but for the header?)

like image 688
rustybeanstalk Avatar asked Oct 13 '11 07:10

rustybeanstalk


People also ask

Which method is used to set title for section header in Table View?

tableView(_:titleForHeaderInSection:) Asks the data source for the title of the header of the specified section of the table view.

How can I make the Footerview always stay at the bottom in Uitableviewcontroller?

If you need to make the footer view fixed at the bottom then you can not use a TableViewController . You will have to use UIViewController , put your tableView as a subview. Put the footer also as another subview and its done.


2 Answers

This isn't radically different than @rckoenes answer, but it does provide a more orthodox way of handling events on views rather than using invisible buttons.

I'd rather add a UITapGestureRecognizer to my header view instead of adding invisible buttons and resizing them:

UITapGestureRecognizer *singleTapRecogniser = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)] autorelease]; [singleTapRecogniser setDelegate:self]; singleTapRecogniser.numberOfTouchesRequired = 1; singleTapRecogniser.numberOfTapsRequired = 1;    [yourHeaderView addGestureRecognizer:singleTapRecogniser]; 

and then:

- (void) handleGesture:(UIGestureRecognizer *)gestureRecognizer; 

You can use gesture.view to see which was touched. Then do whatever you need to do to find out which header it was (tags, data array lookup... )

like image 99
bandejapaisa Avatar answered Sep 22 '22 04:09

bandejapaisa


No there is no way to do it with the UITableViewDelegate.

What you can do is to add a button the size of the section header view and add it to the view. Set the tag of the button to the section index. Then just add the UIViewController as a target for the UIControlEventTouchUpInside.

Then by looking at the tag of the button you can see which section is clicked.

like image 25
rckoenes Avatar answered Sep 21 '22 04:09

rckoenes