sefiroths
sefiroths

Reputation: 1563

Select uitableview row programmatically

i have read a lot on this argument, i have tested this example that works: source code example. but this doesn't use storyboards (i don't know if this is the reason that make my project not working) i have made another project using storyboard, the same sequence of instruction doesn't work...

-(void)viewDidAppear:(BOOL)animated{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
    [firstController.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    [firstTable.delegate tableView:firstTable didSelectRowAtIndexPath:indexPath];
}

here is my project, is the same of the previous link, only adding viewDidAppear my not working project can someone tell me what's wrong? thanks

Upvotes: 12

Views: 22230

Answers (2)

Omarj
Omarj

Reputation: 1159

- (void)viewDidLoad {
    [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
}

If you want the callback from the delegate:

if ([self.tableView.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)]) {
    [self.tableView.delegate tableView:self.tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
}

Upvotes: 22

Ramy Al Zuhouri
Ramy Al Zuhouri

Reputation: 21966

I tried to study your code downloading the project.The problem is that firstController.tableView is nil.
So fix it:

[firstTable selectRowAtIndexPath:indexPath 
                        animated:NO 
                  scrollPosition:UITableViewScrollPositionNone];

Or assign firstController.tableView to the table view.

Upvotes: 25

Related Questions