2

我想自定义在 tableview 单元格上执行“向左滑动”操作时显示的删除按钮。我目前设置了 UITableViewCell 的子类,但也想自定义正在显示的删除按钮。

我的目标是在滑动时放置三个按钮。

我选择了另一个实现,我在每个单元格中都使用了 UIScrollview。

http://www.teehanlax.com/blog/reproduce-the-ios-7-mail-apps-interface/

4

4 回答 4

4

接受的答案在 iOS 7 上不起作用,因为现在 UITableViewCellContentView 介于两者之间。所以子视图循环现在应该看起来像这样(如果你也想支持旧的 iOS 版本,请使用当前接受的 iOS 6.1 答案-)

        for (UIView *subview in self.subviews) {
            for (UIView *subview2 in subview.subviews) {
                if ([NSStringFromClass([subview2 class]) rangeOfString:@"Delete"].location != NSNotFound) {
                    // Do whatever you want here
                }
            }
        }
于 2013-10-18T12:12:00.627 回答
3

这可能会帮助你。

- (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:@"arrow_left_s11.png"]];
                    [[subview.subviews objectAtIndex:0] addSubview:deleteBtn];
                }
            }
        }
    }

参考自:

自定义 UITableView 中的删除按钮

为uitableview创建自定义删除按钮

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

于 2013-09-06T00:26:51.113 回答
1
-(void)willTransitionToState:(UITableViewCellStateMask)state{
    NSLog(@"EventTableCell willTransitionToState");
    [super willTransitionToState:state];
    if((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask)
    {
        UIImageView *deleteBtn = [[UIImageView alloc]initWithFrame:CGRectMake( 320,0, 228, 66)];
        [deleteBtn setImage:[UIImage imageNamed:@"BtnDeleteRow.png"]];
        [[self.subviews objectAtIndex:0] addSubview:deleteBtn];
        [self recurseAndReplaceSubViewIfDeleteConfirmationControl:self.subviews];
        [self performSelector:@selector(recurseAndReplaceSubViewIfDeleteConfirmationControl:) withObject:self.subviews afterDelay:0];
    }
}
于 2014-02-17T12:19:18.893 回答
0

上面的解决方案不适用于 iOS 7,在- (void)willTransitionToState:,删除按钮不在视图层次结构中,所以我无法操作任何东西。我最终在- (void)didTransitionToState:. 下面的示例专门针对当我的单元格在顶部有一些间距时,我正在更改删除按钮的框架。如果你想自定义删除按钮,你可以在删除按钮顶部添加一个视图或者用你自己的 UIButton 替换它

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //your own stuff
        //for some reason, editingAccessoryView cannot be nil and must have a non-CGRectZero frame
        self.editingAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
    }
    return self;
}

- (void)didTransitionToState:(UITableViewCellStateMask)state
{
    [super didTransitionToState:state];
    if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask)
    {
        UIView *deleteButton = [self deleteButtonSubview:self];
        if (deleteButton) {
            CGRect frame = deleteButton.frame;
            frame.origin.y += defined_padding;
            frame.size.height -= defined_padding;
            deleteButton.frame = frame;
        }
    }
}

- (UIView *)deleteButtonSubview:(UIView *)view
{
    if ([NSStringFromClass([view class]) rangeOfString:@"Delete"].location != NSNotFound) {
        return view;
    }
    for (UIView *subview in view.subviews) {
        UIView *deleteButton = [self deleteButtonSubview:subview];
        if (deleteButton) {
            return deleteButton;
        }
    }
    return nil;
}
于 2014-06-13T22:41:19.790 回答