简体   繁体   中英

iPhone : Plain table issue. Cell background image not appearing

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

}

You're overcomplicating things.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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