2011-09-05 24 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]。

顯然,在一個零指針上調用該函數將不會執行任何操作。

相關問題