2010-07-20 70 views
7

我有一個UITableView,並希望將背景圖像應用於所有單元格。每個細胞的身高都是可變的。我應該如何去創建背景圖片?如何設置不同高度的UITableViewCell背景圖片?

cell.contentView.backgroundColor = [UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"background.png"]]; 
+0

這並不爲我工作(固定錯字後)的東西。 – 2012-06-14 07:26:20

回答

3

其中一個想法是將UIImage放置一個可拉伸的圖像。這樣,無論行高是多少,圖像都可以伸展以匹配。

你也可以做類似於馬特做here with a gradient layer

+0

你能描述可拉伸的圖像嗎?我是否需要創建一些1x55像素(只是一個示例)的圖像,並且會自動拉伸x平面或需要額外的代碼? – 2010-07-20 14:01:12

+3

你基本上只是給出一個圖像,然後告訴它哪些部分不能通過'[[UIImage imageNamed:@「someBGImage.png」] stretchableImageWithLeftCapWidth:5 topCapHeight:5];;'其中5像素是isn'可伸縮(如邊框)​​。更多信息http://developer.apple.com/iphone/library/documentation/uikit/reference/UIImage_Class/Reference/Reference.html#//apple_ref/occ/instm/UIImage/stretchableImageWithLeftCapWidth:topCapHeight: – iwasrobbed 2010-07-20 14:16:23

2

在的cellForRowAtIndexPath方法,你可以給單元格背景顏色

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 


{ 
    static NSString *cellIdentifier = @"Cell"; 
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if (cell == nil) 
    {  
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    } 
// for cell background color 
    cell.backgroundView =[[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"tab2.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]; 

// for selection color  
    cell.selectedBackgroundView =[[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"selected.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]; 
    return cell; 
}