-->

Inserting row and deleting row simultaneously. UIT

2020-08-23 05:05发布

问题:

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.

tableSectionDatais 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!

回答1:

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



回答2:

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];


回答3:

I think you need to reload UITableView every time you insert or delete rows with returning exact number of rows in numberOfRowsInSection ...