简体   繁体   English

从核心数据表视图中删除多个单元格会删除错误的行

[英]Multiple cell deletion from core data tableview deleting wrong row

I have a UITableView that is listed in descending order and each tableview cell is associated with a file. 我有一个降序列出的UITableView ,每个tableview单元都与一个文件关联。 The data for the tableview is from a NSFetchResultsController . tableview的数据来自NSFetchResultsController I've enabled multiple row deleting but the deleting execution doesn't seem to be programmed correctly. 我启用了多行删除,但是删除执行似乎没有正确编程。 If I only have 5 rows, the fifth row will be at the top with an index 0 and a file title "track5". 如果我只有5行,第五行将位于顶部,索引为0,文件名为“ track5”。 Row 4 will have a file "track4" and an index 1 and so on all the way down to row 1 with a file "track1" and an index of 4. I also have an NSLog that outputs the index of each row being deleted and the associated file. 第4行将有一个文件“ track4”和一个索引1,依此类推一直到第1行,并有一个文件“ track1”和索引为4。我还有一个NSLog,它输出要删除的每一行的索引,并且关联的文件。

When deleting, if I select rows 4,3, and 2 out of the 5 rows, I end up deleting 4, 3, and 1. 删除时,如果我从5行中选择第4、3和2行,则最终删除4、3和1。
My NSLog first shows index 1 and track4 being deleted, next index 2 and track 3 being deleted, then index 3 and track1 instead of track2. 我的NSLog首先显示删除索引1和track4,然后删除索引2和轨道3,然后显示索引3和track1而不是track2。 I'm assuming this is happening because the rows are being deleted one by one and the index of each remaining row is changing because of the prior cells being deleted. 我假设发生这种情况是因为行被一一删除,并且剩余行的索引正在更改,因为先前的单元格被删除了。 If I select rows 2, 3, and 4 in that order, everything will delete perfectly. 如果我依次选择第2、3和4行,则所有内容都会完美删除。 Its only when i'm deleting in descending order. 仅当我按降序删除时。 Whats the best way to fix this? 解决此问题的最佳方法是什么?

-(void) newMultiDelete
{
    NSArray *selectedRows = [self.audioTable indexPathsForSelectedRows];

    BOOL deleteSpecificRows = selectedRows.count > 0;
    if (deleteSpecificRows)
    {
        for (NSIndexPath *selectionIndex in selectedRows)
        {
            Recording * recording = [self.fetchCon objectAtIndexPath:selectionIndex];
            NSString * pathOfLastTrack = [self.managedDocument pathOfLastTrack];

            NSLog(@"Index path %li and title %@", (long)selectionIndex.row, recording.audioURL);
            [self.managedDocument.managedObjectContext deleteObject:recording];
            NSURL * audioURL = [[NSURL alloc] initWithString:recording.audioURL];
            [self.managedDocument removeFile:audioURL];

         }
    }

    [self.managedDocument.managedObjectContext save:nil];
    [self.managedDocument updateSaveContext];
    [self.audioTable reloadData];
}

Build an array holding the objects to delete, before doing any deletions: 在执行任何删除操作之前,构建一个包含要删除对象的数组:

-(void) newMultiDelete
{
    NSArray *selectedRows = [self.audioTable indexPathsForSelectedRows];

    BOOL deleteSpecificRows = selectedRows.count > 0;
    if (deleteSpecificRows)
    {
        NSMutableArray *objectsToDelete = [NSMutableArray array];
        for (NSIndexPath *selectionIndex in selectedRows)
        {
            Recording * recording = [self.fetchCon objectAtIndexPath:selectionIndex];
            [objectsToDelete addObject:recording];
            NSLog(@"Index path %li and title %@", (long)selectionIndex.row, recording.audioURL);
        }
        for (Recording *recording in objectsToDelete)
        {
            NSString * pathOfLastTrack = [self.managedDocument pathOfLastTrack];
            NSURL * audioURL = [[NSURL alloc] initWithString:recording.audioURL];
            [self.managedDocument removeFile:audioURL];
            [self.managedDocument.managedObjectContext deleteObject:recording];
        }
    }
    [self.managedDocument.managedObjectContext save:nil];
    [self.managedDocument updateSaveContext];
    [self.audioTable reloadData];
}

Thus the deletions will not foul up the indexPaths for the other selected rows. 因此,删除操作不会占用其他选定行的indexPath。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM