在iphone应用程序的分组表视图中的每个部分中添加不同的图像作为背景

时间:2010-09-27 14:02:58

标签: iphone

我想使用Grouped Table View创建一个Iphone应用程序。我创建了Grouped Table View。我的分组表视图有三个部分。我想在分组表视图中为每个部分添加不同的图像作为背景。

如果我在每个部分使用以下代码,则总视图显示在同一图像中。

    NSString *backgroundPath = [[NSBundle mainBundle] pathForResource:@"background" ofType:@"jpg"];

    UIImage *backgroundImage = [UIImage imageWithContentsOfFile:backgroundPath];

    UIColor *backgroundColor = [[UIColor alloc] initWithPatternImage:backgroundImage];

    tableView.backgroundColor = backgroundColor; 

    [backgroundColor release];

1 个答案:

答案 0 :(得分:2)

试试这个:

- (void) tableView: (UITableView *) tableView willDisplayCell: (UITableViewCell *) cell forRowAtIndexPath: (NSIndexPath *) indexPath {
    UIColor * color;

    switch (indexPath.section) {
        case 0:
            color = [UIColor colorWithPatternImage: [UIImage imageNamed: @"...0"]]; break;
        case 1:
            color = [UIColor colorWithPatternImage: [UIImage imageNamed: @"...1"]]; break;
        case 2:
            color = [UIColor colorWithPatternImage: [UIImage imageNamed: @"...2"]]; break;
        default:
            color = [UIColor colorWithPatternImage: [UIImage imageNamed: @"..."]]; break;
            break;
    }

    cell.backgroundColor = color;
}

......X替换为您要使用的图片。添加更多内容或删除一些case以获得正确数量的部分。

相关问题