How to customize the background/border colors of a grouped table view cell?
阅读本文后,我尝试使用此解决方案。这是我的代码,在tableViewDelegate方法中:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCellBackgroundView *customBackground = [CustomCellBackgroundView alloc];
[customBackground setBorderColor:[UIColor blackColor]];
[customBackground setFillColor:[UIColor redColor]];
[customBackground setPosition:0]; //i'll deal with that later
[cell setSelectedBackgroundView:customBackground];
[customBackground release];
UIImageView* selectedBackgroundCell = [[[UIImageView alloc] initWithFrame:CGRectNull] autorelease];
[selectedBackgroundCell setImage:[UIImage imageNamed:@"cell_bg_50_hover.png"]];
[customBackground drawRect:selectedBackgroundCell.frame];
[cell setSelectedBackgroundView:selectedBackgroundCell];
//standard background color
[cell setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"cell_bg_50.png"]]];
}
但不幸的是,它没有改变任何东西。你知道我做错了吗?
答案 0 :(得分:1)
我相信每当制作视图时,你应该为此指定一些框架。 因此,请为自定义背景和背景指定框架CGRectZero ....
在我测试时,只为我工作。
UIView *bkview = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
bkview.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = bkview;
UIView *bkview = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
bkview.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = bkview;
答案 1 :(得分:0)
您发布customBackground,然后调用[customBackground drawRect:selectedBackgroundCell.frame];
很明显,在nil指针上调用函数不会做任何事情。