iPhone: Crash when reloading a tableview row
I'm using the following section of code - it should be simple, but it's causing me a lot of trouble:
NSUInteger index[] = {1,0}; // top row of section one
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath
indexPathWithIndexes:index length:2]] withRowAnimation:UITableViewRowAnimationNone];
The code crashes on the second line, with the error that "The number of rows in section zero is not equal to the number of rows in section zero before the update".
This error is true, I'm ch开发者_高级运维anging the number of rows in section zero. But surely this shouldn't effect whether or not I can reload a row in section 1!?
-
Am I misunderstanding how something works, or is something else going wrong somewhere? Any help or ideas are much appreciated :)
I think you also need to update the numberOfRowsInSection
.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return rowCount;
}
If you are changing (reloading) the number of rows in section 0 (probably by deleting or adding some rows in it) then -
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
is called to get the new number of rows. Here you need to return the correct number of rows i.e. one row less, if you deleted a row in section zero.
So basically, if you were returning [someArray count] for number of rows in section 0, you need to update this someArray as well.
精彩评论