开发者

How to set Background image in UITableView cell?

开发者 https://www.devze.com 2023-04-13 09:11 出处:网络
I have Created custom cell.And i have table view with Grouped style.Now i want to put Backgroung image to each cell of different section.How to set the background image of custom cell.

I have Created custom cell.And i have table view with Grouped style.Now i want to put Backgroung image to each cell of different section.How to set the background image of custom cell. Here is the code for creating the 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 backgroung image for this custom view?


You can try this code that will help you.

Put this code in 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;


set thru setBackgroundView method.Form the ImageView with ur need Image aand set that as,

[cell setBackgroundView:urBgImgView];


You can use following code for different image in different 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];


i'd rather to add a CALayer or UIImageView to be the backgroud view. Because i used to set the BackgroundColor, but it never workd for me. so i changed my mind by this way.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号