【问题标题】:iOS 7: UITableViewController: Changing/Replacing Delete ButtoniOS 7:UITableViewController:更改/替换删除按钮
【发布时间】:2013-10-17 09:22:41
【问题描述】:

免责声明:我知道调整这类东西不是最佳做法,因为随着 Apple 决定改变其内部行为,它可能会崩溃。

有一些解决方案,例如 https://stackoverflow.com/a/12511432/271150,似乎适用于以前的 iOS 版本,但不适用于 iOS 7。

查看控件层次结构时,我可以看到 UITableViewCellScrollView 中有一个 UITableViewCellDeleteConfirmationView。但是通过查看 layoutSubviews 或 willTransitionToState 中的 SubViews 集合,只有我自己的视图,UITableViewCellDeleteConfirmationView 没有出现。

那么,有没有人想出如何修改默认删除按钮/视图?

【问题讨论】:

  • 不。而且也没有正确回答。
  • 对于“如何调整你不应该调整的 UI 层次结构”没有正确的答案——它总是一个 hack。第二和第三个答案似乎很有效。
  • 问题真的是:你想要一个删除按钮吗?然后保持默认行为。你想要别的东西吗?然后实现你自己的 UITableViewCell 并添加一个手势识别器来隐藏/显示你的按钮。
  • 我在我的应用程序中使用了一个红色的 tintColor,它与删除按钮的背景颜色使用的橙红色严重冲突,我认为将它设置为与我的 tintColor 相同的 UIColor 非常理想为此添加我自己的手势识别器有点矫枉过正。如果我们可以针对这样的边缘情况直接修改它,那就太好了,这是个好问题。

标签: ios xamarin.ios ios7


【解决方案1】:

我了解您可能不想覆盖 Apple 的整个实现来编辑表格视图单元格,但我的应用程序遇到了类似的问题。就我而言,当用户从左向右滑动时,我需要两个按钮(更多和丢失)来显示,就像在 iOS7 的新提醒应用程序中一样。而不是使用:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

我创建了一个自定义单元格,它隐藏顶层下方的按钮,并使用平移手势识别器移动顶层,显示下方的按钮。因此,我可以完全控制显示哪些按钮以及它们的颜色等(见下文)。

static CGFloat const kEditButtonWidth = 82.0;

@interface MyCustomCell ()
@property (nonatomic, assign) CGFloat firstX;
@property (nonatomic, assign) CGFloat firstY;
@property (nonatomic, strong) UIView *topLayer;
@property (nonatomic, strong) UIButton *moreButton;
@property (nonatomic, strong) UIButton *lostButton;
@property (nonatomic, strong) UILabel *titleLabel;
@end

@implementation MyCustomCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    if (self) {

        // BOTTOM LAYER (MORE AND LOST BUTTONS)
        UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [aButton setFrame:CGRectZero];
        [aButton setTitle:@"More" forState:UIControlStateNormal];
        [aButton setTitleShadowColor:[UIColor clearColor] forState:UIControlStateNormal];
        [aButton setBackgroundColor:[UIColor colorWithRed:0.78 green:0.78 blue:0.78 alpha:1.0]];
        [[self contentView] addSubview:aButton];
        [self setMoreButton:aButton];

        aButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [aButton setFrame:CGRectZero];
        [aButton setTitle:@"Lost" forState:UIControlStateNormal];
        [aButton setTitleShadowColor:[UIColor clearColor] forState:UIControlStateNormal];
        [aButton setBackgroundColor:[UIColor colorWithRed:1.00f green:0.23f blue:0.19f alpha:1.00f]];
        [[self contentView] addSubview:aButton];
        [self setLostButton:aButton];

        // TOP LAYER
        UIView *aView = [[UIView alloc] initWithFrame:CGRectZero];
        [aView setBackgroundColor:[UIColor whiteColor]];
        [[self contentView] addSubview:aView];
        [self setTopLayer:aView];

        UIPanGestureRecognizer *aPanRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureAction:)];
        [aPanRecognizer setMinimumNumberOfTouches:1];
        [aPanRecognizer setMaximumNumberOfTouches:1];
        [aView addGestureRecognizer:aPanRecognizer];

        // title label
        UILabel *aLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [aView addSubview:aLabel];
        [self setTitleLabel:aLabel];
    }
    return self;
}

- (void)layoutSubviews {

    [super layoutSubviews];

    CGRect bounds = [[self contentView] bounds];
    CGRect frame = CGRectZero;

    // BOTTOM LAYER (MORE AND LOST BUTTONS)
    frame.origin.x = bounds.size.width-(kEditButtonWidth+kEditButtonWidth); // two buttons wide
    frame.size.width = kEditButtonWidth;
    frame.size.height = bounds.size.height;
    [[self moreButton] setFrame:frame];

    frame.origin.x += kEditButtonWidth;
    [[self lostButton] setFrame:frame];

    // TOP LAYER
    frame = bounds;
    CGPoint anchorPoint = CGPointMake(0.0f, [[[self topLayer] layer] anchorPoint].y);
    [[[self topLayer] layer] setAnchorPoint:anchorPoint];
    [[self topLayer] setFrame:frame];

    // title label
    frame.origin.x = 20.0;
    frame.origin.y = 4.0;
    frame.size.width = bounds.size.width-40.0;
    frame.size.height = 21.0;
    [[self titleLabel] setFrame:frame];
}

- (void)panGestureAction:(id)sender {

    // on the first touch, get the center coordinates (x and y) of the view
    CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self];
    if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
        [self setFirstX:[[sender view] center].x];
        [self setFirstY:[[sender view] center].y];
    }

    // add translated point to reference points
    translatedPoint = CGPointMake([self firstX]+translatedPoint.x, [self firstY]);
    [[sender view] setCenter:translatedPoint];

    // when pan ends (set final x to be either back to zero or showing buttons)
    if ([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
        CGFloat finalX = translatedPoint.x+(0.30*[(UIPanGestureRecognizer*)sender velocityInView:self].x);
        if (finalX < -1.0f*FMEditButtonWidth) {
            finalX = -2.0f*FMEditButtonWidth;
            [self setEditMode:YES];
        } else {
            finalX = 0.0f;
            [self setEditMode:NO];
        }

        // animate view
        [UIView animateWithDuration:1.0 delay:0.0 usingSpringWithDamping:1.0f initialSpringVelocity:1.0f options:UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState animations:^{
            [[sender view] setCenter:CGPointMake(finalX, [self firstY])];
        } completion:NULL];
    }
}

@end

希望这会有所帮助。

【讨论】:

  • 感谢分享!我会在接下来的几周内尝试一下。
  • 我尝试了代码,但我无法弄清楚为什么无法识别手势。该方法不会被调用。你能帮忙吗?
  • @alexdd55 在 initWithStyle: 中设置一个断点,以查看它是否正确触发并连接您的手势。
  • 如何检测何时隐藏更多和丢失按钮?我不知道怎么做,所以你一次只能滑动一个单元格。
【解决方案2】:

layoutSubviewswillTransitionToState: 中看不到视图的原因是删除按钮视图还不存在或者它还不是视图层次结构的一部分。 为了“看到”它,您需要推迟一点浏览视图层次结构的代码,并允许操作系统同时创建/添加视图:

- (void)willTransitionToState:(UITableViewCellStateMask)state
{
    [super willTransitionToState:state];

    if (state & UITableViewCellStateShowingDeleteConfirmationMask)
    {
        [self performSelector:@selector(findDeleteButtonViewThroughViewHierarchy:) withObject:self.subviews afterDelay:0];
    }
}

请注意,这是一个脆弱的解决方案,因为它基于可能随时更改的 Apple 内部实现。

这里是一个(几乎)完整的例子:

Customizing iOS 7 Delete button

【讨论】:

    【解决方案3】:

    不确定这是否适用于 ios 7,但请看一下:

    (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
      if (editingStyle == UITableViewCellEditingStyleDelete)
    
       {
    
        //   Your Code
    
        }
    }
    

    【讨论】:

    • 单击按钮时会调用此方法,所以为时已晚 - 但谢谢 ;-)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    • 2013-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多