How to set a repeating background image on a UITableCell

When I set a repeat background image to a UITableView with the following code, this is normal:

tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.png"]]; 

but when I use it to set a repeating background image in a UITableViewCell, following the code, nothing happens, I don’t know why, I also can’t set the background color for this cell either.

 cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.png"]]; 

Does anyone know how to do this? please help me!

+4
source share
2 answers
 cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cell.png"]]; 

which will go into this code block:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[RecipeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cell.png"]]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cell-on.png"]]; } // Configure cell return cell; } 

You can also see how to set the selected image inside inside: selectedBackgroundView.

 cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cell-pressed.png"]]; 

I am not sure about the repetition, but I am sure that the backgroundView can contain contentMode on it.

+9
source

Oh, it's easier than I think. I just need to set the background image by executing the following code:

 cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg.png"]]; 

and the whole background view of the cell is automatically populated with this background image. :)

+1
source

Source: https://habr.com/ru/post/1315482/


All Articles