在TableView的Cell中用到了YYLabel的富文本,然后在刷新的時候頁面總是會閃一下。

IMG_E9F10EC08145-1.jpeg
一直以為是tableView刷新的問題導(dǎo)致的,最后發(fā)現(xiàn)是YYLabel中對富文本賦值導(dǎo)致的。
這是之前出問題的代碼:
[[RACObserve(self, model) ignore:nil] subscribeNext:^(id _Nullable x) {
((YYLabel *)self.detailLab).attributedText = [LHTemplateCellViewModel buildDetailTextWithModel:x];
}];
[[self rac_prepareForReuseSignal] subscribeNext:^(RACUnit * _Nullable x) {
@strongify(self);
self.iconView.image = [UIImage imageNamed:@"template_placehold"];
((YYLabel *)self.detailLab).attributedText = nil;
((YYLabel *)self.detailLab).text = nil;
}];
在YYLabel中是基于CoreText 進(jìn)行繪制的一個框架,所有的元素是繪制在Layer上的。
在清除富文本內(nèi)容,這個是個異步操作,如果cell被復(fù)用,在重新賦值的時候,這里的異步操作會導(dǎo)致在label先顯示了復(fù)用前的text內(nèi)容,然后更新為新的內(nèi)容,所以這里總是會閃一下。
這里又復(fù)習(xí)了一遍YYLabel的源碼,后面再詳細(xì)解釋一遍