First, a bit of information how table works:
UITableView has a background. This is what you see behind your cells. - Every cell has three special views:
contentView, backgroundView, selectedBackgroundView. - The
backgroundView is displayed under the contents of the cell, when the cell is selected, selectedBackgroundView is used instead. - Normally all your contents should go to the
contentsView. The separation of background from content enables the table to animate the not-selected/selected transition propertly. It is also important when the cell enters editing mode - the contents are shrinked/moved and the editing controls (e.g. delete button or cell selection) can be displayed independently on contents. - The cell also directly contains separator view (usually 1 or 2px high view at the cell bottom, depending on
separatorStyle). That's why the cell is always at least 1 px higher that its contentsView.
Second, a bit of information how grouped table works.
- The table has a special default background. You can remove/change it using
backgroundView and backgroundColor. - The cells in grouped table views have smaller
contentView. The cell still has the same width as the whole table, but the contentView has an offset on the left and on the right (around 10 points on iPhone). - The
backgroundView is modified and includes a layer drawing the borders, depending on cell position (different for first, last and middle cells). The border has the color specified by table's separatorColor.
Everything can be modified in your code!
One of the simplest methods how to remove the borders is setting separatorColor to [UIColor clearColor]. This will remove the cell separators, but you can add your own separator, e.g
cell = ... UIView* separator = [[UIView alloc] init]; separator.backgroundColor = ... separator.frame = CGRectMake(0.0f, table.bounds.size.width, table.rowHeight - 1.0f, 1.0f); separator.autoresizingMask = (UIViewAutoresizingMaskFlexibleTopMargin | UIViewAutoresizingMaskFlexibleWidth); [cell addSubview:separator]; //note we are adding it directly to the cell, not to contentsView
You can also use an image (UIImageView) for the separator, instead of a single-color view.
Another method is setting the backgroundView to nil for each cell.
Actually, you can completely ignore the contentsView and add everything to the cell directly. You can then customize the following cell methods to update your cell when its state changes.
- (void)setSelected:(BOOL)selected animated:(BOOL)animated { //update the cell text colors, backgrounds etc for the selected/not selected state //you don't have to call super // (the default implementation changes between backgroundView and selectedBackgroundView) } - (void)setHighlighted:(BOOL)highlited animated:(BOOL)animated { //update the cell text colors, backgrounds etc for the selected/not highlighted state //you don't have to call super // (the default implementation changes between backgroundView and selectedBackgroundView) //by giving empty implementation, you will block highlighting of cells } - (void)setEditing:(BOOL)highlited animated:(BOOL)animated { //you can use this method to add custom editing controls } - (void)willTransitionToState:(UITableViewCellStateMask)state { //use this method to change the layout of your contents //when the cells goes into one of the editing states } - (void)layoutSubviews { //update the frames of cell contents to match the new cell size //note that the cell doesn't have the correct size until it is added to the table, //this method is called when the cell already has the final size //you can also use this method to change the size/position of the `contentsView` }
Edit: To address your specific problems, the best solution is probably:
- Remove the default
backgroundView from the table and suplement your own background (e.g. clear color, white color, color created from pattern or a UIImageView to backgroundView. - Remove the default
backgroundView from every cell and replace it by an UIImageView. You need three special images for first, last and middle cells.