1

假设我TTTableStyledTextItem在符合类中创建对象,<TTTableViewDataSource>如下所示:

NSString* text = [NSString stringWithFormat:@"<b>%@</b>\n%@", @"aaaa..", @"bbbb.."];
TTStyledText* styledText = [TTStyledText textFromXHTML:text lineBreaks:YES URLs:NO];
TTTableStyledTextItem* item = [TTTableStyledTextItem itemWithText:styledText URL:@"my://url"];

默认情况下,由返回的表格视图单元格类tableView:cellClassForObject:将是TTStyledTextTableItemCell.

这可以正常工作,因为我想在单元格处于正常状态时(当它不处于选定状态时)自定义单元格的背景颜色。

TTStyledTextTableItemCell我已经设法通过创建一个子类并覆盖初始化程序来更改处于选定状态的单元格的背景,initWithStyle:reuseIdentifier:如下所示:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)identifier {
    self = [super initWithStyle:style reuseIdentifier:identifier];
    if (self) {
        // WORKS!
        // cell's backgroundview (selected)
        UIView *selectedView          = [[UIView alloc] init];
        selectedView.backgroundColor  = [UIColor someColor..];
        self.selectedBackgroundView   = selectedView;

        // DOESN'T WORK
        // cell's background (normal)
        UIView *normalView          = [[UIView alloc] init];
        normalView.backgroundColor  = [UIColor someColor..];
        self.backgroundView         = normalView;
    }

    return self;
}

但我找不到在未选中时更改单元格背景的方法(self.backgroundView)。我知道类中有一个关联的TTStyledTextLabel子视图TTStyledTextTableItemCell,但我仍然没有成功自定义它。

有没有简单的方法来实现这一点?谢谢

4

0 回答 0