Swipe To Delete TableView Row

后端 未结 8 1003
暖寄归人
暖寄归人 2020-12-24 06:10

I have my array:

self.colorNames = [[NSArray alloc] 
initWithObjects:@\"Red\", @\"Green\",
@\"Blue\", @\"Indigo\", @\"Violet\", nil];

I\'ve

8条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-24 07:04

    For a start, your colorNames should be an NSMutableArray rather than an NSArray. You can’t add or remove objects from a regular (non-mutable) array; you’d have to recreate it each time you made a change. Switching that will make this easier. For your implementation of -tableView:commitEditingStyle:forRowAtIndexPath:, you’ll then be able to do something like this:

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

提交回复
热议问题