简体   繁体   中英

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.

Here's my tableView:cellForRowAtIndexPath method:

{
    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. 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):

        [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. 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.

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.

Either way, you're probably going to just use the standard deleteRowsAtIndexPaths:withRowAnimation: method. Check the UITableView documentation for more details about that method, specifically.

Please note that the tableview is the graphical representation (as per your requirements) of the array it is associated with. 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. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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