简体   繁体   中英

change cell background

How to change uitableview cell background image?

I write this code, it's not working. but I am sure this code some minor mistake.

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"]];
}

You need to wrap the UIImage in a UIImageView before you can set it as a backgroundView :

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

You are passing the incorrect parameter to setBackgroundView: function of UITableViewCell .. It expect either instance of UIView or any instance inherited from UIView .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM