2011-04-19 49 views
6

我做了一个自定义的UITableViewCell,并做了正确的处理(将所有内容添加到contentView,并覆盖了layoutSubviews,所以我的子视图相对于contentView.bounds)。如何在编辑时更改自定义UITableViewCell上的缩进量?

当用户按下编辑按钮时,表格会缩进以留出红色删除标志的空间。这很好,但默认的缩进量太多了,并且毁掉了我的自定义单元格的外观。我怎样才能减少缩进量? setIndentationLeveltableView:IndentationLevelForRowAtIndexPath似乎没有做任何事情。

(有人问过类似的问题here,但它从未解决)。

+0

您是否找到任何解决方案?我患有同样的问题。谢谢。 – harshitgupta 2013-06-05 09:07:48

回答

15

您必须覆盖layoutSubviews并执行以下操作。并且不要忘记将缩进级别设置为大于0的东西:)对于自定义单元格,默认情况下不会应用缩进级别。

为了避免单次滑动删除手势的缩进,您必须做更多的工作。有一个状态反映了单元格的编辑状态。它不是公开的,但可以通过- (void)willTransitionToState:(UITableViewCellStateMask)aState访问,因此将它存储在属性中可以为layoutViews工作。

苹果的文档willTransitionToState:

注意,当用户刷细胞 删除它,细胞转变为 由 UITableViewCellStateShowingDeleteConfirmationMask 常数确定的状态,但 UITableViewCellStateShowingEditControlMask 是没有设置。

头文件

int state; 

... 

@property (nonatomic) int state; 

... 

单元实现

@synthesize state; 

... 

- (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 
     && ((state & UITableViewCellStateShowingEditControlMask) 
     && !(state & UITableViewCellStateShowingDeleteConfirmationMask)) || 
      ((state & UITableViewCellStateShowingEditControlMask) 
     && (state & UITableViewCellStateShowingDeleteConfirmationMask))) 
    { 
     float indentPoints = self.indentationLevel * self.indentationWidth; 

     self.contentView.frame = CGRectMake(indentPoints, 
              self.contentView.frame.origin.y, 
              self.contentView.frame.size.width - indentPoints, 
              self.contentView.frame.size.height);  
    } 
} 

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

完美工作。谢谢! – 2011-04-19 08:41:54

+0

啊...我注意到一个问题。无论用户按下编辑按钮还是轻扫删除,缩进都是相同的量。有什么方法可以拦截滑动删除并告诉它做一些不同的事情吗? – 2011-04-19 08:56:16

+0

好吧,我只是编辑我的代码,并提供了一个简单的解决方案。看一看。 – 2011-04-19 09:15:30

1

尼克·韦弗的最新答案的伟大工程,除了为录制利维指出了问题:

唯一的事情是现在,当你刷到删除,然后取消(点击屏幕上的其他地方)的电池突然跳到左边,然后向后滑动的删除按钮消失

我跑进同样的问题。我不确定它为什么会发生,但在设置contentView框架时解除动画效果。

... 
[UIView setAnimationsEnabled:NO]; 
self.contentView.frame = CGRectMake(indentPoints, 
             self.contentView.frame.origin.y, 
             self.contentView.frame.size.width - indentPoints, 
             self.contentView.frame.size.height); 
[UIView setAnimationsEnabled:YES]; 
相关问题