从UITableView中删除Row

时间:2015-11-02 12:23:39

标签: objective-c uitableview core-data nsmutablearray

请在标记为重复之前阅读....

我目前正在尝试从UITableView删除行但没有任何结果。

我在UITableView的单元格上有一个删除按钮。一旦选中,我希望删除该行。

当选择删除按钮时,它会调用以下方法

- (void)didClickOnCellAtIndex:(NSInteger)cellIndex withData:(id)data
{


    [self.mainTableView beginUpdates];

    NSIndexPath *path = [NSIndexPath indexPathForItem:cellIndex inSection:0];
   //
    [self.mainTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:path] withRowAnimation:UITableViewRowAnimationFade];
   [filteredSubArrayForExpandableSection removeObjectAtIndex:cellIndex];
    [appDelegate.managedObjectContext deleteObject:[self.fetchedResultsController objectAtIndexPath:path]];
    [appDelegate.managedObjectContext save:nil];
     [self.mainTableView endUpdates];

   //
    // Save the context.
    NSError *error;
    if (![appDelegate.managedObjectContext save:&error]) {
        /*
         Replace this implementation with code to handle the error appropriately.

         abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
         */

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
       // abort();
    }
}

以上代码将返回以下问题

  

原因:无效更新:第0部分中的行数无效   更新后现有部分中包含的行数(8)   必须等于之前该部分中包含的行数   更新(8),加上或减去插入或删除的行数   从该部分(0插入,1删除)和加号或减号   移入或移出该部分的行(0移入,0移出)

当我重新加载应用时,行仍然存在。

好的,所以这种情况下的行数应该是7!

所以逻辑上我在beginUpdatesendUpdates方法之间添加了以下代码。

    numberOfRows = numberOfRows - 1;

然而,这会返回以下错误

  

- [ViewController deleteRow:]:无法识别的选择器发送到实例0x7a278a00(lldb)

我可以加载应用程序并且该行已被删除,但显然该应用程序在此之前已崩溃。

如果我从deleteRowsAtIndexPaths方法中移除didClick方法并使用以下NSFetchedController委托方法,我会得到完全相同的上述问题

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
    switch (type) {
        case NSFetchedResultsChangeInsert: {
            [self.mainTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
        }
        case NSFetchedResultsChangeDelete: {

            [self.mainTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

            break;
        }
        case NSFetchedResultsChangeUpdate: {
            //[self configureCell:(TSPToDoCell *)[self.tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
            NSLog(@"results changed update called");
            break;
        }
        case NSFetchedResultsChangeMove: {
            [self.mainTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [self.mainTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
        }
    }
}

更新

行数方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

        if (canExpand) {
         //if can expand section becomes top row for expansion effect thus we need to add 1 to show ALL rows
            return numberOfRows + 1;
        }
        else
        {
            //if cannot expand section we remove 1 from total

            return numberOfRows -1 ;
        }

        return 0  ;
}

任何有关此问题的帮助都将受到极大的欢迎

由于

0 个答案:

没有答案