47
votes

i have a UITableView, where theres 3 images. 1 for the selected Cell, 1 for the Cell background, and 1 for the TableView background.

My selected Cell, is working just fine, but have some problems with the normal cells, and the TableView background(the background behind the cells when you scroll down/up to much)

Can some one help me with a background for each cell, and a TableView background ? how is this done ?

Normal Cell background: (not sure if its right)

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

Selected Cell Background:

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

9 Answers

144
votes

For Cell

    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] ];

For Tableview

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

You can set UITableViewCell background color/image, By following delegate method

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

  }
7
votes

In swift you can set background image to UITableViewCell like below.

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

Write this code in this function of table view.

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

This image will repeat fort each row.

3
votes

this sets the background for the tableView not the cell

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

try get set cellbackground in cellForRowAtIndexPath

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

In Swift 4, this works:

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

set a image in UITableview cell

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

0
votes

Only this solution worked out for me .

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

Hope this helps !

0
votes

You can try this code of block for setting background image of 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];

Hopes it work for you. :)

-1
votes
//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];
}