Assertion Failure for UITableView selectRowAtIndexPath

為{幸葍}努か 提交于 2019-12-06 01:59:28

问题


I feel like this is an issue that requires special attention... my Google-fu is pretty good but I haven't been able to get anything useful.

This is the simplest thing, and yet I can't seem to figure out what the problem is with it.

I have a UITableView. It's a subview that my VC calls _form. I'm using it for styling purposes, not really to display data. It has 2 cells.

On a certain event, I'm trying to select a different cell, using selectRowAtIndexPath:animated:scrollPosition.

When I do this, it SIGABRTS.

A simple example:

- (IBAction)submitClicked:(id)sender
{
    [_submit setTitle:@"Wha!?" forState:UIControlStateNormal];
    NSIndexPath *row = [NSIndexPath indexPathWithIndex:0];
    NSLog(@"%d", [[_form indexPathForSelectedRow] row]);
    [_form selectRowAtIndexPath:row animated:YES scrollPosition:YES];
}

The button's title is changed, AND the table prints that the selected row is 0 or 1, but on trying to select the desired cell, it breaks:

2012-03-09 21:57:39.905 <omitted>[16329:207] 0
2012-03-09 21:57:39.908 <omitted>[16329:207] *** Assertion failure in -[NSIndexPath row], /SourceCache/UIKit_Sim/UIKit-1912.3/UITableViewSupport.m:2598
(gdb)

My assumption is that this implies something is wrong with how my table is configured, but I'm not sure what. Selecting the cells in the table normally (clicking it) is working, as indicated by the expected response in my tableView:didSelectRowAtIndexPath. Everything else works fine with how I have this set up, except this.

(Also, I other people replying with extra debug info, not just "(gdb)". How can I get this?)

Thanks!


回答1:


An indexpath for a tableview cell needs to have both a row and a section. Try creating your index path using the factory method (defined in NSIndexPath UIKit Additions) like this instead:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[_form selectRowAtIndexPath:indexPath animated:YES scrollPosition:YES];


来源:https://stackoverflow.com/questions/9644359/assertion-failure-for-uitableview-selectrowatindexpath

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