Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UICollectionViewCell UILabel带有NSMutableAttributedString attributedText更新速度慢&;显示_Ios_Objective C_Uilabel_Uicollectionview_Nsmutableattributedstring - Fatal编程技术网

Ios UICollectionViewCell UILabel带有NSMutableAttributedString attributedText更新速度慢&;显示

Ios UICollectionViewCell UILabel带有NSMutableAttributedString attributedText更新速度慢&;显示,ios,objective-c,uilabel,uicollectionview,nsmutableattributedstring,Ios,Objective C,Uilabel,Uicollectionview,Nsmutableattributedstring,在iPad和iPhone上使用iOS 8.1.2。我有UICollectionViewCells,它是从xib中使用initWithCoder:创建的,并显示在UICollectionView中 [_collectionView registerNib:[UINib nibWithNibName:@"HomeFeedCell" bundle:nil] forCellWithReuseIdentifier:CELL_IDENTIFIER]; 模板xib单元格包含带有默认属性文本的UILabel。

在iPad和iPhone上使用iOS 8.1.2。我有UICollectionViewCells,它是从xib中使用initWithCoder:创建的,并显示在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更新缓慢?

有点晚了,但似乎我也有同样的问题。对我来说,最简单的解决方案是在
中更新
UILabel
-(void)collectionView:(UICollectionView*)collectionView将显示单元格:(UICollectionViewCell*)用于ItemAtIndexPath的单元格:(NSIndexPath*)indexPath

(此方法在IOS 8及更高版本中可用)。
我在
UITableView中也遇到了同样的问题

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;

如果您在后台线程上执行所有这些操作,这就是为什么。。。在主线程上执行所有操作,它在没有文本的情况下不会显示,尽管显示速度可能很慢。不过,您不应该每次重新加载时都重新创建字符串和字体。当您创建数据源而不是填写CopeDeVIEW时,请考虑做这些事情。用时间分析器通过应用程序运行应用程序,看看它真正被卡在哪里。谢谢,我试过使用<代码> [自PrPrimeStudio主线程:@选择器(StabelelsFordDAT::):对象:DistaDATA WoistunLodo:No);code>以便在主线程上运行整个方法。但它仍然做同样的事情。这是提前将字符串作为数据源的一部分进行计算的一个好方法。我将尝试一下..我想这听起来像是在viewcontroller显示其视图之后调用更新collectionview的调用。即使您在mainQueue上抛出这些调用,它也要在VC被实例化和呈现之后才能运行。因此,我想说的是,在您实际结束呈现集合视图之前(在后台队列中)更新该视图的内容。