【问题标题】:Prevent indentation of UITableViewCell (contentView) while editing编辑时防止 UITableViewCell (contentView) 缩进
【发布时间】:2011-08-12 23:11:30
【问题描述】:

是否可以保持contentView.frame 始终相同,而不管tableView.editing 是什么?我已经尝试覆盖 layoutSubviews 和 willTransitionToState 但这些选项也失败了。我似乎无法更改 contentView 的宽度。或者也许我的方法根本不可能...... 也许还有其他方法可以解决这个问题。

我想要实现的行为如下:我希望 UITableViewCell 的标准 textLabel 在 tableView 进入编辑模式时始终缩进并且不改变位置。我可能会面临的问题是必须纠正detailTextLabel 的行为(例如,如果textLabelcontent 太长,则截断文本)。我不想实现自己的UILabel的原因是因为自定义子视图会显着降低滚动性能。

我希望任何人已经在他们的 UITableView 中实现了类似的东西,并且可以向我展示解决这个乏味问题的方法。提前致谢!

编辑:我正在处理 UITableView 的普通而不是分组样式。

【问题讨论】:

  • 对我来说,这有一个错误。每当我进入编辑模式时,缩进就会跳来跳去——非常奇怪,非常难看。在 NIB 中,我必须将单元格的“样式”更改为其他内容,然后再改回“基本”。缩进现在恢复正常了。

标签: iphone cocoa-touch uitableview uikit


【解决方案1】:

使用UITableViewDelegate 方法:

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

    return NO;
}

这适用于分组和非分组UITableView 类型。但是,如果你有一个分组的 tableview,你可以在单元格上使用这个属性:

cell.shouldIndentWhileEditing = NO;

【讨论】:

  • 我已经尝试过了 - 不幸的是无济于事。就我而言,这不会改变任何事情。
  • Apple UITableViewCell 类参考:shouldIndentWhileEditing This property has an effect only on table views created in the grouped style (UITableViewStyleGrouped); it has no effect on UITableViewStylePlain table views. 和我的表格视图确实是简单的样式...
  • 啊,我稍后才注意到。您可以有两个文本标签数组,一个有缩进,另一个没有缩进。就在您将表格更改为编辑模式时,不要使用动画,即 animate:NO,然后将您的标签数组切换到具有没有缩进的文本的数组。当您从编辑恢复到正常模式时,请执行相反的操作。我希望它有所帮助。
  • @Fscheidl shouldIndentWhileEditingRowAtIndexPath: 确实适用于分组表和非分组表。请注意,您指的是属性shouldIndentWhileEditing,而不是委托方法shouldIndentWhileEditingRowAtIndexPath。
  • 这应该被选为正确答案,而不是上面的hacky答案。
【解决方案2】:

幸运的是,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"];

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

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

【讨论】:

  • 但它在 ARC 中崩溃。错误:类 UITableViewCellContentView 的实例 0xabd7510 已被释放,而键值观察者仍向其注册。观察信息被泄露,甚至可能被错误地附加到其他对象上。在 NSKVODeallocateBreak 上设置断点以在调试器中停止。这是当前的观察信息: ( Context: 0x0, Property: 0xa9c9b80>)跨度>
  • @harshitgupta 你应该可以通过将 removeObserver 调用放在 dealloc 以外的地方来解决这个问题。
  • 我想出了一些代码来解决没有 KVO removeObserver 问题的类似问题:stackoverflow.com/a/23833468/220338
【解决方案3】:

基于Usman.3D

在情节提要上,属性indent While Editing 默认为checked。可以手动取消选中。等于cell.shouldIndentWhileEditing = NO。

【讨论】:

    【解决方案4】:

    您必须覆盖 layoutSubviews 才能做到这一点。它适用于自定义缩进级别,因此它适用于无。

    请查看How can I change the amount of indentation on my custom UITableViewCell while editing?。我提供了一个如何更改缩进级别的示例。虽然它在我的示例应用程序中运行的 OP 并没有 100% 运行。我认为这将为您指明正确的方向。

    【讨论】:

    • 有趣的是,在问我的问题之前不久,我研究了你的代码,不幸的是我遇到了 OP 遇到的同样问题。在我看来,对 contentView.width 属性的任何更改似乎都没有任何效果。
    • 很抱歉没能帮上忙。
    • 我又试了一下,contentView的转换确实有效。我没有注意到的原因是textLabel 和detailTextLabel 不仅仅依赖于contentView.frame。
    【解决方案5】:

    即使shouldIndentWhileEditingRowAtIndexPath 返回 NO,我的普通样式表也遇到了同样的问题

    在我这边,我遇到了这个问题,因为我的第一行不应该被删除,而其余的表格视图单元格应该被删除。

    我添加了第二种方法并且它有效:

    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    
        if (indexPath.row == 0 && indexPath.section == 0) return NO;
    
        return YES;    
    }
    

    希望对你有帮助

    【讨论】:

      【解决方案6】:

      您查看过UITableViewCell 的shouldIndentWhileEditing 和indentationLevel 属性吗?那些可能会对其进行排序。

      【讨论】:

      • 其实我已经看过那些方法了,但是这些都行不通。如果我设置每个单元格的 indentationLevel 并且“UITableView”进入编辑模式,则已经缩进的单元格会进一步缩进。
      【解决方案7】:

      有点晚了,

      但我通过关闭 TableViewCell 中所有视图的 Autoresizing Mask 解决了这个问题:

      [<viewInCell> setAutoresizingMask:UIViewAutoresizingNone];
      

      或者直接在 Interface Builder 中关闭自动调整宽度和/或高度。

      【讨论】:

        【解决方案8】:

        我在研究同样的问题时遇到了这个问题。

        一个非常简单的解决方案是将缩进级别设置为负数 - 毕竟它是一个有符号整数。

        A [cell setIndentationLevel:-3] 非常适合我,给定默认缩进宽度 10,将标签移回普通表格的左侧。

        【讨论】:

          【解决方案9】:

          你总是可以得到一个带有 indexpath 的 tableviewcell。使用该 tableviewcell 重用标识符,您可以避免调整或不调整 tableview 单元格内容大小。我需要实现类似的功能以避免调整单独单元格的大小。 PFB 代码。

          -(BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath{
          
          BOOL shouldIndentWhileEditingRow = NO;
          
          UITableViewCell *lTableViewCell = [tableView cellForRowAtIndexPath:indexPath];
          
          /*Change the position of the target rect based on Sending messages or Receiving messages*/
          if ([lTableViewCell.reuseIdentifier isEqualToString:@"SendingChatCellIdentifier"]) {
          
              shouldIndentWhileEditingRow = NO;
          
          }else if ([lTableViewCell.reuseIdentifier isEqualToString:@"ReceivingChatCellIdentifier"]){
          
              shouldIndentWhileEditingRow = YES;
          }
          
          return shouldIndentWhileEditingRow;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多