ios - 如何在编辑UITableViewCell时删除删除按钮?

标签 ios uikit

我想自定义我的单元格行为 - 我希望更改单元格 View 并显示不同的按钮。我成功了一半 - 当我滑动时, View 就会移动。

但是我在单元格的右侧有一个删除按钮,我想删除它并使用自定义按钮代替它。我应该使用什么来删除它以及在哪里绑定(bind)我的按钮来执行相同的操作?

我尝试使用两种方法,setEditing: 和 willTransitionToState,但这没有帮助。

如果我在其中任何一个中跳过调用父方法,我就不会得到我想要的删除按钮,但我的 View 在编辑后永远不会返回到其原始位置,并且其他单元格停止对滑动使用react。我需要做什么来解决这个问题?

-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:YES];

    if (editing) {

        [UIView animateWithDuration:0.3 animations:^{
            self.parentView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 100, 0);
        }];
    } else {
        [UIView animateWithDuration:0.3 animations:^{
            self.parentView.transform = CGAffineTransformIdentity;
        }];
    }
}

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

    if (state == UITableViewCellStateShowingDeleteConfirmationMask) {

        [UIView animateWithDuration:0.3 animations:^{
            self.parentView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 100, 0);
        }];
    }

    if (state == UITableViewCellStateDefaultMask) {

        [UIView animateWithDuration:0.3 animations:^{
            self.parentView.transform = CGAffineTransformIdentity;
        }];
    }
}

最佳答案

由于您的单元格是 UITableViewCell 的子类,并且您不想使用库存删除,因此您可能需要禁用它。最基本的方法是不返回任何单元格的编辑样式。

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleNone;

}

一些评论提到了您下一步可以做什么。

  1. 在单元格主视图下方添加一个附加 View 。
  2. 在该 View 右侧与删除按钮相同的位置添加一个按钮。
  3. 使用滑动手势识别器进行基本滑动或使用平移手势识别器
    用于拖动(正常行为)。
  4. 根据翻译更新顶 View 框架。

关于ios - 如何在编辑UITableViewCell时删除删除按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21821731/

相关文章:

ios - 协议(protocol)委托(delegate)在自定义 UIView 按钮中不起作用

ios - 使用 GCD 为 UITableView 异步下载图像

iphone - 如何正确removeFromSuperview?

ios - UIKit 中的可扩展 SwiftUI View 垂直居中

objective-c - viewDidLayoutSubviews 相当于 UITableViewCell

ios - Objective-C 在没有相机界面的情况下拍摄照片的简单方法。只需从相机中获取照片并保存到文件中

ios - 如何获得iOS应用程序的root特权?

ios - UIProgressView 动画只能正常工作一次

ios - 没有按钮操作的多个 UITextfields 的总和

ios - 绘制矩形 : withAttributes text center vertically or put some padding