我有一个自定义的 UITableViewCell
,在 Storyboard
上有一个 UILabel
。我动态设置此 UILabel
的文本,并且需要相应地调整 UILabel
的宽度,因为在它之后我还有另一个必须保留的 UILabel
一边到另一边。
我正在尝试
> MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
> author = @"Some dynamic text";
> cell.authorLabel.text = author;
> [cell.authorLabel sizeToFit];
会发生什么:
在第一次渲染时,UILabel
仅保留在 storyboard
上设置的宽度,因此要么文本被裁剪,要么文本后面的空间过多。
滚动后,单元格出队,并正确应用 sizeToFit
方法,但为时已晚,因为第二个 UILabel
已经定位在错误的位置。
(由于信誉度限制,无法发布图片)
有什么想法吗?
最佳答案
也许你的意思是这样的:
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
author = @"Some dynamic text";
cell.authorLabel.text = author; <-- excluded in your code
[cell.authorLabel sizeToFit];
如果这不起作用,请尝试使用如下内容:
子类化 UITableViewCell 并重写以下方法:
- (void)setSelected:(BOOL) selected
{
[super setSelected:selected];
[self.authorLabel sizeToFit];
}
关于iphone - UILabel 动态宽度与单元格上的 sizeToFit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17787465/