Ios5 UITableViewCell背景色基于NSFetchedResultsController-奇怪的问题

Ios5 UITableViewCell背景色基于NSFetchedResultsController-奇怪的问题,ios5,uitableview,nsfetchedresultscontroller,Ios5,Uitableview,Nsfetchedresultscontroller,这简直让我发疯 我有一个UITableView,其中的单元格是通过NSFetchedResultsController填充的,该控件的背景颜色应基于一个核心数据参数进行设置 此表视图位于UISplitViewController的主视图中,所选单元格需要保持可见的选中状态,以指示详细视图中显示的内容 根据其他几个堆栈溢出问题的指导,我了解到配置单元格的理想位置是在willDisplayCell委托调用期间,如下所示: - (void)tableView:(UITableView *)tableV

这简直让我发疯

我有一个UITableView,其中的单元格是通过NSFetchedResultsController填充的,该控件的背景颜色应基于一个核心数据参数进行设置

此表视图位于UISplitViewController的主视图中,所选单元格需要保持可见的选中状态,以指示详细视图中显示的内容

根据其他几个堆栈溢出问题的指导,我了解到配置单元格的理想位置是在willDisplayCell委托调用期间,如下所示:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
WorkTask *workTask = (WorkTask*) [self.fetchedResultsController objectAtIndexPath:indexPath];

if ([workTask.strStatus isEqualToString:@"A"]) {
    cell.backgroundColor = [self colorWithHexString:@"fffdcf"]; 
    // cell.textLabel.backgroundColor = [self colorWithHexString:@"fffdcf"];
    // cell.detailTextLabel.backgroundColor = [self colorWithHexString:@"fffdcf"];

} else if ([workTask.strStatus isEqualToString:@"B"]) {
    cell.backgroundColor = [self colorWithHexString:@"cfffd1"];
    // cell.textLabel.backgroundColor = [self colorWithHexString:@"cfffd1"];
    // cell.detailTextLabel.backgroundColor = [self colorWithHexString:@"cfffd1"];

} else if ([workTask.strStatus isEqualToString:@"C"]) {
    cell.backgroundColor = [self colorWithHexString:@"ffcfcf"];
    // cell.textLabel.backgroundColor = [self colorWithHexString:@"ffcfcf"];
    // cell.detailTextLabel.backgroundColor = [self colorWithHexString:@"ffcfcf"];
} else {
    cell.backgroundColor = [self colorWithHexString:@"ffffff"];
    // cell.backgroundColor = cell.contentView.backgroundColor;
}
这主要是一种工作。但是

取决于我如何使用不同的变体来实现这一点,我最终会在textLabel和detailTextLabel后面忽略背景色(有时是?!?)。或导致选定时单元格显示不正确。或者在没有背景色的情况下显示复选标记指示器。或者将新项目添加到核心数据数据库,并显示在表中,但单元格并没有背景色,但文本标签有背景色

无论我做什么,我都没有找到一种简单直观的方法来让事情按照预期的方式运行——特别是当以编程方式选择单元格时

事实上,细胞选择似乎是问题的根源。所选单元格通常是在我将所选内容更改为另一个单元格后绘制不正确的单元格,尤其是在选定单元格时,如果单元格的颜色发生了更改

有没有什么例子说明这是怎么回事


谢谢

如果我是你,我会创建一个
UITableViewCell
子类,其中包含你自己的
titleLabel/subtitleLabel
uiLabel,并停止使用textLabel/detailTextLabel。在xib文件中,您可以只更改标签和单元格的背景色。当我使用自定义单元格而不是默认单元格时,我从未遇到过您遇到的那种问题

以下是如何从xib加载UITableViewCell的示例:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
    if (cell == nil) {
        // Load the top-level objects from the custom cell XIB.
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
        cell = [topLevelObjects objectAtIndex:0];
    }

    cell.titleLabel.text = @"whatever";
    cell.subtitleLabel.text = @"whatever";

    return cell;
}
您还可以尝试设置单元格contentView的背景色

cell.contentView.backgroundColor = ...;
如果您真的无法理解,那么在UITableViewCell子类中,您可以将自己的UIView放在单元格的背景中,并更改该视图的背景色


willDisplayCell:forRowAtIndexPath:
在调用
InsertRowSatinExpaths:
后可能不会被调用(即,当您使用fetchedresultscontroller将项添加到核心数据中时)。如果绝对必要,您可能应该尝试在
cellForRowAtIndexPath:
willDisplayCell:forRowAtIndexPath:

中设置单元格的背景色及其内容视图,因为某些奇怪的原因,UITableViewCell对象的背景色只能在绘制单元格之前设置。在UITableView的委托中实现此方法:

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

在那里设置单元格的背景色,它将按照您想要的方式绘制。

“在xib文件中,您可以只更改标签和单元格的背景色。”--关键是我正在根据核心数据管理的属性动态更改背景色。返回一个配置好的单元格很容易——让颜色在运行中正确地改变是个问题。有什么想法吗?我仍然认为尝试一个定制的手机值得一试。您仍然可以更改willDisplayCell:中的背景色,也可以更改CellForRowatineXpath:中的事件。我将尝试更改周围的内容以使用自定义单元格,并将在此处报告。同时,悬赏是你的,因为没有其他人给你更好的答案。谢谢您是否已尝试分配
单元格。已选择backgroundView
单元格。backgroundView
?这似乎解决了单元格选择的许多问题。我将覆盖
-setHighlighted:
(在您的单元格子类中)