I'm experiencing some weird behavior when trying to delete a row in a UITableView
.
I have two tableviews in my app, and they both have practically the same code on the tableView:commitEditingStyle:forRowAtIndexPath
: method. One is working fine, the other one has this issue: let's suppose I have more than, I don't know, X rows on my table view, so the cell reuse and scroll are both enabled. If I delete a row on the top of the list, the cells that are showing up on the bottom of the screen are being all duplicated by the bottom cell prior hitting the delete button. So if I delete 5 rows, for example, I'll have identical cells on my tableview. Then if I scroll the tableview they will all go back to normal. (or if I change viewcontroller and then go back to the tableview's one)
Here is the code of the working tableview:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
if (search.text.length == 0) {
[tableArray removeObjectAtIndex:indexPath.row];
[goalTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self setBG];
tempArray = [NSArray arrayWithArray:tableArray];
[[NSUserDefaults standardUserDefaults]setObject:tempArray forKey:@"currentArray"];
totalGoals = [[NSUserDefaults standardUserDefaults] integerForKey:@"totalGoals"];
totalGoals = totalGoals - 1;
[[NSUserDefaults standardUserDefaults] setInteger:totalGoals forKey:@"totalGoals"];
[[NSUserDefaults standardUserDefaults] synchronize];
} } }
And here is the code of the defective tableview:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
if (search.text.length == 0) {
[tableArray removeObjectAtIndex:indexPath.row];
[goalTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self setBG];
tempArray = [NSArray arrayWithArray:tableArray];
[[NSUserDefaults standardUserDefaults]setObject:tempArray forKey:@"completedArray"];
completedGoals = [[NSUserDefaults standardUserDefaults] integerForKey:@"completedGoals"];
completedGoals = completedGoals - 1;
[[NSUserDefaults standardUserDefaults] setInteger:completedGoals forKey:@"completedGoals"];
[[NSUserDefaults standardUserDefaults] synchronize];
} } }
Well, it's pretty much the same, so I'm a little lost here - They have the same code and one is working fine and the other isn't. Could it be an issue at the cellForRowAtIndexPath:
method? I'm wondering that, but couldn't find nothing on this method, plus they are practically the same too.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * identifier = @"Cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell==nil) {
//here I'm setting up the cells, labels, and stuff.
}
if (search.text.length == 0 && [filteredArray count] == 0) {
NSDictionary * dict = [tableArray objectAtIndex:[tableArray count] - 1 - indexPath.row];
//here I use the values on the dict to do some stuff with labels, etc.
}
return cell;
}
The thing is, I'm "fliping" the array with this line:
NSDictionary * dict = [tableArray objectAtIndex:[tableArray count] - 1 - indexPath.row];
If I don't do that, ie, when I do something like [tableArray objectAtIndex:indexPath.row]
only, the deleting issue won't happen at all! It's something that happens cause of the reversed array.
Oh, just to mention, when I'm populating the cell (when the view is loading, for example), the cell reuse is working just fine, no duplicate cells or anything like that. It's something that only happens when deleting rows.