2010-10-16 57 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