简体   繁体   English

删除tableView行

[英]Delete tableView row

I have dynamic tableView with custom cells in it, i want to delete some cell if its value is 0, for example. 我有自定义单元格的动态tableView,例如,如果它的值为0,我想删除一些单元格。

Here's my tableView:cellForRowAtIndexPath method: 这是我的tableView:cellForRowAtIndexPath方法:

{
    NSString *CellIdentifier;
    UITableViewCell *cell;

    if (indexPath.row == 0){
        CellIdentifier = @"adress cell";
        CustomAdressCell *cell = [detailsTableView dequeueReusableCellWithIdentifier:CellIdentifier];

       if (cell == nil) {
            NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"CustomTimeCell" owner:self options:nil];
            cell = [objects lastObject];
        }
        cell.someValue = self.someVal;

      return cell;
    }

    else if (indexPath.row == 1){
        CellIdentifier = @"phone cell";
        CustomPhoneCell *cell = [detailsTableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil) {
            NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"CustomPhoneCell" owner:self options:nil];
            cell = [objects lastObject];
        }

        indexPathTest = indexPath;//index path for delete
        cell.someValue = self.someVal2;

        return cell;
    }
...
    else return cell;
}

For example: if i have cell.someValue = 0 on second row, i need to delete this row. 例如:如果我在第二行有cell.someValue = 0 ,我需要删除这一行。 How can i achieve this? 我怎样才能实现这一目标? Thanks! 谢谢!

EDIT: i'm trying to delete rows after i initialised my tableView in another VC with the following code(custom method): 编辑:我试图删除行后,我使用以下代码(自定义方法)在另一个VC中初始化我的tableView:

        [self.detailsTableView beginUpdates];
        [self.detailsTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPathTest]
                         withRowAnimation:UITableViewRowAnimationFade];
        [self.detailsTableView endUpdates];

And i'm having error: 我有错误:

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 (4), 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).'

Some other questions that didn't helped me: 其他一些问题对我没有帮助:

You'll probably need to delete the row whenever someValue gets set to 0. As in, have the deletion be a part of whatever process sets it to 0, rather than reacting abstractly based on some random event. 每当someValue设置为0时,您可能需要删除该行。如同,删除是任何进程将其设置为0的一部分,而不是基于某个随机事件抽象地做出反应。 Tap the "0" button or input 0 and dismiss the keyboard, and then check to see if the new value is 0. If it is, delete the row, and if it isn't, do nothing. 点击“0”按钮或输入0并关闭键盘,然后检查新值是否为0.如果是,则删除该行,如果不是,则不执行任何操作。

You could also set a timer, so that every X seconds it cycles through the table and deletes them as necessary, but that would be a lot more inefficient. 您还可以设置一个计时器,以便每隔X秒循环一次并根据需要删除它们,但这样效率会低得多。

Either way, you're probably going to just use the standard deleteRowsAtIndexPaths:withRowAnimation: method. 无论哪种方式,您可能只会使用标准的deleteRowsAtIndexPaths:withRowAnimation:方法。 Check the UITableView documentation for more details about that method, specifically. 有关该方法的更多详细信息,请查看UITableView文档

Please note that the tableview is the graphical representation (as per your requirements) of the array it is associated with. 请注意,tableview是与其关联的数组的图形表示(根据您的要求)。 That being said, when you remove a row from a table view you should remove that row/object from the array as well which you are using to construct your tableview. 话虽这么说,当您从表视图中删除一行时,您应该从数组中删除该行/对象,您正在使用它来构建您的tableview。 Fail in doing that and you will end up in a scenario where tableview and the array reflect different states (that's exactly what the error is telling you). 如果没有这样做,你将最终出现在tableview和数组反映不同状态的情况下(这正是错误告诉你的)。

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

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