2013-11-22 25 views
0

我有一個UITableViewCell,我想繪製一些線條(比如覆蓋整個寬度的TableCell頂部1/3的2pt線)。這些行將始終在tableview單元格中的相同位置。在UITableViewCell中繪製線條(不使用drawRect)

一個簡單的解決方案是隻需要一個-drawRect方法,將使用CGContextStrokePath來畫線。但是這似乎是一種矯枉過正,因爲它每次都會調用drawRect,效率不高。

我認爲將有一種方法能夠高速緩存它以某種方式,以便所有的tableview單元格默認情況下與行創建。我能想到的一種方法是創建2pt * 2pt png文件並將其添加到tableview單元格頂部的UIImageView。任何其他方式?

回答

0

您可以添加UIView,就像您的UITableViewCell的子視圖。

1

另一種選擇是,添加一個UIView並設置其背景顏色。例如:

UIView *lineView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,cell.contentView.bounds.size.width, 2)] autorelease]; 
lineView.backgroundColor = [UIColor grayColor]; 

[cell.contentView addSubview:lineView]; 
+2

OFC你也可以這樣做,在故事板,所以所有的單元都有它(並且可重用的單元應該具有比將代碼手動添加到每個單元更好的性能) – Marc

2

試試這個 -

cellForRowAtIndexPath添加該代碼,只需調整y位置OG這個圖像視圖 -

UIImageView *seperatedImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 49, 320, 1)]; 
seperatedImageView.backgroundColor = [UIColor colorWithRed:201.0/255.0 green:250.0/255.0 blue:152.0/255.0 alpha:1.0]; 
[cellBackView addSubview:seperatedImageView];