2010-09-22 42 views
1

我有一些代码可以改变在分组表格视图中完美工作的单元的背景图像。但是,并不是一张普通的表格。有谁知道为什么这些行为有所不同?我想要做的就是给表格单元格添加一个背景图像(普通)iPhone:普通表问题。没有出现单元背景图像

这对分组表格视图完美。我是否需要自定义单元格才能使用普通视图?

感谢西蒙

-(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

你太过于复杂了。

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

这么简单,完全工作 – Burf2000 2010-09-23 13:32:48

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]; 
} 
+0

问题,我的其他开发人员说,你不能把一个图像放在一个普通的单元格?他在说什么BS – Burf2000 2010-09-22 15:02:11

+0

讨论 普通样式表(UITableViewStylePlain)中的单元格的默认值为零,而分组样式表UITableViewStyleGrouped的默认值为零。 UITableViewCell将背景视图添加为所有其他视图背后的子视图并使用其当前帧位置。 http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html – vodkhang 2010-09-22 15:11:22

+0

上述评论来自UITableViewCell文档。然后,我想,这是你的问题,你可能需要设置cell.backgroundView = cellImageView,而不是添加为子视图 – vodkhang 2010-09-22 15:12:38