2013-12-17 35 views
1

我有一个UITableView多个UILabels。问题是当我从服务器接收数据时,这些单元格中的文本会动态变化。当我加载视图控制器时它工作正常。但是当我滚动时,单元格的高度不会更新,因为heightForRowAtIndexPath只被调用一次。调整元素和滚动上的UITableView的高度

下面是截图:

Before Scrolling

After Scrolling

正如我在截图所展示的,该问题在标签大小这导致了间隙(由箭头示出)减少。

这里是我的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吗?

谢谢。

+0

添加您的 - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;码。 – NANNAV

+0

我编辑了这个问题。添加它。 – Anil

回答

0

好的,这里来编辑回答: 问题是您的sizeToFit在您的layoutSubviews。只要按照这个链接来解决此问题: here

如果你也想在细胞和标签动态调整其相应的文本,但在同一时间你timelabel直接是它的下面,你将不得不决定大小您的uilabel基于文本,字体和字体大小。 在这里查看更多信息: here

+0

我正在那样做。但是,这并不会更新不可见的单元格的框架。 – Anil

+0

帧调整在我的heightForRowAtIndexPath中完成 – Anil

+0

标签的'numberOfLines'设置为0吗?你在哪里创建标签,cellForRowAtIndexPath没有帮助,因为你没有在这段代码中使用标签。 – dehlen