iphone UITableViewCell添加图片

时间:2011-10-14 06:56:26

标签: iphone image uitableview

我有一个TableViewController的子类,以及initWithStyle方法,如:

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {        
        [self.view setFrame:CGRectMake(0, 200, 320, 280)];
    }
    return self;
} 

我想在我的每个TableViewCell中添加背景图像,并显示图像,但它只显示图像的一部分(我的图像大小为320×48),我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.imageView.image = [UIImage imageNamed:@"cell_bg_light.png"];    
    return cell;
}

结果: enter image description here

1 个答案:

答案 0 :(得分:0)

你可以尝试这个。

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

或者您可以使用

UIView *cellBackView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
cellBackView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"cell_bg_light.png"]];
cell.backgroundView = cellBackView;