简体   繁体   English

如何在UITableView单元格中设置背景图像?

[英]How to set Background image in UITableView 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. 我创建了自定义单元格。我有分组样式的表视图。现在我想将Backgroung图像放到不同部分的每个单元格。如何设置自定义单元格的背景图像。 Here is the code for creating the custom cell. 以下是创建自定义单元格的代码。

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

// view.backgroundColor=[UIColor darkGrayColor]; // 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 将此代码放在cellForRowAtIndexPath方法中

   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;

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

通过setBackgroundView方法设置。使用你需要Image的形式设置Image,并将其设置为,

[cell setBackgroundView:urBgImgView];

i'd rather to add a CALayer or UIImageView to be the backgroud view. 我宁愿添加CALayer或UIImageView作为背景视图。 Because i used to set the BackgroundColor, but it never workd for me. 因为我曾经设置过BackgroundColor,但它从来没有为我工作过。 so i changed my mind by this way. 所以我改变了主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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