2010-10-16 115 views
1

我正在使用圖像來顯示UITableViewCell的整個內容,並使用backgroundView屬性進行設置。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; 
} 

我嘗試使用圖像的兩個版本取向時改變它們之間進行切換。這個工作到目前爲止,但它的缺點是你總是看到處理動畫時的縮放比例。

感謝您的幫助

阿恩

回答

1

我沒有嘗試這種在模擬器或設備,但以下可能的工作:

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

感謝您的回答。我太遲了,因爲我回來時已經找到了詹姆斯的解決方案。但是,無論如何感謝您的幫助! – arnekolja 2010-10-16 16:06:09

+0

謝謝!我將'UIViewContentModeTopLeft'與'clipToBounds = YES'組合使用,以便爲我的項目工作。 – primehalo 2015-12-21 00:50:15

6

設置背景圖像視圖的內容模式。

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

這告訴UIImageView把它的形象在左上角,而不是對其進行縮放以適應。

+0

不知道那個,比我的想法更聰明(和更短):) – Robin 2010-10-16 15:23:00

+0

有時懶得去付錢。 :)這是一個'UIView'屬性,所以它在幾個地方派上用場。 – 2010-10-16 15:28:04

+0

我正在玩contentMode,但看起來像我在錯誤的地方。謝謝,它像一個魅力:) – arnekolja 2010-10-16 16:03:09