2010-07-20 35 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; 
}