UITableViewCell自定义编辑视图

13

我正在尝试替换单元格的默认编辑模式行为。

我不希望出现红色圆圈和左边附属视图。

进入编辑模式后,我需要将内容视图向左移动并显示我的删除按钮。

目前我的自定义UITableViewCell已经重写了:

-(void) setEditing:(BOOL)editing animated:(BOOL)animated
{
    /* don't call super */
//    [super setEditing:editing animated:animated];
    _isEditing = editing;

    CGFloat newContentViewX = 0.0f;
    UIColor *newDeleteButtonColor = [UIColor clearColor];

    if ( _isEditing )
    {
        newContentViewX = -40.0f;
        newDeleteButtonColor = [UIColor redColor];
    }

    if ( animated )
    {
        [UIView animateWithDuration:0.5f
                              delay:0.0f
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^
         {
             self.contentView.x = newContentViewX; //change frame.origin.x
             _deleteButton.backgroundColor = newDeleteButtonColor;
         }
                         completion:nil];
    }
    else
    {
        self.contentView.x = newContentViewX; //change frame.origin.x
        _deleteButton.backgroundColor = newDeleteButtonColor;
    }
}

-(BOOL) editing
{
    return _isEditing;
}

& 这是结果: setEditing:NO setEditing:YES

以上代码运行良好!直到开始滚动并且内容视图被重置回第一张图片。编辑标志没有保留我的内容视图框架更改。

我已经添加了这行代码:

[cell setEditing:_tableView.isEditing animated:NO];

在cellForRowAtIndexPath方法中, 即使它正确地调用并设置了编辑模式,我的contentview仍然处于原始位置,而不是更新后的位置。

这个问题的详细解释可以看看这个短视频: Youtube链接

还有其他人遇到过这个问题吗?


5
仅凭制作YouTube视频,你就应该获得+1(奖励分)。现在很容易理解。 - Sam B
3个回答

3
不要更改内容视图框架。只需创建您的按钮并将其设置为 editingAccessoryView,演示将为您完成。

是的,除了您仍然可以在左侧看到带有“-”符号的圆形按钮并且它会将内容视图向右移。 - Ryan
1
tableView:editingStyleForRowAtIndexPath: 中返回 UITableViewCellEditingStyleNone。您可能还想自定义单元格何时以及如何进入编辑模式(用于滑动删除)。 - Wain

2
你的问题是UITableView会“自动”使用你从cellForRowAtIndexPath返回的单元格来设置适当的状态布局。
最简单的解决方法是在你的单元格类中覆盖-layoutSubviews方法。
- (void)layoutSubviews
{
   if (self.editing)
  {
     CGRect bounds = self.bounds;
     // do your custom layout
  }
  else
    [super layoutSubviews];
}

那样应该就可以基本解决问题了。


1
看起来你正在遭受UITableView传递setEditting消息给其子单元格的副作用。文档指出,TV仅向可见单元格发送此消息。因此,当新的/已排队的单元格变为可见时,它尚未接收到setEditting消息,因此_editting = NO。因此,您需要在tableView:cellForRowAtIndexPath:方法中测试并设置单元格的_editting = YES,然后您应该获得所需的滚动行为和显示的删除按钮。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接