我可以在自定义uitableviewcell中使用滑动删除功能吗?

Mhd*_*ali 0 uitableview ios ios6

我尝试过但刷卡不会触发tableView:willBeginEditingRowAtIndexPath:就是这样!从不出现删除按钮,是否有解决方法?

编辑:这是我的实现

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

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView beginUpdates];
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        if(_itemsArray == nil)
            NSLog(@"\n\nNIL ARRAY\n\n");
        NSLog(@"\nindexPath.row = %d\nItemsArray Count:%d",indexPath.row,_itemsArray.count);
        int row = [[[_itemsArray objectAtIndex:indexPath.row]valueForKey:@"itemRow"] integerValue];
        [_itemsArray removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [[self delegate]didDeletedBillItemRow:row];
    }

    [tableView endUpdates];
}
Run Code Online (Sandbox Code Playgroud)

kaa*_*101 7

tableView:willBeginEditingRowAtIndexPath:不适合处理这个问题.从官方文档中,您需要在UITableViewController中实现此方法:

注意:要启用表视图的滑动到删除功能(其中用户在行中水平滑动以显示"删除"按钮),必须实现tableView:commitEditingStyle:forRowAtIndexPath: 方法.

就像那样:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        //add code here for when you hit delete
    }    
}
Run Code Online (Sandbox Code Playgroud)