如果声明不适用于表格视图单元格

时间:2013-10-14 09:33:28

标签: ios objective-c uitableview

我正在创建使用Core DataUITableViewControllers来显示MELs列表的应用。

我已经完成了所有事情,但我无法相处,检查UITableViewCell应该是否可编辑。 这是我的应用程序的屏幕截图,它可以帮助您想象我的问题:

enter image description here

我正在检查chapter是否有任何部分。如果这是真的,它将以黑色显示所有内容,如果不是detailTextLabel颜色更改为红色。但是正如你所看到的,即使它们有一些部分,一些细胞也是有色的。怎么可能?

这是我的tableView:cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Initializing Cell and filling it with info
    Chapter *chapter = [self.MELs objectAtIndex:indexPath.row];

    cell.detailTextLabel.text = [NSString stringWithFormat:@"Number: %@ \t Sections: %lu", chapter.number, (unsigned long)[chapter.sections count]];
    cell.textLabel.text = [chapter.title capitalizedString];

    if ([chapter.sections count] == 0) {
        [cell.detailTextLabel setTextColor:[UIColor redColor]];
    }

    return cell;
}

3 个答案:

答案 0 :(得分:3)

当重复使用单元格时,如果不满足条件,则必须将textcolor重置为默认值:

if ([chapter.sections count] == 0) {
    [cell.detailTextLabel setTextColor:[UIColor redColor]];
} else {
    [cell.detailTextLabel setTextColor:[UIColor blackColor]];
}

答案 1 :(得分:2)

这是因为您使用的是[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

[cell.detailTextLabel setTextColor:[UIColor redColor]];保存在可重复使用的单元格中。如果为“红色”单元格添加额外的CellIdentifier,问题就解决了。

此外。您应检查dequeueReusableCellWithIdentifier:forIndexPath的检索到的单元格是否返回nil;

示例:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Initializing Cell and filling it with info
    Chapter *chapter = [self.MELs objectAtIndex:indexPath.row];

    NSString *CellIdentifier = @"Cell";
    if ([chapter.sections count] == 0) {
        CellIdentifier = @"Cell-red";
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (cell == nil) {
            // Cell properties
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

            if ([CellIdentifier isEqual:@"Cell-red"]) {
                [cell.detailTextLabel setTextColor:[UIColor redColor]];
            }
    }

    cell.detailTextLabel.text = [NSString stringWithFormat:@"Number: %@ \t Sections: %lu", chapter.number, (unsigned long)[chapter.sections count]];
    cell.textLabel.text = [chapter.title capitalizedString];

    return cell;
}

答案 2 :(得分:2)

试试这个: -

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.row % 2)// Change this according to your requirement
    {
        [cell.detailTextLabel setTextColor:[UIColor redColor]];
    }
    else
    {
        [cell.detailTextLabel setTextColor:[UIColor blackColor]];
    }
}