0

I would like to programmatically set an image to the background of each cell in a TableViewCell. Any idea on how to do that ?

Thx in advance,

Stephane

3 Answers 3

4

You should implement a tableView delegate method in order to do this because is Apple recomendation.

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"image.png"]];
}

A table view sends this message to its delegate just before it uses cell to draw a row, thereby permitting the delegate to customize the cell object before it is displayed. This method gives the delegate a chance to override state-based properties set earlier by the table view, such as selection and background color. After the delegate returns, the table view sets only the alpha and frame properties, and then only when animating rows as they slide in or out.

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

You can create a UIView with image as background with your tablecell's dimensions and add it to your cell, then you can add rest of the controls and elements on your UIView.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.