在平面样式表视图上设置UITableViewCell背景颜色? [英] Set UITableViewCell background color on plain style table view?

查看:113
本文介绍了在平面样式表视图上设置UITableViewCell背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用搜索栏创建一个简单的样式表视图,并且在Apple的表视图中复制gutter的外观(当表视图被向下拖动时)时出现问题。

I'm building a plain style table view with a search bar and I'm having trouble replicating the look of the "gutter" (when the table view is dragged down) in Apple's table views.

如果我设置表视图的背景颜色,则看起来很好,但是我的单元格使用该颜色作为背景颜色。

If I set the background color of the table view, the gutter looks fine, but my cells use that color as the background color.

我尝试在 tableView:cellForRowAtIndexPath:中设置单元格和单元格的contentView的背景颜色,但它在以后的点被覆盖。我也尝试创建一个空视图,并将其设置为单元格的backgroundView和作为contentView的子视图,但只有边缘是白色的。

I've tried setting the background color of the cell and of the cell's contentView in tableView:cellForRowAtIndexPath:, but it gets overridden at some later point. I've also tried creating an empty view and setting it as the cell's backgroundView and as a subview of the contentView, but only the edges are colored white.

解决方案我可以想到:


  • 将UITableViewController子类转换为UIViewController,并在表视图后插入一个空白视图。

  • 创建一个不允许覆盖backgroundColor的UITableViewCell子类。

相当多的工作。有更简单的方法吗?

Both of these solutions will require quite a bit of work. Is there an easier way?

UPDATE :第一个解决方案没有工作。我必须将表视图的背景颜色设置为clearColor,以便使gutter更改为视图的颜色。但是,当我这样做,我得到的结果与在表视图上设置backgroundColor相同。

UPDATE: The first solution didn't work. I have to set the table view's background color to clearColor in order for the gutter to change to the view's color. However, when I do this, I get the same result as setting the backgroundColor on the table view.

推荐答案

方法称为 -tableView:willDisplayCell:forRowAtIndexPath:
,可以用于在表视图修改后自定义单元格的背景颜色。从 UITableViewDelegate reference

There is a delegate method called -tableView:willDisplayCell:forRowAtIndexPath: that can be used to customize the background color of the cell after the table view has made it's modifications. From the UITableViewDelegate reference:


表视图将此消息发送到其委托使用单元格来绘制一行,从而允许代理在显示之前自定义单元格对象。此方法为委托提供了覆盖由表视图较早设置的基于状态的属性的机会,例如选择和背景颜色。委托返回后,表视图只设置alpha和frame属性,然后只在动态滑动或滑出行时动画。

A table view sends this message to its delegate just before it uses cell to draw a row, thereby permitting the delegate to customize the cell object before it is displayed. This method gives the delegate a chance to override state-based properties set earlier by the table view, such as selection and background color. After the delegate returns, the table view sets only the alpha and frame properties, and then only when animating rows as they slide in or out.

另外,由于当试图滚动过最后一个单元格时,背景颜色从表格的底部窥探出来,我不得不创建一个页脚视图并更新表视图的内容:

Also, since the background color peeks out from the bottom of the table when trying to scroll past the last cell, I had to create a footer view and update the table view's content inset:

UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
footerView.backgroundColor = [UIColor whiteColor];
self.tableView.tableFooterView = footerView;
[footerView release];

UIEdgeInsets inset = self.tableView.contentInset;
inset.bottom = 44 - footerView.frame.size.height;
self.tableView.contentInset = inset;

这篇关于在平面样式表视图上设置UITableViewCell背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆