So I have a UITableView
that represents a video library. The datasource of the library is a NSMutableDictionary
that contains a NSMutableArray
for each of its keys. Each of the Arrays contains an Array with all the info for each value.
NSMutableDictionary
-> Foreach Key a NSMutableArray
that contains -> NSMutableArrays
.
tableSectionData
is my datasource.
I have been trying to insert a row on the first section and delete another row in another section. This is the code I am using:
EDIT This is the new attempt. I first update the datasource and then add and delete the corresponding rows that are indexed with my dataSource.
[mainTableView beginUpdates];
NSMutableDictionary *clone = [[NSMutableDictionary alloc]
initWithDictionary:self.tableSectionData copyItems:YES];
for (NSString *key in [clone allKeys]) {
if([key isEqualToString:@"Videos available for download"]) {
for (NSArray *videoArray in [clone objectForKey:key]) {
if ([[videoArray objectAtIndex:0] isEqualToString:helper.videoName]) {
[[self.tableSectionData objectForKey:@"Downloaded videos"]
addObject:videoArray];
NSUInteger insertIndex = [[self.tableSectionData
objectForKey:@"Downloaded videos"]
indexOfObject:videoArray];
NSIndexPath *pathToInsert = [NSIndexPath
indexPathForRow:insertIndex inSection:0];
NSArray *indexesToAddition = [NSArray
arrayWithObject:pathToInsert];
[mainTableView insertRowsAtIndexPaths:indexesToAddition
withRowAnimation:UITableViewRowAnimationFade];
[[self.tableSectionData
objectForKey:@"Videos available for download"] removeObject:videoArray];
NSUInteger deleteIndex = [[self.tableSectionData
objectForKey:@"Videos available for download"] indexOfObject:videoArray];
NSIndexPath *pathToDelete = [NSIndexPath
indexPathForRow:deleteIndex inSection:1];
NSArray *indexesToDeletion = [NSArray arrayWithObject:
pathToDelete];
[mainTableView deleteRowsAtIndexPaths:indexesToDeletion
withRowAnimation:UITableViewRowAnimationFade];
}
}
}
}
[mainTableView endUpdates];
I thought this would work since I have to first delete a row if I want to insert one (in the case I want to do both.)
The specific error is the following:
'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (4) must be equal to the number of rows contained in that section before the update (3), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
I know what the error says, that I don't have a corresponding number of rows on the section however I don't see my mistake on the code and I logged the datasource and it corresponds to the desired result.
Any help and suggestions would be greatly appreciated!
You have to do all updates inside begin/end updates methods. It's valid always if you have more than one UITableView update.
And don't forget update dataSource before endUpdates called!
[self.tableView beginUpdates];
// do your staff here, like:
[self.tableView inserRowsAtIndexPaths:indexPathsToInsert];
[self.tableView deleteRowsAtIndexPaths:indexPathsToDelete];
[self.tableView endUpdates];
your mistake here - is you are using different begin/end updates sections in your code sample
Where is your dataSource?
before delete
[dataSource removeObjectAtIndex:nIndexDelete];
[mainTableView deleteRowsAtIndexPaths:indexesToDeletion
withRowAnimation:UITableViewRowAnimationFade];
before add
[dataSource inserObject:object atIndex:nIndexAdd];
[mainTableView insertRowsAtIndexPaths:indexesToAddition
withRowAnimation:UITableViewRowAnimationFade];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With