点亮关于绘制单元格矩形的解决scheme

如何自定义分组表格视图单元格的背景/边框颜色?

阅读这篇文章后,我试图使用这个解决scheme。 这是我的代码,在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"]]]; } 

但不幸的是,它不会改变任何东西。 你知道我在做什么错吗?

我相信,每当提出意见,那么你应该指定一些框架。 因此,为自定义背景和背景指定框架CGRectZero ….

在我testing之后,继续为我工作。

  UIView *bkview = [[[UIView alloc] initWithFrame:CGRectZero] autorelease]; bkview.backgroundColor = [UIColor redColor]; cell.selectedBackgroundView = bkview; 

你释放customBackground然后你调用[customBackground drawRect:selectedBackgroundCell.frame];

所以显然,调用一个零指针的函数将不会做任何事情。