2011-09-05 22 views
1

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"]]]; 
} 

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

回答

1

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

以下只是为我工作时,我测试。

UIView *bkview = [[[UIView alloc] initWithFrame:CGRectZero] autorelease]; 

    bkview.backgroundColor = [UIColor redColor]; 

    cell.selectedBackgroundView = bkview; 

+0

它没有帮助。谢谢。 –

+0

感谢您的写作....我与代码并改变了我的答案。请检查天气是否有帮助.... – Mohammad

+0

不要忘记发送'autorelease'到bkview! – mvds

0

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

显然,在一个零指针上调用该函数将不会执行任何操作。

相关问题