UITableView indexPathForSelectedRow
- (NSIndexPath *)indexPathForSelectedRow
Return Value
An index path identifying the row and section indexes of the selected row or nil if the index path is invalid.
Discussion of [UITableView indexPathForSelectedRow]
If there are multiple selections, this method returns the first index-path object in the array of row selections; this object has the lowest index values for section and row.
UITableView indexPathForSelectedRow example.
UITableView's -indexPathForSelectedRow method will allow you to determine which row is selected:
NSIndexPath *selectionPath = [vwDrugTable indexPathForSelectedRow];
if (index) {
NSLog(@"Selected row:%u in section:%u", [selectionPath row], [selectionPath section]);
} else {
NSLog(@"No selection");
}
To be notified when the selection changes, implement tableView:didSelectRowAtIndexPath: in your table view delegate:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"Selection Changed");
}
NSIndexPath *selectionPath = [vwDrugTable indexPathForSelectedRow];
if (index) {
NSLog(@"Selected row:%u in section:%u", [selectionPath row], [selectionPath section]);
} else {
NSLog(@"No selection");
}
To be notified when the selection changes, implement tableView:didSelectRowAtIndexPath: in your table view delegate:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"Selection Changed");
}
Example of [UITableView indexPathForSelectedRow].
NSIndexPath *path = [table indexPathForSelectedRow];
if (path){
row = [path row];
...
}
else{
// No cell selected
}
if (path){
row = [path row];
...
}
else{
// No cell selected
}
UITableView indexPathForSelectedRow example.
So if you had some code like this, indexPathForSelectedRow would be nil,
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if(![_self.tableView indexPathForSelectedRow])
{
NSLog(@"nothing selected, so selecting 1st one");
// sets the detail pane here.
}
}
but if the code was like this (note the viewWillAppear: at the end), then indexPathForSelectedRow would not be nil.
- (void)viewWillAppear:(BOOL)animated {
if(![_self.tableView indexPathForSelectedRow])
{
NSLog(@"nothing selected, so selecting 1st one");
// sets the detail pane here.
}
[super viewWillAppear:animated];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if(![_self.tableView indexPathForSelectedRow])
{
NSLog(@"nothing selected, so selecting 1st one");
// sets the detail pane here.
}
}
but if the code was like this (note the viewWillAppear: at the end), then indexPathForSelectedRow would not be nil.
- (void)viewWillAppear:(BOOL)animated {
if(![_self.tableView indexPathForSelectedRow])
{
NSLog(@"nothing selected, so selecting 1st one");
// sets the detail pane here.
}
[super viewWillAppear:animated];
}