0

我在每一行都有一个很大的自定义UITableViewUILabels我想以黑色或绿色显示某些文本。

NSString's我用from a喂细胞NSArray。假设我只想以黑色显示NSStringfrom index 30

我正在尝试这样的事情,但它不起作用:

NSIndexPath *indexPathWithBlackText = [NSIndexPath indexPathForRow:30 inSection:[indexPath section]];

    if (indexPath.row == indexPathWithBlackText.row) {  
        //Label with text in black 
        topLabel.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0];
        topLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
    } else {
        //Label with text in green 
        topLabel.textColor = [UIColor colorWithRed:0.122 green:0.467 blue:0.255 alpha:1.00];
    }

任何有关正确方向的提示将不胜感激。谢谢!

4

3 回答 3

1

在这个 UITableViewDataSource 委托方法中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

写这个:

if ([self isBlackRow:indexPath.row]) {
    // Your label reference, change color to black here.
} else {
    // Your label reference, change color to green here. 
}

制作一种方法来确定该行是否为黑色

- (BOOL)isBlackRow:(NSInteger)row {
    NSArray* blackRows = [NSArray arrayWithObjects:[NSNumber numberWithInt:30], [NSNumber numberWithInt:11], nil];

    for (NSNumber* number in blackRows) {
        if (number.intValue == row) {
            return YES;
        }
    }

    return NO;
}
于 2012-06-26T18:39:14.037 回答
1
 if (indexPath.row > 29) {  
    //Label with text in black 
    topLabel.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0];
    topLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
} else {
    //Label with text in green 
    topLabel.textColor = [UIColor colorWithRed:0.122 green:0.467 blue:0.255 alpha:1.00];
}
于 2012-06-26T18:44:03.747 回答
0

您如何将标签添加到表格视图中?

可能是您没有引用正确的对象。

于 2012-06-26T18:39:03.157 回答