UITableViewCell backgroundView изменение размера изображения? - PullRequest
1 голос
/ 16 октября 2010

Я использую изображение для отображения всего содержимого UITableViewCell, и оно устанавливается с помощью свойства backgroundView.

Моя проблема заключается в том, что оно всегда масштабируется по размеру ячейки.Есть ли способ отключить масштабирование в этом случае, поэтому я могу предоставить только версию с разрешением 480 пикселей, и она просто обрезается при портретной ориентации?

Я делаю это так:

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

Я пытался использовать две версии изображений для переключения между ними при изменении ориентации.Это тоже пока работает, но есть недостаток, что вы всегда видите масштабирование во время обработки анимации.

Спасибо за вашу помощь

Арне

Ответы [ 3 ]

6 голосов
/ 16 октября 2010

Установите режим содержимого ваших просмотров фонового изображения.

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

Это говорит UIImageView поместить его изображение в верхний левый угол, а не масштабировать его, чтобы соответствовать.

1 голос
/ 16 октября 2010

Я не пробовал это на симуляторе или устройстве, но может работать следующее:

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

Дайте мне знать, удалось ли вам запустить его.

0 голосов
/ 16 октября 2010
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 

это то, что вы искали, я думаю.

больше информации: http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...