2011-10-14 43 views
0

我有TableViewController的一个子类,并initWithStyle方法,如:iphone UITableViewCell中添加图像

- (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

回答

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; 
+0

非常感谢你,你是对的,但为什么呢? – Gaojian922188

+0

表格单元格分为两部分,第一部分是imageview和第二部分文本标签。 –

+0

imageview设置在单元格的左侧。你可以参考http://developer.apple.com/library/IOS/#documentation/UIKit/Reference/UITableViewCell_Class/Reference/Reference.html –

相关问题