具有动态单元格高度的UITableView - 我需要做些什么来修复向下滚动? [英] UITableView with dynamic cell heights -- what do I need to do to fix scrolling down?

查看:149
本文介绍了具有动态单元格高度的UITableView - 我需要做些什么来修复向下滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在iPhone上建立一个小小的Twitter小客户端。当然,我在UITableView中显示推文,它们当然有不同的长度。我基于文本动态地改变单元格的高度:

I am building a teensy tiny little Twitter client on the iPhone. Naturally, I'm displaying the tweets in a UITableView, and they are of course of varying lengths. I'm dynamically changing the height of the cell based on the text quite fine:

- (CGFloat)heightForTweetCellWithString:(NSString *)text {
  CGFloat height = Buffer + [text sizeWithFont:Font constrainedToSize:Size lineBreakMode:LineBreakMode].height;
  return MAX(height, MinHeight);
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  NSString *text = // get tweet text for this indexpath
    return [self heightForTweetCellWithString:text];
  }
}

我正在使用算法显示实际的推文单元格在PragProg书中:

I'm displaying the actual tweet cell using the algorithm in the PragProg book:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *CellIdentifier = @"TweetCell";
  TweetCell *cell = (TweetCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
    cell = [self createNewTweetCellFromNib];
  }
  cell.tweet.text = // tweet text
  // set other labels, etc
  return cell;
}

当我启动时,所有可见的推文都显示得很好。然而,当我向下滚动时,下面的推文已经完全拼凑起来 - 看起来一旦一个单元格从屏幕上滚动,它上面一个单元格的单元格高度就会大于它应该的大小,并且模糊了部分它下面的细胞。当单元格到达视图的顶部时,它会自行重置并正确渲染。向上滚动没有任何困难。

When I boot up, all the tweets visible display just fine. However, when I scroll down, the tweets below are quite mussed up -- it appears that once a cell has scrolled off the screen, the cell height for the one above it gets resized to be larger than it should be, and obscures part of the cell below it. When the cell reaches the top of the view, it resets itself and renders properly. Scrolling up presents no difficulties.

这是一段视频,显示了这一点: http://screencast.com/t/rqwD9tpdltd

Here is a video that shows this in action: http://screencast.com/t/rqwD9tpdltd

我已经尝试了很多:在创建时调整单元框架的大小,对不同高度的单元格使用不同的标识符(即 [NSString stringWithFormat:@Identifier%d,rowHeight] ),在Interface Builder中更改属性...

I've tried quite a bit already: resizing the cell's frame on creation, using different identifiers for cells with different heights (i.e. [NSString stringWithFormat:@"Identifier%d", rowHeight]), changing properties in Interface Builder...

如果有其他代码段我可以发布,请告诉我。在此先感谢您的帮助!

If there are additional code snippets I can post, please let me know. Thanks in advance for your help!

推荐答案

叹息。事实证明,我并没有很好地调整所有属性。但至少我摆脱了这个bug。 :)

Sigh. Turns out I didn't tweak all the properties just quite well enough. But at least I'm rid of that bug. :)

通过确保检查来解决此问题 UITableViewCell 的剪辑子视图属性。

This behavior was fixed by being sure to check the "Clip Subviews" property of the UITableViewCell.

该行为是由声明我的推文文字引起的标签具有必要的最大高度 - 当表单元的子视图未被剪切时,上面单元格中的标签将呈现在下面单元格的顶部。由于SDK呈现单元格的顺序 - 向下 - 以及它如何将每个单元格堆叠在另一个之上,因此在第一次渲染屏幕时不可见。

The behavior was caused by declaring my tweet text label to be of the maximum height necessary -- when the subviews of the table cell were not clipped, the label in the cell above would render overtop of the cell below. This was not visible on the first rendering of the screen due to the order that the SDK renders the cells -- downward -- and how it stacks each one above the other.

这篇关于具有动态单元格高度的UITableView - 我需要做些什么来修复向下滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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