具有动态高度的UITableViewCell中的UILabel [英] UILabel in a UITableViewCell With Dynamic Height

查看:105
本文介绍了具有动态高度的UITableViewCell中的UILabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UITableView中实现了动态行高的一些问题 - 但它不是我遇到问题的单元格,它是单元格内的UILabel。

I'm having some problems implemented dynamic row heights in a UITableView - but it isn't the cells that I'm having a problem with, its the UILabel inside of the cell.

单元格只包含一个显示文本的UILabel。我的tableView:heightForRowAtIndexPath:通过使用NSString的sizeWithFont:方法计算将在其中的标签的高度来正确调整每个单元格的大小。

The cell just contains a UILabel to display text. My tableView:heightForRowAtIndexPath: is correctly resizing each cell by calculating the height of the label that will be in it using NSString's sizeWithFont: method.

我有一个UITableViewCell的子类只保存连接在故事板中的UILabel属性。在故事板中,我将其行设置为0,因此它将使用所需数量的行,并且我将其lineBreak设置为Word Wrap。

I have a subclass of UITableViewCell that just holds the UILabel property that is hooked up in storyboard. In storyboard I've set its lines to 0 so it will use as many lines as it needs, and I've set its lineBreak to Word Wrap.

以下是我设置单元格的方法:

Here is how I'm setting up the cells:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  *)indexPath
{
    ExpandCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"  forIndexPath:indexPath];

   SomeObject *object = self.tableObjects[index.row];

    cell.myLabel.text = [object cellText];

    [cell.myLabel sizeToFit];

    return cell;
}

当我构建这个时,我得到了我的表视图,其中单元格的大小都是它们内容的正确高度,但标签都是1行,只是从单元格的一侧运行。但是,如果我滚动表格让单元格离开屏幕,然后滚动回到它们,它们的标签将正确调整大小,单元格将看起来我最初的预期。

When I build this, I get my table view with the cell's all sized to the correct height for their content, but the labels are all 1 line that just runs off the side of the cells. However, if I scroll the table so cell's leave the screen, and then scroll back to them, their label will be resized correctly and the cell will look how I expected it to initially.

我还尝试使用与计算行高相同的方法计算标签框架,并且我得到相同的行为 - 它不会正确绘制,直到它滚出屏幕并再次返回。

I have also attempted calculating the labels frame with the same method I'm calculating the row height with, and I get the same behavior - it doesn't draw correctly until it scrolls off of the screen and back on again.

我找到了两种方法来解决这个问题,这两种方法都不是可接受的解决方案。

I have found two ways to work around this, and neither are acceptable solutions.

首先,如果在viewDidAppear中:我在tableview上调用reloadData,单元格和标签第一次正确绘制。这对我的情况不起作用,因为我将添加和删除此表中的单元格,并且我不想在每次添加单元格时调用reloadData。

First, if in viewDidAppear: I call reloadData on my tableview, the cells and labels draw themselves correctly the first time. This won't work for my situation because I will be adding and removing cells to this table, and I don't want to call reloadData every time a cell is added.

第二种解决方法对我来说似乎很奇怪 - 如果我将字体设置保留在UILabel上的默认系统字体17中,则单元格会自行绘制。一旦我改变字体大小,它就会恢复到它没有正确绘制标签的行为,直到它离开屏幕并返回,或者在tableView上调用reloadData。

The second workaround seems very strange to me - if I leave the font settings at the default System Font 17 on the UILabel, the cells draw themselves correctly. As soon as I change the font size, it reverts to its behavior of not drawing a label correctly until it leaves the screen and comes back, or gets reloadData called on the tableView.

我很感激你对这个有任何帮助。

I'd appreciate any help with this one.

推荐答案

我最终通过alloc / init'标签来解决这个问题在cellForRowAtIndexPath中。我不完全确定为什么这是一个解决方案 - 但是我遇到的问题似乎与故事板(或者何时?)在单元格中创建对象有关。如果我在cellForRowAtIndexPath中的单元格中分配/初始化标签,那么一切都正确加载和调整大小。

I ended up resolving this by alloc/init'ing the label in cellForRowAtIndexPath. I'm not entirely sure why this is a solution - but it appears the problem I was experiencing has to do with how storyboard (or when, perhaps?) creates the objects within the cell. If I alloc/init the label in the cell in cellForRowAtIndexPath, everything loads and sizes correctly.

所以...我当前的修复是检查单元格是否有我的自定义标签在里面。如果没有,我分配/初始化标签并将其放入单元格中。如果它确实有一个,就像它已经出列的单元格一样,那么我只需在已经存在的标签中设置文本。

So... my current fix is to check if the cell has my custom label in it. If it doesn't, I alloc/init the label and put it in the cell. If it does have one, as in its a cell that's been dequeued, then I just set the text in the label that is already there.

不确定它是否是最好的解决方案,但它现在工作。

Not sure if its the best solution, but its working for now.

这篇关于具有动态高度的UITableViewCell中的UILabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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