I have a UItableView (grouped style). I wanted to cus开发者_StackOverflow社区tomize it so that it appears like a "Stick it" or a "Post it" with a yellow background interface.
Hence is added the following code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UIView *backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
backgroundView.backgroundColor = [UIColor yellowColor];
cell.backgroundView = backgroundView;
cell.textLabel.backgroundColor=[UIColor yellowColor];
}
This actually worked. I was able to achieve the yellow background, but the separator line is missing. Can some1 help me out ?
Is this the best way to do it? Or should i create an image for the cell and use it as the backgroundView?
Actually, none of the above answers are (I believe) correct. According to the doco:
Note: If you want to change the background color of a cell (by setting the background color of a cell via the backgroundColor property declared by UIView) you must do it in the
tableView:willDisplayCell:forRowAtIndexPath:
method of the delegate and not intableView:cellForRowAtIndexPath:
of the data source. Changes to the background colors of cells in a group-style table view has an effect in iOS 3.0 that is different than previous versions of the operating system. It now affects the area inside the rounded rectangle instead of the area outside of it.
So rather than call setBackgroundColor
in tableView:cellForRowAtIndexPath:
, you should be doing so in tableView:willDisplayCell:forRowAtIndexPath:
:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
ShoppingList *object = (ShoppingList*)[self.fetchedResultsController objectAtIndexPath:indexPath];
if ([object isComplete] == YES) {
[cell setBackgroundColor:[UIColor greenColor]];
} else {
[cell setBackgroundColor:[UIColor clearColor]];
}
}
This I've found works perfectly, and fits in with the way the rest of the table cell rendering works. There's no need for any background views to be added or anything.
I'm actually surprised that worked to set the background color. The proper method to setting a UITableViewCell background is:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor yellowColor];
}
Also, I notice you don't have any reuse code in your cellForRowAtIndexPath method. I'm hoping you just didn't paste this in, but that's kind of a big no-no. Table View Programming Guide
Outdated.
Why are you creating a custom background view? If you only want to achieve a yellow color you can customize your color by using UIColor colorWithRed: green: blue: alpha:
to create your own color and just add it to cell setBackgroundColor:UIColor
. It seems like the seperator is being completely overwritten if you add a background view in grouped table view. If you still want to have a custom UIView as your background, I suggest adding a custom tableViewCell that looks like your seperator. You can regulate this in cellForRowAtIndexPath
.
Working code for my first suggestion:
[cell setBackgroundColor:[UIColor yellowColor]];
Does this work for you to put inside "cellForRowAtIndexPath"?
....................
if (cell == nil) {
....................
cell.backgroundColor = [UIColor yellowColor];
....................
tableView.backgroundColor=[UIColor clearColor];
}
Set the separatorStyle of the tableview to UITableViewCellSeparatorStyleSingleLine.
Should be something like below.
myTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
精彩评论