改变细胞背景

时间:2012-02-17 04:22:17

标签: iphone objective-c

如何更改uitableview单元格背景图片?

我写这段代码,它不起作用。但我确信这段代码有些小错误。

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) 
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        [cell setBackgroundView:[UIImage imageNamed:@"selected-bg.png"]];
}

2 个答案:

答案 0 :(得分:4)

您需要将UIImage包裹在UIImageView中,然后才能将其设为backgroundView

UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"selected-bg.png"]] autorelease];
[cell setBackgroundView:imageView];

答案 1 :(得分:3)

您将错误的参数传递给setBackgroundView:的{​​{1}}函数。它期望UITableViewCell的实例或从UIView继承的任何实例。

UIView