【问题标题】:How to provide an custom delete button in the swipe-to-delete gesture of UITableView?如何在 UITableView 的滑动删除手势中提供自定义删除按钮?
【发布时间】:2010-04-19 19:58:47
【问题描述】:

我已经在 UITableView 中实现了默认的滑动删除手势,现在我觉得这个默认按钮实际上在上下文中看起来不太好。此外,它似乎没有本地化。在法语中,它仍将“删除”显示为文本。

如何在此处提供自定义按钮?这就是我实现那个人的方式:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    // do stuff, a lot of stuff...really a lot...
}

【问题讨论】:

    标签: iphone uitableview


    【解决方案1】:

    您希望在 UITableViewDelegate 协议中使用tableView:titleForDeleteConfirmationButtonForRowAtIndexPath:

    【讨论】:

    • 所以这只是为了改变标题。但是我也可以更改按钮本身吗?或者我必须提供我自己的实现吗?
    • 我不认为你可以轻易改变按钮,只是标题。如果你想要一个完全不同的按钮,我认为你必须完全重新实现滑动删除手势、按钮动画等等。
    【解决方案2】:

    创建一个自定义单元类并包含:

    - (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];
                }
            }
        }
    }
    

    @"delete.png" 是您的删除图片

    【讨论】:

    • 从不使用这样的东西是个好主意:[NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"]。它可能随时中断。
    猜你喜欢
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-23
    • 2021-03-28
    • 2012-01-26
    相关资源
    最近更新 更多