使用tableView willDisplayCell:delegate方法自定义tableview背景

时间:2013-03-28 12:19:33

标签: iphone ios5 uitableview uiimage

我想用我自己的图像自定义tableview背景我正在使用fallowing代码来自定义单元格背景视图

-(void)tableView:(UITableView *)tableView
 willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSInteger rowCount = [self tableView:[self tableView] numberOfRowsInSection:0];
    NSInteger rowIndex = indexPath.row;
    UIImage *background = nil;

    if (rowIndex == 0) {
        background = [UIImage imageNamed:@"TableTopBackground.png"];
    } else if (rowIndex == rowCount - 1) {
        background = [UIImage imageNamed:@"TableBottomBackground.png"];
    } else {
        background = [UIImage imageNamed:@"TableMiddleBackground.png"];
    }

    [cell setBackgroundView:[[UIImageView alloc]initWithImage:background]];

}

我的问题是当我在ios5(图像1)中运行时,底部单元格图像和中间单元格图像互换,但在ios6中它很好(图像2)。

ios 5 image

ios 6 image

但是设置背景颜色它在ios5和ios 6中都运行正常。任何人都可以帮我解决它。

在cellForRowatindexpath中编写代码,但为什么它在willdisplaycell中不起作用。

谢谢

1 个答案:

答案 0 :(得分:1)

image放入cellForRowAtIndexPath方法并添加

    cell.backgroundView.backgroundColor = [UIColor colorWithPatternImage:image];

此代码在

之间写入
if(cell == nil)
 {
 }
相关问题