views:

31

answers:

3

Hello,

I am using an image to display the whole content of a UITableViewCell and it's set using the backgroundView property.

My problem is that it is always scaled to fit the cell. Is there a way to disable scaling in this case, so I can provide the 480 pixel version only and it just gets cropped when orientation is portrait?

I'm doing it like this:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
        UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"menu.0%d.large.png", indexPath.row+1]]];
        UIImageView *selectedBgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"menu.0%d.large.selected.png", indexPath.row+1]]];    
        cell.backgroundView = bgView;
        cell.selectedBackgroundView = selectedBgView;        
        [bgView release];
        [selectedBgView release];
    return cell;
}

I tried using two versions of the images to switch between them when orientation is changed. This works so far, too, but it's got the drawback that you always see the scaling while the animation is processed.

Thanks for your help

Arne

A: 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 

is was you looking for, I think.

more information : http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html

dwursteisen
A: 

Hi

I did not try this on the simulator or device, but the following might work:

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
    // New!
    cell.backgroundView.clipsToBounds = YES;

    UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"menu.0%d.large.png", indexPath.row+1]]];
    UIImageView *selectedBgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"menu.0%d.large.selected.png", indexPath.row+1]]];    
    // New!
    bgView.center = CGPointMake(160, 25); // Adjust the y-Value to be exactly half of the height of your cell
    bgView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin; // Not quite sure whether these two will work, maybe you have to play a little with the Masks

    cell.backgroundView = bgView;
    cell.selectedBackgroundView = selectedBgView;        
    [bgView release];
    [selectedBgView release];
return cell;
}

Let me know whether you managed to get it running.

Robin
Thanks for your answer. I was too late to check it out, as I already found James' solution when I came back. But thanks for your help anyway!
arnekolja
+1  A: 

Set the content mode of your background image views.

bgView.contentMode = UIViewContentModeTopLeft;
selectedBgView.contentMode = UIViewContentModeTopLeft;

This tells the UIImageView to put its image in the top left corner rather than scale it to fit.

James Huddleston
Didn't know that one, much smarter (and shorter) than my idea :)
Robin
Sometimes it pays to be lazy. :) It's a `UIView` property, so it comes in handy in a few places.
James Huddleston
I was playing with contentMode, but seems like I did it in the wrong place. Thanks, it works like a charme :)
arnekolja