UITableViewCell displaying incorrect alpha based background colors
I have a UITableViewCell with the UITableViewStyleGrouped style and I would like to change the background color of the cell.
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)ip {
// Do cell creation stuff ...
cell.backgroundColor =
[UIColor colorWithRed:255.0/255.0 green:243.0/255.0 blue:175.0/255.0 alpha:0.50];
}
The trouble is, this doesn't display properly on a grid with the UITableViewStyleGrouped
; I use the same color on a UITableViewStylePlain
and it displays correctly. I开发者_开发百科'm developing for OS 3.0 and have read the multiple posts on setting background color. I can set the color it just doesn't set properly! What am I missing?
You must be doing something in your cell creation/reuse logic to change the default behavior. Starting a project from scratch and implementing this code works for me:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
cell.backgroundColor = [UIColor colorWithRed:1.0 green:0 blue:0 alpha:1.0];
return cell;
}
Double check the documentation if you need something more complex.
Also, could the color shift be coming because of the different table background colors in the two different styles? A UITableViewStylePlain tableView has a default white background. A UITableViewStyleGrouped tableView will have a gray background. Since your are setting the alpha to 0.5, it will overlay onto two different color backgrounds and give you a color shift.
I'm sure this method is not supported, but it does work. Close xcode, open the .xib or .storyboard file in a text editor such as vi. Find the XML for your table and change the cell colour. For example, here is the original section for a default white table cell:
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" indicatorStyle="black" dataMode="prototypes" style="plain" rowHeight="44" sectionHeaderHeight="22" sectionFooterHeight="22" id="k63-au-YAF">
<rect key="frame" x="0.0" y="0.0" width="320" height="480"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<prototypes>
Find and change the color tag. Here is an example that includes the colours from the original post:
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" indicatorStyle="black" dataMode="prototypes" style="plain" rowHeight="44" sectionHeaderHeight="22" sectionFooterHeight="22" id="k63-au-YAF">
<rect key="frame" x="0.0" y="0.0" width="320" height="480"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" red="1.0" green="0.95294117647059" blue="0.68627450980392" alpha="0.5" colorSpace="calibratedRGB"/>
<prototypes>
Open xcode again and your cell colour has been updated.
Note: For the color codes, 243/255 = 0.95294117647059 (Green), 175/255 = 0.68627450980392 (Red), etc.
精彩评论