First, some information on how the table works:
UITableView has a background. This is what you see behind your cells.- Each cell has three special views:
contentView , backgroundView , selectedBackgroundView . backgroundView displayed below the contents of the cell when the cell is selected, selectedBackgroundView used instead.- Usually your content should be in
contentsView . Separating the background from the content allows the table to correctly animate the non-selected / selected transition. It is also important when a cell goes into edit mode - the contents are compressed / moved, and the editing controls (for example, the delete button or cell selection) can be displayed regardless of the content. - The cell also directly contains the view of the separator (usually 1 or 2px tall view at the bottom of the cell, depending on
separatorStyle ). Therefore, a cell is always at least 1 px higher than its contentsView .
Secondly, some information on how a grouped table works.
- The table has a special default background. You can remove / change it with
backgroundView and backgroundColor . - Cells in grouped table views have a smaller
contentView . The cell still has the same width as the whole table, but the contentView has an offset on the left and right (about 10 points on the iPhone). backgroundView changed and includes a layer that draws borders, depending on the position of the cell (different for the first, last and middle cells). The border has the color specified in the separatorColor table.
Everything can be changed in your code!
One of the easiest ways to remove borders is to set separatorColor to [UIColor clearColor] . This will remove the cell separators, but you can add your own separator, for example
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];
You can also use an image ( UIImageView ) as a separator instead of a monochrome representation.
Another method is to set the backgroundView to nil for each cell.
Actually, you can completely ignore contentsView and directly add everything to the cell. You can then configure the following cell methods to update your cell when its state changes.
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
Edit: To solve your specific problems, the best solution is probably the following:
- Remove the default
backgroundView from the table and add your own background (e.g. pure color, white, the color created from the template, or from UIImageView to backgroundView . - Remove the default
backgroundView from each cell and replace it with a UIImageView . You need three special images for the first, last and middle cells.
source share