I have a UITableViewController that is populated dynamically depending on a previous selection by the user. In some cases, the user should be able to select multiple rows in the table but in other cases he should not. I want to automatically select the first row in either case, but I have found that it only works when I have allowsMultipleSelection = YES.
The following code is called during -(void)viewDidLoad
switch(self.field)
{
case people:
self.options = (NSArray *)[data objectForKey:@"People"];
self.tableView.allowsMultipleSelection = YES;
enter code here break;
case place:
self.options = (NSArray *)[data objectForKey:@"Places"];
self.tableView.allowsMultipleSelection = NO;
break;
case reason:
self.options = (NSArray *)[data objectForKey:@"Reasons"];
self.tableView.allowsMultipleSelection = NO;
break;
}
[self.tableView reloadData];
NSIndexPath *zeroPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView selectRowAtIndexPath:zeroPath animated:NO scrollPosition:UITableViewScrollPositionNone];
This works correctly for the “People” case but not for the others. I have tried setting allowMultipleSelection to YES and that solves the problem of the selection, but it is not acceptable in terms of the application logic.
Am I missing something about how these functions work? How can I programmatically select a row in a single selection table?
Fixed with the following from the template:
Still not sure exactly why it didn’t work before.