UITableView单元格与背景图像

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

我select的单元格工作得很好,但在正常单元格和TableView背景(当您向下滚动/向上滚动时单元格背后的背景)

有人可以帮助我为每个单元格的背景和一个TableView背景? 这是怎么做的?

正常细胞背景:(不知道是否正确)

// CELL'S BACKGROUND-IMAGE self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cell_normal.PNG"]]; 

选定单元格背景:

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

对于细胞

  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背景颜色/图像 ,通过下面的委托方法

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

在swift中,可以像下面那样将背景图像设置为UITableViewCell。

 cell.backgroundView = UIImageView(image: UIImage(named: "yourImage.png")!) 

把这个代码写在这个表格视图的函数中。

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

这个图像将重复每行。

这为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"]]; 

在UITableview单元格中设置一个图像

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

只有这个解决scheme为我解决。

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

希望这可以帮助 !

你可以试试这个代码块来设置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]; 

希望它为你工作。 🙂

 //this concept is right but one problem is create when ur fastly scroll in uitable View then cell background color slowly change and indexing problem for Cell UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame]; myBackView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:0.75 alpha:1]; cell.selectedBackgroundView = myBackView; [myBackView release]; for table aTableView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"cloud.jpg"]]; aTableView.separatorStyle = UITableViewCellSeparatorStyleNone; //this concept is right but one problem is cteare when ur fastly scroll in uitable View then cell background color slowly change and indexing problem so use this code for Cell - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { cell.backgroundColor = (indexPath.row%2)?[UIColor lightGrayColor]:[UIColor grayColor]; }