【发布时间】:2013-12-17 08:14:02
【问题描述】:
我有一个UITableView 和多个UILabels。问题是当我从服务器接收数据时,这些单元格中的文本会动态变化。当我加载视图控制器时它工作正常。但是当我滚动时,单元格的高度不会更新,因为 heightForRowAtIndexPath 只被调用一次。
以下是截图:
正如我在屏幕截图中显示的那样,问题标签的大小会减小,从而导致出现间隙(如箭头所示)。
这是我的cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIndentifier = @"CustomCell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
if (cell == nil)
{
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
}
cell.question.autoDetectLinks = YES;
// Used to populate cell from NSDictionary
[self setDataToCell:cell AtIndexPath:indexPath];
return cell;
}
这是我的自定义单元格的layoutSubviews:
- (void) layoutSubviews
{
CGRect frame = self.question.frame;
frame.size.width = 277.0f; //you need to adjust this value
self.question.frame = frame;
self.question.numberOfLines = 2;
[self.question sizeToFit];
// Place time below question
CGRect timeFrame = self.time.frame;
timeFrame.origin.y = self.question.frame.origin.y + self.question.frame.size.height + 5;
self.time.frame = timeFrame;
[self.time sizeToFit];
}
所以为了解决这种情况,我打电话给
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:@[_tableIndexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
在- (void) scrollViewDidScroll:(UIScrollView *)scrollView
这解决了我的问题,但会降低性能,并且即使在将动画设置为 UITableViewRowAnimationNone 之后,元素也会在稳定之前跳来跳去。有更好的方法吗?我应该在其他地方打电话给reloadRowsAtIndexPaths吗?
谢谢。
【问题讨论】:
-
添加你的 - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;代码。
-
我已经编辑了这个问题。已添加。
标签: ios objective-c uitableview uilabel