【问题标题】:UITableViewCell backgroundView image resizing?UITableViewCell backgroundView 图像调整大小?
【发布时间】:2010-10-16 14:58:53
【问题描述】:

我正在使用图像来显示 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;
}

当方向改变时,我尝试使用两个版本的图像在它们之间切换。到目前为止,这也是可行的,但它的缺点是在处理动画时总是会看到缩放。

感谢您的帮助

阿恩

【问题讨论】:

    标签: objective-c uitableview ios4 uiimageview


    【解决方案1】:

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

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

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

    【讨论】:

    • 不知道那个,比我的想法更聪明(也更短):)
    • 有时懒惰是值得的。 :) 这是一个UIView 属性,所以它在一些地方会派上用场。
    • 我在玩 contentMode,但似乎我做错了地方。谢谢,它就像一个魅力:)
    【解决方案2】:

    我没有在模拟器或设备上尝试过,但以下方法可能有效:

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

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

    【讨论】:

    • 感谢您的回答。我来不及检查它,因为我回来时已经找到了詹姆斯的解决方案。不过还是谢谢你的帮助!
    • 谢谢!我将UIViewContentModeTopLeftclipToBounds = YES 结合使用以使其适用于我的项目。
    【解决方案3】:
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
    

    你在找吗,我想。

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

    【讨论】:

      猜你喜欢
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 2012-10-17
      • 1970-01-01
      • 2010-11-20
      • 1970-01-01
      • 2015-11-02
      相关资源
      最近更新 更多