我有一个UITableView
多个UILabels
。问题是当我从服务器接收数据时,这些单元格中的文本会动态变化。当我加载视图控制器时它工作正常。但是当我滚动时,单元格的高度不会更新,因为heightForRowAtIndexPath
只被调用一次。调整元素和滚动上的UITableView的高度
下面是截图:
正如我在截图所展示的,该问题在标签大小这导致了间隙(由箭头示出)减少。
这里是我的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];
}
因此,要解决这种情况我在- (void) scrollViewDidScroll:(UIScrollView *)scrollView
称为
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:@[_tableIndexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
这解决了我的问题,但即使在将动画设置为UITableViewRowAnimationNone
之后,也会降低性能并且在设置之前跳转元素。有没有更好的方法来做到这一点?我应该在其他地方拨打reloadRowsAtIndexPaths
吗?
谢谢。
添加您的 - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;码。 – NANNAV
我编辑了这个问题。添加它。 – Anil