在UITableView中执行commitEditingStyle时,如何创建“删除按钮”的自定义视图

时间:2013-09-05 22:42:07

标签: iphone uitableview animation uiview uibutton

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

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

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

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

4 个答案:

答案 0 :(得分: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
                }
            }
        }

答案 1 :(得分: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];
                }
            }
        }
    }

参考:

Customize the delete button in UITableView

create custom delete button for uitableview

Custom Delete button On Editing in UITableView Cell

答案 2 :(得分: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];
    }
}

答案 3 :(得分: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;
}
相关问题