简体   繁体   English

UITableViewCell标记不需要的缩进

[英]UITableViewCell labels unwanted indentation

I've just updated an Objective-C iOS app to use the "new" iOS 8 storyboard. 我刚刚更新了Objective-C iOS应用程序以使用“新”iOS 8故事板。 After updating all constrains, I noticed that while scrolling, some title labels (customs cells) seem to indent, what is not what I want them to do. 在更新所有约束之后,我注意到在滚动时,一些标题标签(海关单元格)似乎缩进,这不是我想让他们做的。 It seems to always happen when I change the scrolling direction, but I'm not entirely sure about how to reproduce it. 当我改变滚动方向时似乎总会发生,但我不完全确定如何重现它。 I have no idea where to search for a solution, but I'm pretty sure it doesn't have anything to do with the constrains. 我不知道在哪里搜索解决方案,但我很确定它与约束没有任何关系。 Below is a picture of some cells. 下面是一些细胞的图片。 The first two are properly indented, the last two not. 前两个是正确缩进的,最后两个没有。

What can I do to prevent this indentation? 我该怎么做才能防止这种缩进?

缩进的单元格标签

In your cellForRowAtIndexPath method, you could instead add a label onto your cell as a subview with your custom text. 在您的cellForRowAtIndexPath方法中,您可以使用自定义文本将标签添加到单元格中作为子视图。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] init];
    }

    UILabel *title = [[UILabel alloc] init];
    title.text = @"my text";
    title.frame = CGRectMake(0,0,desiredWidth,rowHeight); // setting the origin at (0,0) should position the label over that indentation you don't want
    [titleview addSubview:title];

    return cell;
}

I ran into this issue as well and did some research. 我也遇到了这个问题并做了一些研究。

To reproduce it, it seems like you need a custom table view cell that uses auto layout to lay out its subviews. 要重现它,您似乎需要一个自定义表格视图单元格,它使用自动布局来布局其子视图。 The indentation occurs when the table view is scrolled AND the scroll direction is changed AND the affected cell moves outside the visible screen AND subsequently back in view again WHILE the finger is not lifted off the screen during the change of direction. 当滚动表格视图并且滚动方向改变并且受影响的单元格移动到可见屏幕之外并且随后在改变方向期间手指未从屏幕抬起时再次返回视图时,发生缩进。

The obvious first suspect was cell reuse. 显而易见的首要嫌疑人是细胞再利用。 Overriding prepareForReuse in the custom cell implementation like this seems to solve the problem: 在这样的自定义单元格实现中覆盖prepareForReuse似乎可以解决问题:

-(void)prepareForReuse{
    [super prepareForReuse];
    [self layoutSubviews];
}

The question is whether or not this is a bug. 问题是这是否是一个错误。 It does seem like something happens that invalidates the auto layout constraints when a cell is queued for reuse. 当一个单元排队等待重用时,似乎发生了一些使自动布局约束无效的事情。 Further research (breakpoints, NSLog etc) shows that all the layout constraints remain in place with correct values. 进一步的研究(断点,NSLog等)表明所有布局约束都保持正确的值。 From there comes the conclusion that layoutSubviews could probably solve the problem, which turned out to be the case. 从那里得出结论,layoutSubviews可能解决问题,事实证明是这样的。

Is it a bug? 这是一个错误吗? Maybe not. 也许不吧。 The docs for prepareForReuse say: prepareForReuse的文档说:

For performance reasons, you should only reset attributes of the cell that are not related to content, for example, alpha, editing, and selection state. 出于性能原因,您应该仅重置与内容无关的单元格属性,例如,alpha,编辑和选择状态。 The table view's delegate in tableView:cellForRowAtIndexPath: should always reset all content when reusing a cell. tableView中的表视图委托:cellForRowAtIndexPath:在重用单元格时应始终重置所有内容

So a strict interpretation of that would be that when auto layout is involved you probably have to re-leyout in tableView:cellForRowAtIndexPath: by calling layoutSubviews on the cell from there, since the auto layout constraints are part of the content. 所以对此的严格解释是,当涉及自动布局时,您可能必须在tableView:cellForRowAtIndexPath中重新调用:通过从那里调用单元格上的layoutSubviews,因为自动布局约束内容的一部分。 This also seems to indicate that prepareForReuse might not be the intended place to re-layout. 这似乎也表明prepareForReuse可能不是重新布局的预期位置。 Anyway, both ways seem to do the job, if someone knows more details as to which way is better, please feel free to comment. 无论如何,两种方式似乎都可以完成这项工作,如果有人知道哪种方式更好的详细信息,请随时发表评论。

Are you changing view frames or some other UI programatically? 您是以编程方式更改视图框架还是其他UI?

Sometimes this happens if you are using auto layout and also changing views programatically. 有时,如果您使用自动布局并以编程方式更改视图,则会发生这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM