- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经尝试了几乎所有我能想到的方法,用谷歌搜索了很多,搜索了 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/
我已经尝试了几乎所有我能想到的方法,用谷歌搜索了很多,搜索了 SO,但仍然找不到解决方案。这是问题所在: 我有一个自定义的 UITableViewCell,里面有几个标签。文本动态变化,背景颜色(单元
我有一个带表格的 View Controller 。第一次创建和呈现单元格时,将按顺序调用以下函数(除了 View Controller 函数): .... func viewWillAppear(_
我有我的数据源 (productsAry),它是通过 SQLite 填充的,我一次获取 25 个项目。我正在调用“willDisplayCell”UITableView 委托(delegate),当
我的目标是定义我自己的单元格样式,包括背景、字体和大小。我试试这个。 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRo
每当我创建一个新单元格时,就会出现奇怪的魔法。如果我稍后退出 View Controller (数据全部保存),然后再次返回 View Controller (重新加载数据等),一切都很好(但如果我留
您好,当用户向下滚动时,如果用户滚动到顶部,则通常会在没有动画的情况下移动,从而为表格 View 单元格设置动画。 -(void)tableView:(UITableView *)tableView
有一个 UITableView 帖子。 看到的帖子 id 保存在 sqlite 我想以橙色显示已看过的帖子,以黑色显示其他帖子。 但是,当我在 willDisplayCell 方法中为看到的帖子设置橙
我有两种方法可以改变单元格的附件类型。第一个在所有单元格加载后调用,另一个在用户单击任何单元格时调用。 第二种方法工作正常,它从选定的单元格中删除复选标记。不幸的是,第一个什么都不做(我想在其中一个单
我正在尝试将动画放在 UITableViewCell 的底部到顶部,该代码可以正常工作: -(void)tableView:(UITableView *)tableView willDisplayCe
我正在尝试在 willDisplayCell 方法中实现两个自定义单元格 customCellView1 和 customCellView2 - 这似乎只支持一个单元格。有没有办法重写它以包括两个?谢
有一个 UITableView 的帖子。 已查看的帖子 ID 保存在 sqlite 中 我想显示,看到的帖子是橙色的,其他的是黑色的。 但是当我在 willDisplayCell 方法中为 seen
在我所有的 UITableView 编程中,我总是在 -[UITableViewDataSource tableView:cellForRowAtIndexPath:] 中配置我的 UITableVi
我想为我的自定义单元格制作自定义角圆角背景,它与下面的代码一起使用,但第一行不受影响,我应该向上滚动它以使其具有背景 func tableView(tableView: UITableView, w
我试图通过加载与他们在传递中单击的单元格相对应的数字列表来预选择表格 View 中的单元格。我从 nsuserdefaults 加载它并使用 willdisplaycell 方法单击单元格。我已经包含
我正在尝试在将显示单元格中对 UITableViewCell 进行初始设置,但它不起作用。谁能帮帮我? -(void) tableView:(UITableView *)tableView willD
我想更改 TableView 中单元格的状态。我尝试在“cellForRowAtIndexPath”方法中设置状态,但它没有用。然后我看到帖子说为了改变单元格的状态需要使用委托(delegate)方法
我对 Obj-C 和 Cocoa 的东西还很陌生,所以请原谅任何愚蠢的行为。 我正在使用带有 NSTreeController 的 NSOutlineView,它向它提供了我自己的对象数组。我的委托(
this page谈到平滑表格 View 滚动的技巧,说表格 View 单元格不应该在 tableView:cellForRowAtIndexPath: 上绘制而是在 tableView:willDi
我从互联网上看到了下面这一段,并按照他的建议将我的代码移到了 willDisplayCell。但是,我没有看到任何性能改进。所以我做了一些进一步的调查,发现还有其他人说这段话是不真实的。 Proper
我偶然发现了一篇博文,其中一位作者解释了如何在滚动整个表格 View 时使用以下方法逐渐扩大单元格的大小: -(void)tableView:(UITableView *)tableView will
我是一名优秀的程序员,十分优秀!