编辑自定义UITableViewCell

时间:2016-09-16 15:57:17

标签: ios objective-c uitableview

我已经研究了这个,但似乎还没有找到解决方案。我有一个自定义的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.
}

2 个答案:

答案 0 :(得分:3)

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

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

    [self setNeedsLayout];
}

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

无关 - 在您的表格视图代码中,请勿使用isEqual:来比较两个表格视图,请使用==

if (tableView == self.tableView) {

你实际上确实想看看它们是否是相同的指针。

答案 1 :(得分:0)

相关问题