Iphone 带背景图像的UITableView单元格

Iphone 带背景图像的UITableView单元格,iphone,uitableview,background,Iphone,Uitableview,Background,我有一个UITableView,其中有3个图像。 1表示选定单元格,1表示单元格背景,1表示TableView背景 我选择的单元格工作正常,但与普通单元格和TableView背景(向下/向上滚动时单元格后面的背景)有一些问题 有人能帮我为每个单元格设置一个背景吗?还有一个TableView背景吗?这是怎么做到的 正常单元格背景:(不确定是否正确) 所选单元格背景: // SELECTED CELL'S BACKGROUND-IMAGE UIView *viewSelected = [[[

我有一个UITableView,其中有3个图像。 1表示选定单元格,1表示单元格背景,1表示TableView背景

我选择的单元格工作正常,但与普通单元格和TableView背景(向下/向上滚动时单元格后面的背景)有一些问题

有人能帮我为每个单元格设置一个背景吗?还有一个TableView背景吗?这是怎么做到的

正常单元格背景:(不确定是否正确)

所选单元格背景:

// SELECTED CELL'S BACKGROUND-IMAGE
    UIView *viewSelected = [[[UIView alloc] init] autorelease];
    viewSelected.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"cell_highlighted.PNG"]];
    cell.selectedBackgroundView = viewSelected;

这将为tableView而不是单元格设置背景

// CELL'S BACKGROUND-IMAGE
    self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cell_normal.PNG"]];
尝试在cellForRowAtIndexPath中设置cellbackground

 cell.backgroundColor =  [UIColor colorWithPatternImage:[UIImage imageNamed:@"cell_normal.PNG"]];
细胞

    cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cell_normal.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];  
    cell.selectedBackgroundView =  [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cell_pressed.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
对于Tableview

    [mEditTableView setBackgroundView:nil];
    [mEditTableView setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"apple.png"]] ];

您可以通过以下委托方法设置
UITableViewCell
backgroundcolor/image

 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath
 *)indexPath  
{      
     if((indexPath.row)==0)  
       cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cell_normal.PNG"]]; //set image for cell 0

     if (indexPath.row==1)  
       cell.backgroundColor = [UIColor colorWithRed:.8 green:.6 blue:.6 alpha:1]; //set color for cell 1

     tableView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"cloud.jpg"]]; //set image for UITableView

  }

您可以尝试使用此代码块设置UITableviewCell的背景图像

self.backgroundView = [[[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"background.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]] autorelease];

self.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"background_selected.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]] autorelease];

希望它对您有用。:)

在swift中,您可以将背景图像设置为UITableViewCell,如下所示

cell.backgroundView = UIImageView(image: UIImage(named: "yourImage.png")!)
在tableview的这个函数中编写这个代码

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 

此图像将在每行重复。

在UITableview单元格中设置图像


cell.imageView.image=[UIImage ImageName:@“image.png”]

只有这个解决方案对我有效

Cell.backgroundColor = [UIColor clearColor];
Cell.contentView.backgroundColor=[UIColor clearColor];

希望这有帮助

在Swift 4中,这一功能:

cell.backgroundView = UIImageView.init(image: UIImage.init(named: "YORIMAGE.jpg"))

当我尝试在手机上调试时遇到一些问题:/phone:-模拟器:-可能有什么问题?出现问题了lol。。我的手机文件名为xxx.PNG而不是xxx.PNG-显然它的案例sencetiveWorks很棒。。。如果您使用自动引用计数(RFC),只需删除自动删除即可。我用这个答案实现了内心的平静。2014年效果很好我知道这已经得到了回答:)但也要检查这个答案
Cell.backgroundColor = [UIColor clearColor];
Cell.contentView.backgroundColor=[UIColor clearColor];
cell.backgroundView = UIImageView.init(image: UIImage.init(named: "YORIMAGE.jpg"))