How to set background image in UITableView cell?

I created a custom cell. And I have a table view with a group style. Now I want to put a Backgroung image in each cell of another section. How to set a background image of a custom cell. Here is the code to create a custom cell.

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 305, 90)]; 

//view.backgroundColor = [UIColor darkGrayColor];

 UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(11, 9, 61, 55)]; // NSLog(@"imageArray%@",[play.imageArray count]); NSString *img=[play.imageArray objectAtIndex:indexPath.row]; NSLog(@"img=%@",img); imageV.image = [UIImage imageNamed:img]; [view addSubview:imageV]; [imageV release]; UILabel *lblAchievementName = [[UILabel alloc] initWithFrame:CGRectMake(90, 10, 168, 21)]; lblAchievementName.text = [play.listData objectAtIndex:indexPath.row]; lblAchievementName.backgroundColor=[UIColor clearColor]; lblAchievementName.textColor=[UIColor orangeColor]; [lblAchievementName setFont:[UIFont fontWithName:@"Arial Black" size:16.5]]; [view addSubview:lblAchievementName]; [lblAchievementName release] [cell.contentView addSubview:view]; return cell; 

Now, how to set the inverse image for this custom view?

+4
source share
4 answers

You can try this code to help you.

Put this code in the cellForRowAtIndexPath method

  UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 277, 58)]; av.backgroundColor = [UIColor clearColor]; av.opaque = NO; av.image = [UIImage imageNamed:@"categorytab1.png"]; cell.backgroundView = av; 
+21
source

You can use the following code for another image in another section.

 UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(11, 9, 61, 55)]; // NSLog(@"imageArray%@",[play.imageArray count]); NSString *img=[play.imageArray objectAtIndex:indexPath.row]; NSLog(@"img=%@",img); if (indexPath.section == 0) { imageV.image = [UIImage imageNamed:img0]; } else if (indexPath.section == 1) { imageV.image = [UIImage imageNamed:img1]; } else if (indexPath.section == 2) { imageV.image = [UIImage imageNamed:img2]; } [view addSubview:imageV]; [imageV release]; 
+3
source

set via the setBackgroundView method. To form ImageView with ur you need image a and set what like,

 [cell setBackgroundView:urBgImgView]; 
+2
source

I would rather add a CALayer or UIImageView as a backgroud view. Because I used to install BackgroundColor, but it never worked for me. so I changed my mind this way.

0
source

All Articles