iPhone:普通表问题。细胞背景图像没有出现

时间:2010-09-22 13:33:29

标签: iphone background cell tableview

我有一些代码可以更改单元格的背景图像,该图像在分组表格视图中完美运行。但是,不在普通表中。有谁知道为什么这些表现不同?我想要做的就是将一个背景图像添加到表格单元格(普通)

这适用于分组表格视图。我是否需要一个自定义单元格才能使用普通视图?

谢谢Simon

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

UniversalAppAppDelegate *appDelegate = (UniversalAppAppDelegate *)[[UIApplication sharedApplication] delegate];

NSArray *rowData;

// Check see if section Data is populated
if (self.sectionData == nil) {
    rowData = [(NSDictionary *)[tableData objectAtIndex:indexPath.section] objectForKey:@"SectionItems"];
} else {
    rowData = [(NSDictionary *)[sectionData objectAtIndex:indexPath.section] objectForKey:@"SectionItems"]; //[self.sectionData objectAtIndex:indexPath.section];
}

NSDictionary *cellDetails = [rowData objectAtIndex:indexPath.row];

// Clear any previous imageview
UIView *view = [cell.backgroundView viewWithTag:99];
if (view != nil)
    [view removeFromSuperview];

// Add a new imageview
NSString *filename = [cellDetails objectForKey:@"TableItemFullImage"];
if (filename.length > 0) {

    UIImageView *cellImageView = [[UIImageView alloc] initWithImage:[appDelegate imageForImageID:[cellDetails objectForKey:@"TableItemFullImage"] imageExtension:[cellDetails objectForKey:@"TableItemFullImageType"]]];
    cellImageView.tag = 99;
    CGRect newFrame = cell.backgroundView.bounds;
    cellImageView.frame = newFrame;

    [cell.backgroundView addSubview:cellImageView];
    [cellImageView release];
}

}

2 个答案:

答案 0 :(得分:2)

你过度复杂了。

UIImage * image = [appDelegate ...];
cell.backgroundView = [[[UIImageView alloc] initWithImage:image] autorelease];

答案 1 :(得分:1)

我怀疑在普通表视图中,indexPath.section不会返回正确的数据。所以这几行代码可以给你一个nil rowData。仔细检查

// Check see if section Data is populated
if (self.sectionData == nil) {
    rowData = [(NSDictionary *)[tableData objectAtIndex:indexPath.section] objectForKey:@"SectionItems"];
} else {
    rowData = [(NSDictionary *)[sectionData objectAtIndex:indexPath.section] objectForKey:@"SectionItems"]; //[self.sectionData objectAtIndex:indexPath.section];
}