滑动即可删除

时间:2013-12-10 09:22:04

标签: objective-c uitableview swipe delete-row

我有一个tableview和一个单元格:D选择了披露附件。我用故事板创建了所有ui元素。 (没有在单元格的内容中添加视图) 我正在使用SWTableViewCell来实现刷卡到删除,但是除非我在方法上设置断点,否则一切似乎都能正常工作

#pragma mark -SWTableViewDelegate-

-(void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index
{

    NSIndexPath *path=[self.table indexPathForCell:cell];
    [anArray removeObjectAtIndex:path.row];
    [self.table deleteRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationRight];
}

这将有助于您了解我在这里所做的事情

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    SWTableViewCell *cell=(SWTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    if (!cell)
    {

        NSMutableArray *rightUtilityButtons = [NSMutableArray new];
        [rightUtilityButtons sw_addUtilityButtonWithColor:[UIColor colorWithRed:1.0f
                                                                      green:0.231f
                                                                       blue:0.188
                                                                      alpha:1.0f]
    title:@"Delete"];

        cell = [[SWTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell" containingTableView:_table leftUtilityButtons:nil rightUtilityButtons:rightUtilityButtons];
    }

    NSMutableDictionary *deDict=[[NSMutableDictionary alloc]initWithDictionary:[anArray objectAtIndex:indexPath.row]];

    cell.textLabel.text= [deDict objectForKey:@"Name"];

    return cell;
}

然而,我没有得到任何错误,因为当我尝试在模拟器上滑动时,它根本不起作用..

1 个答案:

答案 0 :(得分:0)

不需要使用SWTableViewCell只需覆盖UITableView委托的内置方法,以支持条件编辑&滑动即可删除

用于条件编辑 - 可选 -

//Override this method only if you are going to be returning NO for some items. 
//By default, all items are editable.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return YES if you want the specified item to be editable.
    return YES;
}
滑动删除

// Override to support editing the table view.
// for swipe to delete
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //add code here for when you hit delete
    }    
}