UITabel在UITableViewCell中的位置在第一次尝试时失败 [英] UILabel position in a UITableViewCell fails on the first try

查看:80
本文介绍了UITabel在UITableViewCell中的位置在第一次尝试时失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚刚开始使用iOS开发,所以我希望我能在这里获得正确的详细信息......

Just getting my feet wet with iOS development, so I hope I'm getting the right level of detail here...

我有一个 UILabel ,加载在通过.xib创建的表格单元格中。实际上有几个标签,其中一个标签长度不同(因此包装高度不同)。正确覆盖 heightForRowAtIndexPath 等,并正确计算两个标签所需的高度,并将其分配给每个 .frame

I have a UILabel, loaded in a Table cell created via a .xib. Actually several labels, one of which is of varying length (and therefore varying wrapped height). heightForRowAtIndexPath, etc. are properly overridden, and the required height for both labels is being calculated correctly and assigned to each's .frame

一旦设置了两个尺寸,我设置较小(固定长度,总是一行高)标签的frame.origin.y以匹配较大尺寸的标签标签。这可以看作逐步通过 cellForRowAtIndexPath ,但在新创建的单元格的初始显示中,原点似乎在容器中不是非常垂直居中,而不是顶部。

Once both sizes have been set, I set the frame.origin.y of the smaller (fixed length, always one row high) label to match that of the larger label. This works as far as can be seen stepping through cellForRowAtIndexPath but on initial display of the newly-created cell, the origin seems to be not-quite-vertically-centered in the container, as opposed to stuck up top.

单击穿过单元格到另一个视图,然后返回到(现在重用的,可能已经是正确大小的容器),位置设置为之前,它出现在我预期的位置。

After clicking "through" the cell to a different view, and returning to the (now-reused, and presumably already properly-sized container), the position is set as before, and it appears where I expected it to.

似乎就像容器在 cellForRowAtIndexPath <之后被调整大小一样/ code>返回,并且在那里的某处找到了我们标签的新位置;这可以解释为什么以后重复使用单元格可以解决问题。

It seems like the container is being resized after cellForRowAtIndexPath returns, and that somewhere in there a new position is found for our label; this would explain why reusing the cell later fixes the problem.

我已经尝试了内外模式和基线设置,拉伸设置等各种明显合理的组合。无法解释和/或解决方法。

I've tried every apparently-sane combination of inner and outer Mode and Baseline settings, stretch settings, etc. At a loss for explanations and/or workarounds.

更新:

A有关更改自定义UITableViewCell中自定义UIButton位置的建议问题解决了这个问题。解决方案是继承 UITableViewCell 并覆盖 layoutSubviews ,从而有机会在子元素之后重新定位子元素。 TableView已经完成了正常的定位。一旦这个人在这里回应,或者赏金到期,我会用代码回答自己。

A suggestion on the Changing the position of custom UIButton in custom UITableViewCell question solved the problem. The solution was to subclass UITableViewCell and override layoutSubviews, thereby getting a chance to re-position the sub-elements after the TableView had done its normal positioning. Will close this once either that person responds here, or the bounty expires and I'll answer myself, with code.

推荐答案

我在UITableViewCell中做任何复杂的布局我试着让它远离cellForRowAtIndexPath。一个选项是在中进行布局 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 但我更喜欢封装自定义UITableViewCell类中的布局。

If I do any complex layout in a UITableViewCell I try to keep it out of cellForRowAtIndexPath. One option is to do layout in - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath but I prefer to encapsulate layout in a custom UITableViewCell class.

创建自己的UITableViewCell子类。在init方法中创建自定义按钮/标签/视图并将其添加到单元格的contentView中,或者通过访问者属性懒惰地创建和添加它。覆盖layoutSubviews并根据需要设置按钮。

Make your own UITableViewCell subclass. Create and add the custom button / label / view to the cell's contentView in your init method, or create and add it lazily via an accessor property. Override layoutSubviews and postion the button as desired.

这样的事情:

@implementation MyCustomCell

- (void) init
{
    self = [super initWithStyle: UITableViewCellStyleDefault reuseIdentifier: nil];
    if ( self != nil )  
    {
         _myButton = [[UIButton buttonWithType: UIButtonTypeRoundedRect] retain];
         [self.contentView addSubview: _myButton];
    }

    return self;
}

- (void) layoutSubviews
{
    [super layoutSubviews];

    // dynamic layout logic:
    if ( ... )
    {

         _myButton.frame = CGRectMake( 10, 10, 100, 30 );
    }
    else 
   {
         _myButton.frame = CGRectMake( 20, 10, 50, 30 );

   }
}

这篇关于UITabel在UITableViewCell中的位置在第一次尝试时失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆