【问题标题】:Custom TableViewCells are flickering while reloading tableview自定义 TableViewCells 在重新加载 tableview 时闪烁
【发布时间】:2023-03-15 18:45:02
【问题描述】:

从网上下载了示例聊天演示应用程序。我必须更改两个自定义单元格的布局方向(左侧传入消息,右侧传出消息)。截至目前,传入和传出单元格布局方向均保留。所以,我用 UISemanticContentAttributeForceRightToLeft 改变了传出单元格的方向。

左侧的传入消息,右侧的传出消息都可以正常工作。但是自定义tableview 单元格在重新加载表格视图时会闪烁。

请在不更改传出消息单元的设计的情况下帮助我。

【问题讨论】:

  • 请在此处分享您的某种代码,以便更好地理解您的问题。
  • 输出单元格:dispatch_async(dispatch_get_main_queue(), ^{ cell.contentView.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft; });
  • - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"MovieCell"; MoviesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; if (indexPath.row%2 == 0) { // 偶数行 dispatch_async(dispatch_get_main_queue(), ^{ cell.contentView.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft; }); } 返回单元格; }
  • @HardikS 你明白我的意思了吗?

标签: ios objective-c swift uitableview


【解决方案1】:

您应该从cellForrowAtIndexPath 中删除dispatch_async 内容。

该函数已在 UI 线程中运行,因此无需将任何内容分派到主队列中。

此外,由于单元格被重复使用,您还应该在indexPath.row%2 != 0 时将semanticContentAttribute 设置为从左到右,否则您最终只能从右到-滚动一段时间后离开单元格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"MovieCell";
    MoviesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    if (indexPath.row%2 == 0) {
        cell.contentView.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;
    } else {
        cell.contentView.semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
    }

    return cell; 
}

【讨论】:

  • 我想显示偶数行是 RTL,奇数行是 LTR 方向。这是我不改变设计的要求。
  • 是的,但您只设置了 RTL 属性。如果在奇数行上重用“偶数”RTL 单元格,它将保持其 RTL 属性并且不会显示为 LTR,因为在 您的 代码中从未分配 UISemanticContentAttributeForceLeftToRight。
  • 我添加了计时器 [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(reloadTableView) userInfo:nil repeats:YES];我注意到这里闪烁。 RTL 和 LTR 行为仅适用于 dispatch_async。
  • 为什么要每 5 秒重新加载一次 tableView?这不是很明智的表现。您能否更清楚地解释您的要求,并将您的整个 viewController 发布在您的原始问题中。
  • 我想在收到新消息时重新加载 tableview。我添加了用于测试目的的计时器
猜你喜欢
  • 2021-11-09
  • 2012-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-06
  • 2016-06-05
  • 1970-01-01
  • 2023-03-22
相关资源
最近更新 更多