willDisplayCell不会改变与逻辑一致的背景颜色

时间:2014-05-17 02:47:17

标签: ios objective-c background uitableview

我已经尝试过几乎所有我能想到的东西,谷歌搜索,搜索SO,仍然无法找到解决方案。问题在于:

我有一个自定义UITableViewCell,其中包含多个标签。文本动态变化,背景颜色(单元格,而不是标签)也应该有所不同。标签中的文本更改工作正常。然而,无论我做什么,BG颜色都不会改变。唯一的好处是我不会感到孤独。对于一些高级别的人来说,这显然是一个谜。我希望有人找到解决方案(或者可以指出我的错误)。

这就是我所做的:

将逻辑放在cellForRowAtIndexPath中。失望。

用谷歌搜索了一下,然后再说:

将逻辑置于willDisplayCell电话中。什么都没有。

用Google搜索,然后:

将逻辑放回cellForRowAtIndexPath。纳达。

再次将逻辑放入willDisplayCell来电。不行。

在SO上找到一条帖子,建议在自定义单元格中放置另一个视图以覆盖原始背景并将其设置为随逻辑更改。它没有。

尝试将逻辑重新放回cellForRowAtIndexPath

尝试在逻辑中使用switch语句。

在逻辑中使用if, else if, else尝试。

我无法记住的其他几件事。仍然无法工作。

是的,设置了UItableViewdelegate

这是当前的代码,它也不起作用:

编辑:在顶部if语句中稍作修改以反映@Adrian下面的建议。不幸的是,它并没有解决问题。

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    id<NSFetchedResultsSectionInfo> sectionInfo = [[detailFRC sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
    NSLog(@"There are %lu objects in the frc",(unsigned long)[sectionInfo numberOfObjects]);
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell * cell = [tableView dequeueReusableCellWithIdentifier:@"myCustomCell"];
    if (!cell)
    {
        [tableView registerNib:[UINib nibWithNibName:@"CustomCell" bundle:nil] forCellReuseIdentifier:@"myCustomCell"];
        cell = [tableView dequeueReusableCellWithIdentifier:@"myCustomCell"];
    }


    return cell;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(CustomCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    thisActivity = [detailFRC objectAtIndexPath:indexPath];

    if (self.activityOrCategory == 0)
    {

            if (thisActivity.name == self.detailFocusItem)
            {
            [cell.myBGView setBackgroundColor:Rgb2UIColor(255, 215, 215)]; // Light red
            cell.backgroundView = cell.myBGView;
            }
            else if (thisActivity.name == self.detailBenchmarkItem)
            {
                [cell.myBGView setBackgroundColor:Rgb2UIColor(215, 220, 255)]; // Light blue
            }
            else
            {
                cell.myBGView.backgroundColor = [UIColor whiteColor];
            }
    }

    else if (self.activityOrCategory == 1)

    {
            if (thisActivity.category == self.detailFocusItem)
            {
                cell.myBGView.backgroundColor = Rgb2UIColor(255, 235, 200);
            }
            else if (thisActivity.category == self.detailBenchmarkItem)
            {
                cell.myBGView.backgroundColor = Rgb2UIColor(200, 255, 200);
            }
            else
            {
                cell.myBGView.backgroundColor = [UIColor whiteColor];
            }
    }

    NSLog(@"cell.backgroundColor is %@",cell.backgroundColor);
    NSLog(@"This row says %@",thisActivity.name);
    cell.activityLabel.text = thisActivity.name;
    cell.categoryLabel.text = thisActivity.category;

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
    [dateFormat setDateFormat: @"MM/dd/yyyy hh:mm:ss"];

    cell.fromDateLabel.text = [dateFormat stringFromDate:thisActivity.startTime];
    cell.toDateLabel.text = [dateFormat stringFromDate:thisActivity.stopTime];
    }

非常感谢您花时间去看!所有的帮助都表示赞赏,即使这是我的愚蠢错误。

3 个答案:

答案 0 :(得分:1)

我的印象是UITableViewCell具有backgroundView的实际属性,我可以看到你有一些名为BGView的东西,但是我看不到你的示例中的单元属性设置。

所以我可以说的是,对于我完成此操作的示例,我总是创建一个UIView并设置它的颜色,然后将其分配给backgroundView属性。

我希望这可能会有所帮助,而且我在前面的例子中并没有想到你这样做!

//   Effectively draws the cell with a red background
UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 44)];
backView.backgroundColor = [UIColor redColor];
cell.backgroundView = backView;

由于 Adrian_H

答案 1 :(得分:1)

我之前从未使用过Rgb2UIColor,我不知道它是如何工作的。

尝试以下代码,它适用于我的应用: -

willDisplayCell

CGFloat nRed=255/255.f;
CGFloat nGreen= 215/255.f;
CGFloat nBlue = 215/255.f;
UIColor * myColor = [UIColor colorWithRed:nRed green:nGreen blue:nBlue alpha:1]; //Light Red

cell.textLabel.backgroundColor = myColor;
cell.detailTextLabel.backgroundColor = myColor;
cell.backgroundColor =myColor;
[cell.backgroundView setBackgroundColor:myColor];

答案 2 :(得分:0)

我希望那些看过的人,尤其是那些花时间回答这个问题的人,可以对此有一种幽默感:

事实证明,上面慷慨贡献的两个答案都可行(有少量的mod)。我觉得有点像假人,但我遇到的问题不在于背景颜色的设置,而在于逻辑本身。也就是说,我在if语句中使用了这段代码:

 if (thisActivity.name == self.detailFocusItem)

当由于这两个对象都是NSStrings时,它应该是:

 if ([thisActivity.name isEqualToString:self.detailFocusItem])

因此,控制只是跳过颜色设置代码。 :/

我真的为花时间而道歉。

OTOH,如果不是因为我实施你的建议而被迫重新审视它,我不知道它可能需要多长时间才能看到森林而不是树木。

你们两个都得到了支持,我希望你们能够温柔,并将其归结为一个不高兴的爱好者的经验。

非常感谢您的帮助!