简体   繁体   English

Obj-c - 滑动以删除 tableview 行?

[英]Obj-c - Swipe to delete tableview row?

I'm having the strangest issue.我遇到了最奇怪的问题。 I'm using the below code to allow my user to swipe over a row in my tableview, have it reveal the delete button, and upon tapping delete, the row should disappear.我正在使用下面的代码来允许我的用户在我的表格视图中滑动一行,让它显示删除按钮,然后点击删除,该行应该消失。 That said, this all seems to work when I remove the animation line, but this one line is crashing my app:也就是说,当我删除 animation 行时,这一切似乎都有效,但是这一行正在使我的应用程序崩溃:

 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];

Of course, I need the animation in order to visually represent the removal of the row.当然,我需要 animation 以直观地表示行的删除。

The error I get is:我得到的错误是:

*** Assertion failure in -[UITableView _Bug_Detected_In_Client_Of_UITableView_Invalid_Number_Of_Rows_In_Section:], UITableView.m:2435 2021-02-03 18:06:50.543199-0800 Docket[28322:1330031] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).' *** Assertion failure in -[UITableView _Bug_Detected_In_Client_Of_UITableView_Invalid_Number_Of_Rows_In_Section:], UITableView.m:2435 2021-02-03 18:06:50.543199-0800 Docket[28322:1330031] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason :'无效更新:第0节中的行数无效。更新后现有节中包含的行数(1)必须等于更新前该节中包含的行数(1),加或减从该部分插入或删除的行数(0 插入,1 删除)加上或减去移入或移出该部分的行数(0 移入,0 移出)。

ViewController.m视图控制器.m

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    
   if (tableView == self.todaytableView) {
        
        NSLog(@"DELETING");
        
    return YES;
        
  }
    
    return NO;
}


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView == self.todaytableView) {
        
        return UITableViewCellEditingStyleDelete;
        
    }
    
    return NO;
}

- (BOOL)tableView:(UITableView *)tableview shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

    - (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
     
        if (tableView == self.todaytableView) {
            
        if (editingStyle == UITableViewCellEditingStyleDelete) {
          
            [self.finalTimes removeObjectAtIndex:indexPath.row];
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    
            
         }
            
        }
    
    }

first add this首先添加这个

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

then然后

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //remove the deleted object from your data source.
        //If your data source is an NSMutableArray, do this
        [self.dataArray removeObjectAtIndex:indexPath.row];
        [tableView reloadData]; // tell table to refresh now
    }
}

You could try wrapping your updates in beginUpdates and endUpdates (they must be paired), as below:您可以尝试将更新包装在 beginUpdates 和 endUpdates 中(它们必须配对),如下所示:

- (void)tableView:(UITableView *)tableView 
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath {
 
    if (tableView == self.todaytableView) {
        
        if (editingStyle == UITableViewCellEditingStyleDelete) {

            [self.finalTimes removeObjectAtIndex:indexPath.row];

            [tableView beginUpdates];
            [tableView deleteRowsAtIndexPaths:@[indexPath]
                             withRowAnimation:UITableViewRowAnimationLeft];
            [tableView endUpdates];
        }
    }
}

I don't think that is what causes the assert failure though.我不认为这是导致断言失败的原因。 You may want to double check that your您可能需要仔细检查您的

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

method is returning the correct value after altering the dataSource for your table, since it looks like that is the specific thing the error mentions.方法在更改表的数据源后返回正确的值,因为看起来这是错误提到的具体内容。 If the value returned by this method doesn't match the number rows you had in the first place minus the number of rows you just removed from your table, then the assert will fail.如果此方法返回的值与您最初拥有的行数减去您刚从表中删除的行数不匹配,则断言将失败。

I did see that you had我确实看到你有

[self.finalTimes removeObjectAtIndex:indexPath.row];

so it's possible you're already handling that, but I didn't want to assume anything about what was going on in tableView:numberOfRowsInSection:所以你可能已经在处理这个问题,但我不想假设 tableView:numberOfRowsInSection 中发生了什么:

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

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