简体   繁体   中英

Set UITableViewCell background color on plain style table view?

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.

Contacts screenshot http://img200.imageshack.us/img200/2633/picture5m.png

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

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.

There are two solutions I can think of:

  • Convert my UITableViewController subclass to a UIViewController and insert a blank view behind the table view.
  • Create a UITableViewCell subclass that doesn't allow the backgroundColor to be overridden.

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

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.

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 :

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;
UIView *myView = [[UIView alloc] initWithFrame:cell.frame];
myView.backgroundColor = [UIColor colorWithRed:214.00/255.00 green:233.00/255.00 blue:247.00/255.00 alpha:1.0];
cell.backgroundView = myView;
[myView release];

Generally I have been using multiple view controllers when I display table views. I use a UIViewController subclass to controller the whole view and any bars or buttons (anything not table view related). Then I create a UITableViewController to control only the table.

This has several effects:

First, it allows me to encapsulate behavior much better. The UITableViewController is much cleaner and portable.

Second, you get a real UIView to deal with issues like the problem you are having. It seems like more work, but allows greater customization. You can do whatever you want for the background.

Third, It allows me to use a XIB for the main view much easier, without worrying about the UITableView content (which may be network loaded or not, or have other loading issues).

So my suggestion, would be your first solution. But you should do this all the time, not just when you think you will need the additional flexibility. That way it is already in place.

只需将您的Tableview和搜索栏放在UIView内(使UITableViewController成为UIViewController),然后将视图背景设置为所需的IB或代码颜色。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM