UICollectionViewCell UILabel与NSMutableAttributedString的attributesText缓慢更新&显示

时间:2015-01-06 18:13:58

标签: ios objective-c uilabel uicollectionview nsmutableattributedstring

在iPad和iPad上使用iOS 8.1.2苹果手机。我有使用initWithCoder创建的UICollectionViewCells:来自xib并显示在带有

的UICollectionView中
[_collectionView registerNib:[UINib nibWithNibName:@"HomeFeedCell" bundle:nil] forCellWithReuseIdentifier:CELL_IDENTIFIER];

模板xib单元格包含带有默认属性文本的UILabel。单元格也设置为使用自动布局约束。

当细胞被实例化时&使用其数据更新我调用以下方法来更新属性文本:

- (void)setLabelsForData:(NSDictionary*)dict_data {

    NSMutableAttributedString* attributedText_date = [[NSMutableAttributedString alloc] initWithString:[self dateStringForTimestamp:[dict_data objectForKey:@"ts"]]];
    NSMutableAttributedString* attributedText_time = [[NSMutableAttributedString alloc] initWithString:[self timeStringForTimestamp:[dict_data objectForKey:@"ts"]]];

    UIFont* font_date = [UIFont fontWithName:@"Helvetica-Bold" size:12.0];
    UIFont* font_time = [UIFont fontWithName:@"Helvetica" size:12.0];

    [attributedText_date addAttribute:NSFontAttributeName value:font_date range:NSMakeRange(0, attributedText_date.length)];
    [attributedText_time addAttribute:NSFontAttributeName value:font_time range:NSMakeRange(0, attributedText_time.length)];

    dateLabel.attributedText = attributedText_date;
    timeLabel.attributedText = attributedText_time;

}

然而,当以模态方式推送UICollectionView时,最初显示视图中的单元格,并且每个显示其原始xib默认形式的UILabel属性文本大约1秒钟,然后最终使用正确的数据&格式。一旦显示了UICollectionView,虽然UILables在滚动浏览这些单元格和任何新单元格时没有出现此问题。在UICollectionView出现时,最初只在可见的那些上。

我试过了:

[cell setNeedsDisplay];
[cell setNeedsLayout];
[collectionView invalidateLayout];

在方法结束时。我甚至把整个方法放在主线程上,希望它是一个线程问题,但没有运气。

什么可能导致UILabel的缓慢更新?

1 个答案:

答案 0 :(得分:3)

有点晚了,但似乎我遇到了同样的问题,对我来说最简单的解决方案是更新UILabel中的 - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
(此方法在IOS 8及更高版本中可用)。
我也遇到了与UITableView

相同的问题
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;