【问题标题】:Adjusting elements and the height of UITableView on scroll在滚动时调整元素和 UITableView 的高度
【发布时间】: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


【解决方案1】:

好的,编辑后的答案来了: 问题是您的sizeToFit 在您的layoutSubviews 中。只需点击此链接即可解决您的问题: here

如果您还希望单元格和标签动态调整其相应文本的大小,但同时您的时间标签直接位于其下方,您将必须根据文本、字体和字体确定 uilabel 的大小-尺寸。 浏览此处获取更多信息: here

【讨论】:

  • 我正在这样做。但这不会更新不可见的单元格的框架。
  • 标签的numberOfLines 是否设置为0?您在哪里创建标签, cellForRowAtIndexPath 没有帮助,因为您没有在此代码中使用标签。
  • 我已经在我的 customcell 类的 layoutSubviews 中做到了。现在更新问题。
  • 对不起。这没有帮助
  • 如果您更改 sizeToFit,我提供的链接中的答案有什么帮助?
猜你喜欢
  • 2019-08-15
  • 1970-01-01
  • 1970-01-01
  • 2012-07-22
  • 1970-01-01
  • 2016-05-20
  • 2014-12-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多