为什么UITableViewCell背景颜色不起作用(在界面生成器中设置)?

时间:2011-03-02 11:20:51

标签: ios uitableview cocoa-touch uikit

为什么UITableViewCell背景颜色不起作用(在界面构建器中设置)?

我从一些搜索中注意到,在UITableViewController的自定义子类中设置的以下代码确实有效(见下文):

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.backgroundColor = [UIColor redColor];
}

但我仍然想了解为什么界面构建器具有TableViewCell的背景颜色设置,这实际上似乎不起作用?

14 个答案:

答案 0 :(得分:70)

有点晚了,但我刚遇到这个问题......在contentView上设置背景颜色很好:

cell.contentView.backgroundColor = [UIColor redColor];

答案 1 :(得分:16)

对我来说有用的是创建我自己的UIView对象,将其设置为UITableViewCell的背景视图。在cellForRowAtIndexPath中:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIView* bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
bgview.opaque = YES;
bgview.backgroundColor = [UIColor orangeColor];
[cell setBackgroundView:bgview];

答案 2 :(得分:11)

尝试设置UITableViewCell本身的背景颜色不是你应该这样做的。表格单元格包含五个可以访问的有用视图的集合,其中一个是backgroundView

推荐阅读:

http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html

然后:

http://cocoawithlove.com/2010/12/uitableview-construction-drawing-and.html

答案 3 :(得分:7)

根据Apple文档,您应该始终按tableView:willDisplayCell:forRowAtIndexPath:而不是tableView:cellForRowAtIndexPath:方法为单元格提供更改。

这个作品!!

答案 4 :(得分:3)

[cell.textLabel setBackgroundColor:[UIColor clearColor]];
[cell.contentView setBackgroundColor:[UIColor redColor]];

答案 5 :(得分:2)

在定义单元格颜色之前,请确保backgroundView为空

cell.backgroundView = nil;
cell.backgroundColor = [UIColor redColor];

答案 6 :(得分:1)

这很简单且有效:在界面构建器中,将视图对象添加到相同尺寸的UITableViewCell,并将控件放在该视图中。然后你得到你想要的任何背景颜色。

编辑:这是一个接受的理由,它让我印象深刻:仔细看看当你双击你放入你的笔尖的IB中的UITableViewCell时你得到了什么,开始添加东西:你不要得到一个简单的矩形外观视图,你得到一个非常清楚的椭圆形矩形:内容视图。因此,您所能做的就是在IB中设置UITableViewCell的内容视图,并且设置内容视图的背景颜色属性不会在表中产生颜色更改。正如文档所说,UITableViewCells是一个复杂的野兽,有许多部分,在UITableView调整它和摆弄它的背后有很多部分。

答案 7 :(得分:0)

它完美无缺,如果您选择tableview并将背景设置为例如红色,则整个表格将为红色。 也许你没有链接表格或其他东西

希望它有所帮助

答案 8 :(得分:0)

(试试这个)

Apple的IB设计是通用的,可应用于多种组件,也面向高技术受众(开发人员)。因此,尚未设计(作为应用程序)为每个组件/情况类型完全自定义接口。因此,在这种情况下,看似设置背景颜色的能力有点误导。

答案 9 :(得分:0)

不是,请使用setBackgroundColor

self.table.separatorColor=[UIColor whiteColor];
[table setBackgroundColor:[UIColor colorWithRed:.8 green:.8 blue:1 alpha:1]];

答案 10 :(得分:0)

您可以在Interface Builder中部分执行此操作(可能最近添加到Xcode中):

  • 展开Storyboard的文档大纲中的单元格,然后选择“内容视图”
  • 在“属性”检查器中设置“内容视图”背景颜色
  • 选择单元格中的每个内容项并设置其背景颜色 到ClearColor

仍然没有找到设置配件背景的方法。

答案 11 :(得分:0)

我确实也遇到了这个问题。 在IB中,您可以在选择单元格时更改单元格的背景颜色。这是不正确的。您应该转到故事板左侧的文档大纲,然后选择单元格内的内容视图。更改内容视图的背景。你现在应该得到正确的细胞颜色。

答案 12 :(得分:0)

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
UIView *view = [[UIView alloc] init];
[view setBackgroundColor:[UIColor redColor]];
[cell setSelectedBackgroundView:view];}

我们需要改变这种方法的颜色。

答案 13 :(得分:0)

试试这个对我有用:

you can use like this 


<ul>
           <li ng-repeat="c in countries">
               <label>
                   <input type="checkbox" ng-model="CountryName" />
                   {{c.CountryName}}
               </label>
               <ul ng-repeat="s in cities" ng-if="c.CountryId==s.CountryId">
                   <li>
                       <label>
                            <input type="checkbox" ng-model="CityName" />
                          {{s.CityName}}
                       </label>
                     </li>
               </ul>
           </li>
       </ul>


 refere below url http://www.codeproject.com/Tips/987499/Nested-Ng-Repeat-on-AngularJS