I have a data table filled with text in each table row. How do I attach to the event for the row being selected and know which row was selected?
views:
49answers:
2
+1
A:
Your UITableViewDelegate should implement the tableView:didSelectRowAtIndexPath:
which will be called whenever a row is selected
You can get the section and row that were selected form the indexPath passed into the method with
[indexPath row];
[indexPath section];
See more under the UITableViewDelegate protocol reference
Brandon Bodnár
2010-03-21 21:28:14
+2
A:
The object that is the UITableViewDelegate
for your UITableView
(usually the view controller that owns the table view) just needs to implement the tableView:didSelectRowAtIndexPath:
method. You can get the column and row from the parameter passed in. Be sure to conform to the Apple HIG and deselect the row in this method.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
// do something here
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Shaggy Frog
2010-03-21 21:29:51
Thanks for the great answer and sample code!
MikeN
2010-03-21 21:44:33
Thank you for pointing out this is also the right place to deselect rows. Too many people miss that.
toholio
2010-03-22 01:53:02