编辑时防止UITableViewCell(contentView)缩进

fsc*_*idl 33 iphone cocoa-touch uitableview uikit

contentView.frame无论如何,是否可以保持始终如一tableView.editing?我已经试图覆盖layoutSubviews,willTransitionToState但这些选项也失败了.我似乎无法更改contentView的宽度.或者也许我的方法根本不可能......也许还有另一种解决方法.

我想实现的行为如下:我想标准textLabelUITableViewCell始终缩进而当进入的tableView编辑模式不改变位置.我可能面临的问题是detailTextLabel必须纠正意志的行为(例如,如果textLabel内容太长,则截断文本).我之所以不想实现自己UILabel的原因,是因为自定义子视图会大幅降低滚动性能.

我希望任何人都已经在他们UITableView身上实现了这样的东西,并且可以向我展示解决这个繁琐问题的方法.提前致谢!

编辑:我正在处理一个UITableView简单而不分组的风格.

Usm*_*.3D 102

使用UITableViewDelegate方法:

- (BOOL)tableView:(UITableView *)tableView 
        shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath 
{

    return NO;
}
Run Code Online (Sandbox Code Playgroud)

这适用于分组和非分组UITableView类型.但是,如果您有分组的tableview,则可以在单元格上使用此属性:

cell.shouldIndentWhileEditing = NO;
Run Code Online (Sandbox Code Playgroud)

  • Apple UITableViewCell类参考:shouldIndentWhileEditing`此属性仅对以分组样式(UITableViewStyleGrouped)创建的表视图有效; 它对UITableViewStylePlain表视图没有影响.我的表视图确实很简单...... (13认同)
  • 这应该被选为正确的答案,而不是上面的hacky答案. (10认同)
  • @Fscheidl`insIndentWhileEditingRowAtIndexPath:`确实适用于分组和非分组表.请注意,您指的是属性`shouldIndentWhileEditing`而不是委托方法`shouldIndentWhileEditingRowAtIndexPath`. (7认同)
  • 我已经尝试过了 - 不幸的是无济于事.就我而言,这不会改变任何事情. (4认同)

mvd*_*vds 10

幸运的是,iOS完全可以保持任何价值不变.

这条(丑陋的)代码将使框架固定为任何值origin.x==0.这很容易适应您的特定需求.

// put this in your UITableViewCell subclass
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    NSLog(@"observed value for kp %@ changed: %@",keyPath,change);
    if ( [keyPath isEqual:@"frame"] && object == self.contentView )
    {
        CGRect newFrame = self.contentView.frame;
        CGRect oldFrame = [[change objectForKey:NSKeyValueChangeOldKey] CGRectValue];
        NSLog(@"frame old: %@  new: %@",NSStringFromCGRect(oldFrame),NSStringFromCGRect(newFrame));

        if ( newFrame.origin.x != 0 ) self.contentView.frame = oldFrame;
    }
}

// add the cell as an observer for frame changes, somewhere in initialization:
[self.contentView addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionOld context:nil];

// IMPORTANT: remove the cell as an observer in -dealloc:
[self.contentView removeObserver:self forKeyPath:@"frame"];
Run Code Online (Sandbox Code Playgroud)

这将只允许框架更改为值origin.x==0.删除NSLog()发布版本的行.

我已经测试了几分钟,没有看到任何副作用.


Aec*_*Liu 8

基于Usman.3D

在故事板,属性indent While Editingchecked默认.它可以手动取消选中.它等于cell.shouldIndentWhileEditing = NO.

在此输入图像描述