自定义删除按钮在UITableView单元格中编辑

Sag*_*ari 23 iphone uitableview

我已经完成了这个显示以下代码的问题:

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"Sagar";
}
Run Code Online (Sandbox Code Playgroud)

如果我想要自定义图像而不是默认的红色按钮怎么办?

小智 34

在自定义单元格中实现此方法

- (void)willTransitionToState:(UITableViewCellStateMask)state{
    [super willTransitionToState:state];
    if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask) {
        for (UIView *subview in self.subviews) {
            if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"]) {
                UIImageView *deleteBtn = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 64, 33)];
                [deleteBtn setImage:[UIImage imageNamed:@"delete.png"]];
                [[subview.subviews objectAtIndex:0] addSubview:deleteBtn];
                [deleteBtn release];
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


huo*_*ong 7

这是一个迟到的回复,但我希望有人可能会觉得这很有帮助.所以接受的答案对我来说似乎有点复杂,@ user1684899的答案只有在您只想更改删除按钮的外观时才有效.我想彻底改变删除按钮的外观(即它的框架,位置等),所以我的解决方案是将我自己的删除按钮添加到我的自定义单元格并保持最初隐藏,并且仅在单元格显示时显示在编辑模式下.更重要的是,我想隐藏iOS的原始删除按钮并支持向后兼容性,这是我的诀窍:

- (void)willTransitionToState:(UITableViewCellStateMask)state
{
    [super willTransitionToState:state];
    if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask)
    {
        if (!IS_IOS_7){
            for (UIView *subview in self.subviews)
            {
                if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"])
                {
                    // hide original button
                    [[subview.subviews objectAtIndex:0] setHidden:YES];
                    // show my custom button
                    [self.deleteButton setHidden:NO];
                }
            }

        } else {
            for (UIView *subview in self.subviews) {
                for (UIView *subview2 in subview.subviews) {
                    if ([NSStringFromClass([subview2 class]) rangeOfString:@"Delete"].location != NSNotFound) {
                        // hide original button
                        [subview2 setHidden:YES];
                        // show my custom button
                        [self.deleteButton setHidden:NO];
                    }
                }
            }
        }
    } else {
        // hide my custom button otherwise
        [self.deleteButton setHidden:YES];
    }
}
Run Code Online (Sandbox Code Playgroud)

别忘了添加:

[cell.deleteButton addTarget:self action:@selector(deleteEntryAtIndexPath:) forControlEvents:UIControlEventTouchUpInside];
Run Code Online (Sandbox Code Playgroud)

in cellForRowAtIndexPath,以便您可以在单击删除按钮时添加所需的任何内容.这是我的结果:

在此输入图像描述

  • 我NSLog子视图的课程.我没有找到UITableViewCellDeleteConfirmationControl.我只有几个_UITableViewCellSeparatorView (4认同)

小智 0

luvieere 是对的——如果你想要同样的“删除”比喻,你想把它保留在苹果提供的红色按钮上。为什么要改变它?它是全球公认的删除按钮标准。

不过,如果您想要像 Tweetie 这样完全改变滑动行为的东西,您可以使用像 ABTableViewCell 这样的东西,您只需在自己的视图中绘制即可。创建自定义表格视图,覆盖 -touchesBegan:withEvent:,并检查触摸。计算两次触摸的增量(在 TouchMoved:withEvent: 中)并移动您自己的视图。

  • “为什么要改变它?” - 因为老板让我们这么做,所以就是这样。 (15认同)
  • 这个答案回避了实际的问题,应该接受用户1684899提供的工作答案 (2认同)