【问题标题】:How to delete a row from UITableView on iPad如何从 iPad 上的 UITableView 中删除一行
【发布时间】:2015-05-10 22:34:04
【问题描述】:

我在向左滑动时实现了一个 tableView 和一个删除(正常方式)。当我点击删除按钮时,记录会从数据库中删除,但不会从视图中删除。我想立即将其从桌子上删除。为了看到记录已被删除,我需要返回上一个视图并再次返回 tableView,然后记录才消失。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
    NewArt *currArt = [self.lN_Dep objectAtIndex:indexPath.row];

    self.deptsub = [self.lN_Dep subarrayWithRange:NSMakeRange(indexPath.row, (self.lN_Dep.count - indexPath.row))];

    self.selectedURL = [currArt.link absoluteString];
    self.selected_ID = (NSString *)currArt.art_ID;
    self.selectedArt = currArt;

    NSArray *ArrayOfArtIDs_old = [ArtString componentsSeparatedByString:@","];
    NSMutableArray *ArrayOfArtIDs_new = [[NSMutableArray alloc]init];
    ArrayOfArIDs_new = [[NSMutableArray alloc] initWithArray:ArrayOfArtIDs_old];

    for (NSString *item in ArrayOfArtIDs_old) {
        if ([item isEqualToString:self.selected_ID]) {
            [ArrayOfArtIDs_new removeObject:self.selected_ID];
            NSString *updateArt = [self updatedArtIDs:ArrayOfArtIDs_new];
            if ([updateArt isEqualToString:@"true"]) {
                [self.tableView beginUpdates];
                [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
                [_lN_Dep removeObjectAtIndex:indexPath.row];
                //[_lN_Dep removeObject:indexPath];
                [self.tableView endUpdates];
            }
        }
    }
    [self bookMarkedFeed];
}

当我按下删除时程序中断并且这一行被突出显示。

[_lN_Dep removeObject:indexPath]; 

[_lN_Dep removeObjectAtIndex:indexPath.row];

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    抱歉,没有代表发表评论... 最新的错误是因为您的 NSArray 不可变。 试试:

    NSMutableArray *temp = [NSMutableArray arrayWithArray:self.lN_Dep];
    [temp removeObjectAtIndex:indexPath.row];
    self.lN_Dep = [NSArray arrayWithArray:temp];
    

    【讨论】:

    • 你不能写最后一行。将 NSMutableArray 转换为 NSArray 是不兼容的。
    • 其实'copy'更干净,但我看不出这是什么问题?
    • 我对您的代码做了些许改动。我不知道为什么,但这很好用。顺便说一句,我的 LN_dept 数组是可变数组。哈哈,这太令人困惑了。但它现在工作正常。
    【解决方案2】:

    _lN_Dep 是一个NSArray_NSArrayI -> _NSArrayI(mmutable) 是 NSArray 类集群的具体私有实现),因此它不支持任何删除方法。将其设为NSMutableArray 或将其复制到可变临时数组,删除那里的内容,将其分配回(作为友好副本)。

    【讨论】:

      【解决方案3】:

      使用建议的代码删除您的行:

      [tableView beginUpdates];
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
      [tableView endUpdates];
      

      您得到的错误是因为 tableView 数据源仍然包含您已删除的行的记录。您需要从数组中删除该对象。我认为它会是:

      [self.lN_Dep removeObjectAtIndex:indexPath.row];
      

      【讨论】:

      • 我编辑了代码。请看上面。在尝试了您的建议后,我仍然收到一条错误消息。
      【解决方案4】:

      使用下面这行删除动画,

      [tableView beginUpdates];
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
      [tableView endUpdates];
      

      【讨论】:

      • 我收到此错误:由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第 0 节中的行数无效。更新后现有节中包含的行数(4) 必须等于更新前该节中包含的行数 (4),加上或减去从该节插入或删除的行数(0 插入,1 删除)加上或减去行数移入或移出该部分(0移入,0移出)。'
      • 试试这个 - [NSArray arrayWithObjects:indexPath, nil] 代替 [NSArray arrayWithObject:indexPath]
      • 不,它不起作用。同样的错误。等等,我需要替换 NSArray 这个词吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 2017-03-02
      • 2012-03-11
      • 2018-12-11
      相关资源
      最近更新 更多