简体   繁体   中英

How can I make an image in a grouped tableview cell the width of the screen?

I have large images displayed in a grouped tableview. I would like the images to fill the entire width of the screen. I have been able to make this work using a tableViewCell interface builder, but in an attempt to improve performance I am now trying to do this programmatically.

However, I have not been able to get the image to be flush against the left side of the screen. It seems to be using the default position of the cells in the grouped tableview.

I was able to make it work using a plain tableview, but then the section headers anchor on the top of the screen and I need them to scroll.

Any ideas how to do this programmatically? Here's my original code below. Thanks!

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *MyTableViewCellIdentifier = @"Cell";

MyTableViewCell *cell = (MyTableViewCell *) 
            [tableView dequeueReusableCellWithIdentifier: MyTableViewCellIdentifier];

    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyTableViewCell" owner:self options:nil];
    for(id currentObject in nib)

        {
            cell = (DetailTableViewCell *)currentObject;
        }

MyAppAppDelegate *appDelegate = (MyTableViewCell *)[[UIApplication sharedApplication] delegate];
NSString *Path = [[NSBundle mainBundle] bundlePath];
NSString *MainImagePath = [Path stringByAppendingPathComponent:
        ([[appDelegate.myDictionaryOfImages objectAtIndex:indexPath.section] objectForKey:@"LargeImage"])];

cell.myLargeImage.image = [UIImage imageWithContentsOfFile:MainImagePath];

return cell;
}

I finally got it working. Here is my final code.

#define PHOTO_TAG 1

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Photo";

UIImageView *photo;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
UIImage *theImage = [UIImage imageNamed:[[appDelegate.sectionsDelegateDict objectAtIndex:indexPath.section] objectForKey:@"MainImage"]];

imageHeight = CGImageGetHeight(theImage.CGImage);
imageWidth = CGImageGetWidth(theImage.CGImage);

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    photo = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, imageWidth, imageHeight)] autorelease];
    photo.tag = PHOTO_TAG;
    [cell addSubview:photo];
} else {
    photo = (UIImageView *) [cell viewWithTag:PHOTO_TAG];
    [photo setFrame:CGRectMake(0, 0, imageWidth, imageHeight)];
}

photo.image = theImage;
return cell;
}

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