UITableViewCell 内容视图帧在编辑模式下向右移动太多



我有一个UIImageViewUILabelUIButton在我的自定义UITableViewCell。当我将编辑模式设置为 YES 时,整个单元格帧向右移动太多。

上下滚动表格似乎可以修复它。

但是为什么会发生这种情况,我该如何真正解决它?

在自定义UITableViewCell中使用layoutSubviews和willTransitionToState。在您的 .h 文件中添加 int 状态;在您的 .m 单元格中添加

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

还要添加以下内容:

    - (void)layoutSubviews
    {
     [super layoutSubviews];
     self.contentView.frame = CGRectMake(0,
                                     self.contentView.frame.origin.y,
                                     self.contentView.frame.size.width,
                                     self.contentView.frame.size.height);
           if (self.editing )
            {
             switch (state) {
             case 2:
            // swipe action -> here you play with how much your content will move
             self.contentView.frame = CGRectMake(90,
                                                  self.contentView.frame.origin.y,
                                                  self.contentView.frame.size.width-90,
                                                  self.contentView.frame.size.height);
            break;
              }
            } else {
            NSLog(@"subview not in edit %i",state);
            self.contentView.frame = CGRectMake(0,
                                         self.contentView.frame.origin.y,
                                         self.contentView.frame.size.width,
                                         self.contentView.frame.size.height);
             [self setNeedsDisplay];
   }
}

这应该足以自定义缩进。

这通常是由于单元格的布局没有被调用而发生的 使用自定义单元格并在布局中设置框架子视图 这将解决您的问题

最新更新