动态配置UITableViewCell文本

时间:2011-08-10 16:47:04

标签: iphone uitableview uilabel

这是我的问题:我有UITableView,我想用不同的文本字体动态配置“THE Cell”文本,例如第一行将是12,Helvetica,Bold和第二行将是10,Helvetica。另请注意,行数未知,需要动态确定。任何帮助表示赞赏!

ps:显然问题不是很清楚。我并不是要在不同的单元格中显示每一行。因此,只考虑一个单元格并为此特定单元格配置文本。我有一个动态确定的单元格行数,因此可能是2或3,具体取决于信息的可用性。我希望这些线条具有不同的字体和不同的颜色。一种方法是为单元格提供动态数量的UILabel,但我想看看是否还有其他选项?

2 个答案:

答案 0 :(得分:3)

如果没有示例模式,我无法真正回答这个问题,但在这里:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath中添加您的自定义。以下是一些例子:

1)

if (indexPath = 1) {  //row 1
    cell.detailTextLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:12]; //Important note: -Bold can vary per font. For example, Arial's bold variant is 'Arial-BoldMT'.
    //change to needs
}
else if (indexPath = 2) { //row 2
    //etc.

2)

if (indexPath <= 6) {  //row 1-6
    cell.detailTextLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:12]; //Important note: -Bold can vary per font. For example, Arial's bold variant is 'Arial-BoldMT'.
    //change to needs
}
else if (indexPath >=7 && indexPath <=15) { //rows 7-15
    //etc.

3)

///etc.

else if (indexPath >=84) { //rows 84 and over
    cell.detailTextLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:12];
}

除非您有特定的模式,否则如果您不知道有多少行可能会很难。如果您需要更多帮助,请在下面发表评论。

答案 1 :(得分:0)

这是相当微不足道的。只需在

中设置单元格的内容即可
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

您可以根据单元格的indexPath调整字体大小,颜色等。

如果您想要了解所有可能的大量示例,请查看Apple的Table View Programming Guide

相关问题