繁体   English   中英

滚动调整UITableView的元素和高度

[英]Adjusting elements and the height of UITableView on scroll

我有一个带有多个UILabelsUITableView 问题是,当我从服务器接收数据时,这些单元格中的文本会动态更改。 当我加载视图控制器时,它工作正常。 但是当我滚动时,单元格的高度不会更新,因为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吗?

谢谢。

确定这里来的编辑答案:问题是你sizeToFitlayoutSubviews 只需点击此链接即可解决您的问题: 此处

如果您还希望单元格和标签动态调整为相应的文本大小,但同时您的时间标签位于其正下方,则必须根据文本,字体和字体大小确定uilabel的大小。 有关更多信息,请参见此处: 此处

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM