views:

75

answers:

2

Hi there

I have a some code that changes the background image of a cell which works perfectly in a grouped table view. However, does not in a plain table. Does anyone know why these behave differently? All I want to do is add a background image to a table cell (plain)

This works perfectly on a grouped table view. Do I need a custom cell for it to work with a plain view?

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

}

+1  A: 

I suspect that in the plain table view, the indexPath.section will not return you correct data. So these few lines of codes can give you a nil rowData. Double check for that

// 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];
}
vodkhang
Question, my other developer said that you cant put a image on a plain cell? is he talking BS
Burf2000
DiscussionThe default is nil for cells in plain-style tables (UITableViewStylePlain) and non-nil for grouped-style tables UITableViewStyleGrouped). UITableViewCell adds the background view as a subview behind all other views and uses its current frame location. http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html
vodkhang
The above comment is from UITableViewCell docs. Then, I think, it is your problem, you may need to set the cell.backgroundView = cellImageView rather than add as a subview
vodkhang
+1  A: 

You're overcomplicating things.

UIImage * image = [appDelegate ...];
cell.backgroundView = [[[UIImageView alloc] initWithImage:image] autorelease];
tc.
So simple and totally worked
Burf2000