【问题标题】:Swipe to delete滑动删除
【发布时间】:2013-12-10 09:22:04
【问题描述】:

我有一个表格视图和一个单元格: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;
}

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

【问题讨论】:

    标签: objective-c uitableview swipe delete-row


    【解决方案1】:

    无需使用 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
        }    
    }
    

    【讨论】:

    • 它们不是UITableView 方法,您不会覆盖它们。它们是委托方法。
    • 好吧,我现在正在做 commitEdittingStyle 和 titleForDeleteConfirmationButtonForRowAtIndexPath,看看效果如何
    • 那么就可以了
    • 如果这有助于找到您的解决方案,请接受我的回答,不要忘记向上
    • 问题是,我在情节提要上分配了代表,而不是以编程方式完成的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-17
    • 2021-01-02
    • 2023-03-21
    • 2013-10-02
    • 1970-01-01
    • 2016-12-12
    • 2014-07-28
    相关资源
    最近更新 更多