Select uitableview row programmatically

我们两清 提交于 2020-01-22 10:55:25

问题


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


回答1:


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.




回答2:


- (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]];
}


来源:https://stackoverflow.com/questions/13808752/select-uitableview-row-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!