2012-02-16 93 views
2

我想讓UITableView看上去很像是它的樣式爲,但有一些細微差別(如不同的顏色和圓角較短的半徑)。行具有可變的高度。我想爲我的應用程序中的大多數表格視圖控制器使用此自定義樣式。查看Twitter,Groupon,& GitHub發佈示例應用程序。自定義UITableViewCell backgroundView和selectedBackgroundView

我該怎麼做?

我想這樣做像做的。 套backgroundView & selectedBackgroundView既能的UIGroupTableViewCellBackground實例(的UIView的子類)。 UIGroupTableViewCellBackground是其層的委託並實現drawLayer:其層中的內容設定爲CGImageRef

  1. 我敢肯定蘋果根據Quartz 2D Programming Guide : Creating a Bitmap Graphics Context,也建議使用CGLayer考慮,而不是繪製成位圖圖形上下文創建此CGImageRef。這對於這個應用程序更好?

  2. 另外,表視圖的所述第一小區添加陰影帶倒圓的角部,以使用UIImageView其頂部(它它設置爲一個可調整大小(寬度方向)UIImage具有半透明PNG文件)。爲什麼它去做?這不是很慢的滾動嗎?爲什麼不把它畫到第一個單元的CGImageRef?也許性能下降並不顯着,並且通過圖像更容易使單元看起來正確。我將單元格的CGImageRef保存到磁盤並使用預覽將其打開。它仍然有圓角。此疊加視圖只是添加頂部陰影。

    下面是運行我的設備與選定的表視圖「顏色混合層」的核芯顯卡儀器工具的屏幕截圖。你可以看到頂部的影子是混合的。而且,看起來最後一個單元格中還有一些混合。

    enter image description here

    enter image description here

    在選中第三小區(不混合)。選擇第一個或最後一個單元格的行爲相同,但不會擺脫已混合的重疊視圖。

    enter image description here

回答

0

你可以指望正在繪製的行數和圖像設置爲背景單個細胞。

,你所要做的就是

cell.backgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cell_normal.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease]; 
cell.selectedBackgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cell_pressed.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease]; 

使用這個方法來設置背景圖片

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{  

     &&&& 


    if((indexPath.row)==0) 
     // set image upper corner image here 

    else if (indexPath.row==1) 
     //set normal image here 

    else if (indexPath.row==YourArray) 
     // also set the lower corner round image in the last low, which can be calculated by comparing indexpath.row to the mutablearray/array 

} 

您還可以在應用一個條件& & &部分之前應用不同的圖像其中有上下兩個角落的情況array.count == 1

相關問題