编辑和删除按钮的UITableView(Edit and Delete Button UITable

2019-10-19 01:13发布

我可以把一个UITableView进入编辑模式,并显示删除按钮。 我将如何添加删除按钮旁边的一个蓝色的“编辑”按钮?

就像向左滑动在iOS6的邮件,但邮件应用程序显示“更多”,我希望有一个“编辑”按钮。

Answer 1:

这不是自带的苹果的标准功能UITableViewCell -你将需要使自己的子类UITableViewCell用自己的刷卡识别。

这GitHub的项目是一个很好的开始-使用它,你应该能够使用这个代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"Cell";

    SWTableViewCell *cell = (SWTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        NSMutableArray *rightUtilityButtons = [NSMutableArray new];

        [rightUtilityButtons sw_addUtilityButtonWithColor:
                    [UIColor colorWithRed:0.78f green:0.78f blue:0.8f alpha:1.0]
                    title:@"More"];
        [rightUtilityButtons sw_addUtilityButtonWithColor:
                    [UIColor colorWithRed:1.0f green:0.231f blue:0.188 alpha:1.0f] 
                        title:@"Delete"];

        cell = [[SWTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
                    reuseIdentifier:cellIdentifier 
                    containingTableView:_tableView // For row height and selection
                    leftUtilityButtons:nil 
                    rightUtilityButtons:rightUtilityButtons];
        cell.delegate = self;
    }
...

return cell;

然后,实现对细胞的委托方法:

- (void)swippableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index {
    switch (index) {
        case 0:
            NSLog(@"More button was pressed");
            break;
        case 1:
        {
            // Delete button was pressed
            NSIndexPath *cellIndexPath = [self.tableView indexPathForCell:cell];

            [_testArray removeObjectAtIndex:cellIndexPath.row];
            [self.tableView deleteRowsAtIndexPaths:@[cellIndexPath] 
                    withRowAnimation:UITableViewRowAnimationAutomatic];
            break;
        }
        default:
            break;
    }
}


文章来源: Edit and Delete Button UITableView