Can't Change Transparent BG in Custom UITableViewCell (Grouped Table View)
There are a number of posts regarding various issues with the backgrounds of cells (custom or standard) in UITableViews (grouped and plain), but I can find no mention of this specific issue.
Basically I have a UITableView with Grouped style.
The Problem: I can't get my custom cells to have anything other than a transparent background.
Here's what my tableview looks like before I implement my custom cells (i.e. this is just a normal grouped table view):
Standard, normal grouped Tableview. Great. But I want custom cells.
So I built a custom cell that looks like this:
With this hierarchy:
And this background metadata for the cell:
Unfortunately no matter what I do, whether changing background to white, clear, or even to black, nothing makes a difference, my table view still comes out looking like this:
(that's the table view background, fwiw. i.e. these cells are transparent now).
And, of course, I can't solve this by changing the background of the View I added to the cell, because then everything just comes out looking like this (notice the absence of rounded corners, etc):
Which is obvi开发者_开发百科ously ugly and unacceptable.
How do I get the cells to have a normal white background like the standard default cells?? The bizarre thing is that for an app last week I made a custom cell following exactly the same procedure and those backgrounds came out white just fine.
What have I missed here? All I need is for my custom cells to have a white background that conforms to the curved top and bottom corners of a grouped table view. I have seen the white background in other implementations, so I know it's possible with a custom cell. But something isn't working here!
I'm happy to post whatever code, just not sure what would be needed/wanted. Here's my cellForRowAtIndexPath method (this is just a core data version of tableView:cellForRowAtIndexPath:):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForManagedObject:(NSManagedObject *)managedObject
{
static NSString *ReuseIdentifier = @"customSummaryCell";
CustomSummaryTableViewCell *cell = (CustomSummaryTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ReuseIdentifier];
if (cell == nil) {
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomSummaryTableViewCell" owner:nil options:nil];
for (id currentObject in nibObjects) {
if([currentObject isKindOfClass:[CustomSummaryTableViewCell class]])
{
cell = (CustomSummaryTableViewCell *)currentObject;
}
}
}
cell.leftLabel.text = @"Some data";
cell.rightLabel.text = @"Some Info";
return cell;
}
My class files should be basically unchanged, but here they are. The .h file for my custom cell class:
import
@interface CustomSummaryTableViewCell : UITableViewCell {
IBOutlet UIView *backgroundView;
IBOutlet UILabel *leftLabel;
IBOutlet UILabel *rightLabel;
}
@property (nonatomic, retain) IBOutlet UIView *backgroundView;
@property (nonatomic, retain) IBOutlet UILabel *leftLabel;
@property (nonatomic, retain) IBOutlet UILabel *rightLabel;
@end
And the .m file:
import "CustomSummaryTableViewCell.h"
@implementation CustomSummaryTableViewCell
@synthesize backgroundView;
@synthesize leftLabel;
@synthesize rightLabel;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)dealloc
{
[leftLabel release];
[rightLabel release];
[backgroundView release];
[super dealloc];
}
@end
Note that I have changed the class of the cell in the xib to CustomSummaryTableViewCell
Any help would be much appreciated here, not sure what the problem is and this is driving me nuts!!
Thanks in advance.
I found that adding subviews to cell is easier than implementing custom cells. In your case, with two labels in one cell, that looks easy enough to do.
EDIT - Simplified example
-(UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath
{
. . .
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, labelWidth, labelHeight)];
[label2 setText:@"Something"];
[[cell contentView] addSubview:label2];
return cell;
}
EDIT - Here's a real, full example:
-(UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath
{
static NSString *reusableCell = @"reusableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusableCell];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusableCell] autorelease];
thumbnail = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 106, 81)];
feedTitle = [[UILabel alloc] initWithFrame:CGRectMake(116, 3, [IOSDevice screenWidth] - 140, 25)];
postDate = [[UILabel alloc] initWithFrame:CGRectMake(116, 10, [IOSDevice screenWidth] - 140, 50)];
description = [[UILabel alloc] initWithFrame:CGRectMake(116, 45, [IOSDevice screenWidth] - 140, 50)];
// setting tag reminders so we can identify the element to replace
[thumbnail setTag:1];
[feedTitle setTag:2];
[postDate setTag:3];
[description setTag:4];
[[cell contentView] addSubview:thumbnail];
[[cell contentView] addSubview:feedTitle];
[[cell contentView] addSubview:description];
[[cell contentView] addSubview:postDate];
[thumbnail release];
[feedTitle release];
[description release];
[postDate release];
}
thumbnail = (UIImageView *)[[cell contentView] viewWithTag:1];
feedTitle = (UILabel *)[[cell contentView] viewWithTag:2];
postDate = (UILabel *)[[cell contentView] viewWithTag:3];
description = (UILabel *)[[cell contentView] viewWithTag:4];
[feedTitle setBackgroundColor:[UIColor clearColor]];
[feedTitle setFont:[UIFont fontWithName:@"Helvetica-Bold" size:16]];
[feedTitle setTextColor:[UIColor colorWithRed:0.215 green:0.215 blue:0.215 alpha:1.0]];
[description setBackgroundColor:[UIColor clearColor]];
[description setFont:[UIFont fontWithName:@"Helvetica" size:12]];
[description setTextColor:[UIColor colorWithRed:0.328 green:0.328 blue:0.328 alpha:1.0]];
[description setNumberOfLines:2];
[description setLineBreakMode:UILineBreakModeWordWrap];
[postDate setBackgroundColor:[UIColor clearColor]];
[postDate setFont:[UIFont fontWithName:@"Helvetica" size:12]];
[postDate setTextColor:[UIColor colorWithRed:0.707 green:0.180 blue:0.141 alpha:1.0]];
[thumbnail setImage:[[items objectAtIndex:[indexPath row]] objectForKey:@"thumb"]];
[feedTitle setText:[[items objectAtIndex:[indexPath row]] objectForKey:@"title"]];
[description setText:[[items objectAtIndex:[indexPath row]] objectForKey:@"summary"]];
// Format date
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[postDate setText:[dateFormatter stringFromDate:[[items objectAtIndex:[indexPath row]] objectForKey:@"date"]]];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
if([feedList contentOffset].y < -50)
{
shouldUpdate = TRUE;
[activityIndicator stopAnimating];
[feedList setContentOffset:CGPointMake(0, -30) animated:NO];
[self loadData];
loadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, -25, [IOSDevice screenWidth], 20)];
[loadingLabel setText:@"Loading New Data"];
[loadingLabel setTextAlignment:UITextAlignmentCenter];
[loadingLabel setBackgroundColor:[UIColor clearColor]];
[loadingLabel setTextColor:[UIColor colorWithRed:0.215 green:0.215 blue:0.215 alpha:1.0]];
[loadingLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:14]];
reloadingSpinner = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(70, -25, 20, 20)];
[reloadingSpinner setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
[reloadingSpinner startAnimating];
[reloadingSpinner setHidesWhenStopped:YES];
[feedList addSubview:reloadingSpinner];
[feedList addSubview:loadingLabel];
}
return cell;
}
Check the Alpha and Background of your UITableViewCell view in nib file.
The Alpha should be 1.0 .
精彩评论