ios - UITableViewCell contentView 框架在编辑模式下向右移动太多

标签 ios objective-c uitableview

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

上下滚动表格似乎可以解决这个问题。

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

最佳答案

在您的自定义 UITableViewCell 中使用layoutSubviews 和 willTransitionToState。 在你的 .h 文件中添加 int state; 在您的 .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];
   }
}

这应该足以自定义缩进。

关于ios - UITableViewCell contentView 框架在编辑模式下向右移动太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27126896/

相关文章:

ios - apple-app-site-association 通用链接签名问题

ios - 如何在 swift3 中增加 String.Index?

iphone - 无法摆脱顶部导航栏

ios - 如何在 Objective-C 中按下第一个按钮时显示第二个按钮

ios - 无法通过 didSelectRowAtIndexPath 传递值

ios - Xcode 更改存储库

ios - 为什么我的 SpriteNode 没有被添加到场景中?

objective-c - 删除两个字符之间的字符串

ios - 将 TableView 点击位置转换为单元格 View 位置

iphone - 行数为 0 时 UITableView 中的 EXC_BAD_ACCESS 崩溃