【问题标题】:Customize the delete button in UITableView自定义 UITableView 中的删除按钮
【发布时间】:2012-12-02 13:54:57
【问题描述】:

我有“滑动删除”TableViewCell 的功能。我想自定义删除按钮。

这可能吗?如何访问删除按钮?

【问题讨论】:

  • @beryllium 所以你的建议是创建一个自定义单元格?但是我已经构建了单元格,我宁愿设置一个手势识别器,然后将其设为自定义单元格。
  • 您想要删除按钮的操作或者您需要更改删除按钮
  • 看看我的回答,但要确保你需要一个 customcell @Spire
  • @Spire,你是如何自定义删除按钮的。分享你的答案。

标签: iphone xcode uibutton customization


【解决方案1】:

如果只需要更改按钮的标题,则只需要添加titleForDeleteConfirmationButtonForRowAtIndexPath方法即可。

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"NewButtonName";
}

【讨论】:

    【解决方案2】:

    用户执行滑动操作时会调用此方法

    只需将其放在您的 CustomCell 中

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

    你不应该这样做,你不应该改变 Apple 的默认视图。

    【讨论】:

    • 我找到了另一种删除方法,但感谢您的努力。我将在这方面接受更多教育,因为我将需要它用于其他项目。 thx 有一件事,我不使用自定义单元格,这就是为什么我需要有关如何更改 DELETE 外观的信息,使其看起来像 Nudge 按钮
    • 你不应该这样尝试,你不应该改变苹果的默认视图。你看到了吗?
    • 不正确,您可以进行各种自定义 UI hack,如果它们添加到产品中,Apple 将接受它们。我们一直在做自定义 UI。
    【解决方案3】:

    试试这个:

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

    【讨论】:

    • 好的,如何在// Your Code 部分调用删除按钮?我如何在UIButtonTypeCustom 中更改它?
    • 这是 Table 的内置功能,所以你可以做任何你想做的事情,比如从数组中删除值,例如 - [idArray removeObjectAtIndex:indexPath.row];其中 idArray 是具有 ids 的 NSMutableArray。最后只需通过调用 [tableView reloaddata] 重新加载您的表;其中 tableView 是您的表的名称。
    猜你喜欢
    • 2012-01-26
    • 2010-12-09
    • 1970-01-01
    • 1970-01-01
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    • 2011-01-28
    • 2017-02-11
    相关资源
    最近更新 更多