gpt4 book ai didi

ios - willDisplayCell 不会改变符合逻辑的背景颜色

转载 作者:行者123 更新时间:2023-11-29 10:42:45 25 4
gpt4 key购买 nike

我已经尝试了几乎所有我能想到的方法,用谷歌搜索了很多,搜索了 SO,但仍然找不到解决方案。这是问题所在:

我有一个自定义的 UITableViewCell,里面有几个标签。文本动态变化,背景颜色(单元格的,而不是标签的)也应该变化。标签中的文本更改工作正常。但是,无论我做什么,BG 颜色都不会改变。唯一的好处是我不觉得那么孤独。即使对一些高级代表来说,这显然也是一个谜。我希望这里有人找到了解决方案(或者可以指出我的错误)。

这是我所做的:

将逻辑放在 cellForRowAtIndexPath 中。失望。

用 Google 搜索并搜索更多内容,然后:

将逻辑放在 willDisplayCell 调用中。什么都没有。

Google 了更多,然后:

将逻辑放回 cellForRowAtIndexPath。纳达。

再次将逻辑放入 willDisplayCell 调用中。不行。

在SO上找到一个帖子,建议在自定义单元格中放置另一个 View 以覆盖原始背景并设置它随逻辑变化。它没有。

尝试将逻辑放回 cellForRowAtIndexPath

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

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

还有一些我想不起来了。还是不行。

是的,UItableViewdelegate 已设置。

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

编辑:对顶部的 if 语句进行了轻微更改,以反射(reflect)下面@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];
}

非常感谢您抽出时间来看!感谢所有帮助,即使这是我的愚蠢错误。

最佳答案

我的印象是 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;

谢谢阿德里安_H

关于ios - willDisplayCell 不会改变符合逻辑的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23707087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com