开发者

How to set the background of a TableViewCell with a static image?

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

Thx in advance,

Stephane


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜