我已经对此进行了研究,但似乎尚未找到解决方案。我有一个自定义UITableViewCell(带有各种子视图,包括单选按钮,标签等)。当表格视图设置为编辑时,我希望+和-插入/删除编辑控件显示在单元格的最左侧。

如果我使用标准的UITableViewCell,则可以完美地工作。但是,在使用自定义单元格时,控件不会出现。有人对如何解决该问题有任何想法吗?

以下是我的表格视图代码的一些快照。...

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.isEditing) {
        if ([tableView isEqual:self.tableView]) {
            if (editingStyle == UITableViewCellEditingStyleInsert) {
                // ...
            }
            else if (editingStyle == UITableViewCellEditingStyleDelete) {
                // ...
            }
        }
    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView isEqual:self.tableView]) {
        if (indexPath.row == 0) {
            return UITableViewCellEditingStyleInsert;
        }
        else {
            return UITableViewCellEditingStyleDelete;
        }
    }
    else {
        return UITableViewCellEditingStyleNone;
    }
}

和自定义表格视图单元格代码...
- (void)awakeFromNib
{
    [super awakeFromNib];
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [self setNeedsLayout];
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    [self configureConstraints];
}

- (void)configureConstraints
{
    // This is where the cell subviews are laid out.
}

最佳答案

您没有在自定义单元格中正确实现setEditing:animated:方法。您忘记了拨打super:

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

    [self setNeedsLayout];
}

这是一种罕见的重写方法,您不调用super

不相关-在表格视图代码中,请勿使用isEqual:来比较两个表格视图,请使用==
if (tableView == self.tableView) {

您实际上确实希望查看它们是否是相同的指针。

关于ios - 编辑自定义UITableViewCell时未出现插入/删除编辑控件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39535682/

10-10 21:30
查看更多