UITableViewCell标签的奇怪背景

时间:2013-11-24 00:03:15

标签: ios

我正在创建一个应用程序,通过设置其单元格的背景颜色来显示运动结果并对其进行颜色编码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Cell"];
    if (_tabBar.selectedItem == _homeGamesItem) {
        cell.backgroundColor = [UIColor whiteColor];
        cell.detailTextLabel.text = @"";
        cell.textLabel.text = [NSString stringWithFormat:@"Home Game %d", indexPath.row];
    } else if (_tabBar.selectedItem == _myEventsItem) {
        cell.backgroundColor = [UIColor whiteColor];
        cell.detailTextLabel.text = @"";
        cell.textLabel.text = [NSString stringWithFormat:@"My Result %d", indexPath.row];
    } else if (_tabBar.selectedItem == _resultsItem) {
        SportEvent *event = [self.appDelegate.user.resultSportEvents objectAtIndex:indexPath.row];
        if ([event.gender isEqualToString:@""]) {
            cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", event.level, event.activity];
            cell.detailTextLabel.text = event.result;
        } else {
            cell.textLabel.text = [NSString stringWithFormat:@"%@ %@ %@", event.gender, event.level, event.activity];
            cell.detailTextLabel.text = event.result;
        }
        if ([[event.result substringToIndex:1] isEqualToString:@"W"])
            cell.backgroundColor = [UIColor colorWithRed:0 green:1 blue:0 alpha:.05];
        else if ([[event.result substringToIndex:1] isEqualToString:@"T"])
            cell.backgroundColor = [UIColor colorWithRed:1 green:1 blue:0 alpha:.05];
        else if ([[event.result substringToIndex:1] isEqualToString:@"L"])
            cell.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:.05];
    }

    if (_grayedOut) {
        cell.textLabel.textColor = [UIColor grayColor];
        cell.detailTextLabel.textColor = [UIColor grayColor];
    } else {
        cell.textLabel.textColor = [UIColor blackColor];
        cell.detailTextLabel.textColor = [UIColor blackColor];
    }

    return cell;
}

奇怪的是,这导致一些细胞在标签周围有奇怪的背景(我为大量图像道歉):

enter image description here

为什么只有一些细胞会在标签周围出现这种奇怪的变暗?

1 个答案:

答案 0 :(得分:0)

由于您在其他选项卡中使用这些单元格,并且只将某些单元格的背景颜色更改为白色(例如homeGames),然后添加以下代码以使所有可重用单元格在再次实际使用之前具有相同的颜色(clearColor):

...
if (cell == nil)
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Cell"];

//Add This Code
cell.backgroundColor = [UIColor clearColor];
//Also make labels same color just to be sure
cell.textLabel.backgroundColor = [UIColor clearColor]; //or whiteColor
cell.detailTextLabel.backgroundColor = [UIColor clearColor]; //or whiteColor

if (_tabBar.selectedItem == _homeGamesItem) {
...