Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 881
sefiroths Avatar asked Dec 10 '12 20:12

sefiroths


2 Answers

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.

like image 187
Ramy Al Zuhouri Avatar answered Oct 19 '22 15:10

Ramy Al Zuhouri


- (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]];
}
like image 43
Omarj Avatar answered Oct 19 '22 16:10

Omarj