UITableViewCell backgroundView图像大小调整?

时间:2010-10-16 14:58:54

标签: objective-c uitableview ios4 uiimageview

我正在使用图像显示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 个答案:

答案 0 :(得分:6)

设置背景图片视图的内容模式。

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

这告诉UIImageView将其图片放在左上角而不是缩放以适合。

答案 1 :(得分: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;
}

让我知道你是否设法让它运行。

答案 2 :(得分:0)

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
我想是你在寻找。

更多信息: http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html