编辑时防止UITableViewCell(contentView)缩进

时间:2011-04-26 11:13:50

标签: iphone cocoa-touch uitableview uikit

是否可以保持contentView.frame始终相同,而不管tableView.editing?我已经尝试覆盖layoutSubviewswillTransitionToState,但这些选项也失败了。我似乎无法更改contentView的宽度。或许我的做法根本不可能...... 也许还有另一种解决方法。

我想要实现的行为如下:我希望textLabel的标准UITableViewCell 始终缩进,并且当tableView进入编辑模式时不会更改位置。我可能会面临的问题是,detailTextLabel的行为必须得到纠正(例如,如果textLabel内容太长,则截断文字)。我之所以不想实现自己的UILabel,是因为自定义子视图会大幅降低滚动性能。

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

编辑:我正在处理普通而非分组风格的UITableView

9 个答案:

答案 0 :(得分:99)

使用UITableViewDelegate方法:

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

    return NO;
}

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

cell.shouldIndentWhileEditing = NO;

答案 1 :(得分: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"];

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

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

答案 2 :(得分:8)

基于Usman.3D

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

enter image description here

答案 3 :(得分:2)

您必须覆盖layoutSubviews才能执行此操作。它适用于自定义缩进的级别,因此它不适用。

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

答案 4 :(得分:2)

即使shouldIndentWhileEditingRowAtIndexPath返回NO

,我的普通样式表也遇到了同样的问题

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

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

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

    if (indexPath.row == 0 && indexPath.section == 0) return NO;

    return YES;    
}

希望有所帮助

答案 5 :(得分:0)

您是否查看了UITableViewCell的{​​{1}}和shouldIndentWhileEditing属性?那些可能会对它进行排序。

答案 6 :(得分:0)

有点晚了,

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

[<viewInCell> setAutoresizingMask:UIViewAutoresizingNone];

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

答案 7 :(得分:0)

刚才我正在研究同样的问题。

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

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

答案 8 :(得分:0)

您始终可以获得带有索引路径的tableviewcell。使用该tableviewcell reuseidentifier,您可以避免调整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;
}
相关问题