TheM00s3
TheM00s3

Reputation: 3711

Invalid index path for use with UITableView

I have a method which is designed to clear all UITableViewCellAccessories and I get the following error when it is called
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid index path for use with UITableView. Index paths passed to table view must contain exactly two indices specifying the section and row. Please use the category on NSIndexPath in UITableView.h if possible.'

Here is the method

-(void)clearTableViewAccessories{
    NSIndexPath *indexPath = [NSIndexPath new];
     UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark){
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
}

Upvotes: 1

Views: 7600

Answers (3)

Danny S
Danny S

Reputation: 1291

The way you are initialising the indexPath object, creates an index path of length 0 and no path, which is formed by a section and a row.

You should create the indexPath with the designated initialiser

indexPathForRow:inSection:

Anyway I don't think your implementation is the best way to solve your problem. With your current setup you need to create an indexPath for every cell you want to create iterating through all of them, and you will get nil for any non-visible cell.

The best thing would be to start looking at UITableView dataSource methods, first of all tableView:cellForRowAtIndexPath: and inside here make the decision whether to clean the cell or not.

Upvotes: 10

Neprocker
Neprocker

Reputation: 99

Check your number or rows and number of sections , I had a similar issue which I solved by correctly populating number of rows in each section( if you have rows in section ). If you update the question with your data source eg array for section, and Rows , will be easier to answer

Upvotes: 0

Mundi
Mundi

Reputation: 80265

In your cellForRowAtIndexPath check if the cell should have no accessory. For example, you could modify your datasource or just use an ivar BOOL noAccessory. Set the accessory accordingly. Then simply call

[self.tableView reloadData];

Upvotes: 1

Related Questions