tableviewcell中的属性字符串在单元出列并重新加载之前不会显示粗体文本

问题描述 投票:6回答:3

我有一个表格视图,其中的单元格带有带有一些属性文本的标签。已从cellForRowAtIndexPath正确设置了文本。文本的颜色已正确设置,但在使单元格退出队列之前,不会显示粗体属性。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        MyCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
        Model *model = [self.fetchedResultsController objectAtIndexPath:indexPath];
        cell.tag = indexPath.row;
        [cell updateContentWithModel:model atIndexPath:indexPath];
        return cell;
}


- (void)updateContentWithModel:(Model *)model atIndexPath:(NSIndexPath *)indexPath
{
self.model = model;
[self setTextFromModel];
[self setImageFromModelAtIndexPath:indexPath];
}

- (void) setTextFromModel
{
self.title.text = self.model.header;
self.status.attributedText = [self boldString:self.model.status fontSize:self.status.font.pointSize color:[UIColor redColor]];
}

+(NSMutableAttributedString *)boldString:(NSString *)stringToBold fontSize:(CGFloat)fontSize color:(UIColor *)color
{
NSMutableAttributedString *boldString = [[NSMutableAttributedString alloc] initWithString:stringToBold];
[boldString setAttributes:@{NSForegroundColorAttributeName: color,
                            NSFontAttributeName:[UIFont boldSystemFontOfSize:fontSize]} range:NSMakeRange(0, boldString.length)];
return boldString;
}

以前有没有人经历过类似的事情?

ios uitableview nsmutableattributedstring
3个回答
6
投票

由于UIAppearance,我有同样的问题。在设置UILabel属性文本对我有用之前,将font \ color设置为nil。


3
投票

我有类似的问题。我试图在自定义UITableViewCell中的UILabel上设置属性文本(特别是字体颜色),即使我在tableView:willDisplayCell:forRowAtIndexPath:中进行了更改,它也只能部分反映更改(我相信这是进行此类更新的推荐位置) 。

原来我需要在主线程上设置属性

dispatch_async(dispatch_get_main_queue(), ^{
            [label setAttributedText:text];
        });

[虽然我已经知道所有UI更新都将在主线程上完成,但我很惊讶地发现,willDisplayCell尚未从主线程中调用,至少在这种情况下没有。


0
投票

对我来说,问题在于我正在使用UILabel的自定义子类,该子类使用UIAppearance来显示颜色。当我切换为使用常规UILabel时,问题已解决,但现在我必须自己处理字体颜色,而不是使用UIAppearance

© www.soinside.com 2019 - 2024. All rights reserved.