0

I have a table view with some 'n' number of cells.My issue is with background image for cells!

I am already aware of the fact that apple documentation of UITableViewCell specifically says:

Note: If you want to change the background color of a cell (by setting the background color of a cell via the backgroundColor property declared by UIView) you must do it in the tableView:willDisplayCell:forRowAtIndexPath: method of the delegate and not in tableView:cellForRowAtIndexPath: of the data source.

The above description can also be seen here

But my concern is not with changing the color of the cell,rather it is with setting the background image for a cell.As we know we can make use of background color property to set the background image i.e.:

if (cell == nil) 
{
   cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier] autorelease];
   cell.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"buttonbg.png"]];
}

But using background color for setting background image to a table view cell isn't working and when I am trying to change the image but not color,why doesn't the above method/property work in datasource method(cell for row at index path)

Also to my surprise I found out that using an image view and setting the view as background view for cell works perfect and the same is suggested in this link by Mr.Ammy Worral. i.e.:

UIImage *cellImage = [UIImage imageNamed:kCellImage];
UIImageView *cellImageView = [[UIImageView alloc]initWithImage:cellImage];
[cell setBackgroundView:cellImageView];

But my doubt is why doesn't the background image work with background color method of cell!

Need some clarification on this!

Thanks all in advance :)

4

2 回答 2

1

表格视图会自动将单元格的背景颜色设置为自己的。它会在 -tableView:cellForRowAtIndexPath: 之后做到这一点,当然 :) 你有两个选择:1)在你的单元格中添加一个额外的视图并设置它的背景颜色(几乎你所做的)2)实现 -tableView:willDisplayCellAtIndexPath:在您的数据源/委托中(或其中的某些内容,请参阅苹果文档以获取确切名称)并手动设置单元格的背景颜色。

于 2012-07-27T08:08:52.807 回答
1

它不起作用,因为 UITableView 在您从 cellForRowAtIndexPath: 返回后对单元格的外观进行了一些修改。解决方案是将您的单元格子类化并覆盖 layoutSubviews 方法并在那里进行自定义。

于 2012-07-27T06:28:00.647 回答