2012-08-24 84 views
1

我正面临一个问题,即显示单元格选择的图像的高度高于单元格高度。如何使用高度超过细胞高度的细胞背景图像?

下面是示例:

enter image description here

所以在这里,我的身高是44,但所选单元格的背景图像高度为50

我试图设置单元格选择背景图片并且通过将新的图像视图添加到细胞作为子视图。但是这样做是为了让单元格变得被选中,并且图像越来越大,以填充单元格高度的大小。所以箭头出现在单元格内,图像被挤压。

我试过的第二种方法是使用按钮分开箭头图像,但该选项对我也不起作用。 如果我采用这种方法,那么按钮不会显示。在这里,我认为这部分会被表格视图的下一行重叠。

请告诉我如何解决这种情况。

在此先感谢。

+0

因此您可以调整单元格大小或将图像视图设置为自定义单元格。 –

+0

否,单元格大小要求相同,请检查显示的图像以及问题。如果我改变单元格高度比人物图像和名称对齐再次看起来很奇怪。无论如何,我们不能重叠下一个单元吗? – Mrunal

+0

将特定图像调整为高度44 –

回答

0

感谢您的回复。我有解决这一问题,该代码是在这里:

- (id)initWithStyle:(UITableViewCellStyle)style 
     reuseIdentifier:(NSString *)reuseIdentifier 
    { 
     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 

     if (self) { 
     // Initialization code 

     [self setClipsToBounds:NO]; 

     // selected background image view 
     if (self.selectedBgImageView == nil) { 

    //  w 302 : h 50 
      self.selectedBgImageView = [[UIImageView alloc] 
             initWithFrame:CGRectMake(0, -5, 302, 50)]; 
     } 
     [self.selectedBgImageView setContentMode:UIViewContentModeScaleToFill];  
     [self.contentView addSubview:self.selectedBgImageView]; 
} 


- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 

    // child count label 
    [self.childCountLabel setBackgroundColor: 
    [UIColor colorWithPatternImage: 
    [UIImage imageNamed:@"btn_contacts_network_total"]]]; 

    // selected background image view 
    if (self.indentationLevel % 2 == 0) { 

    [self.selectedBgImageView setImage: 
    [UIImage imageNamed:@"img_contacts_network_expand_box_tier1"]]; 
    } 
    else { 

    [self.selectedBgImageView setImage: 
    [UIImage imageNamed:@"img_contacts_network_expand_box_tier2"]]; 
    } 

    [self.selectedBgImageView setContentMode:UIViewContentModeScaleToFill]; 
// [self.con:selectedBgImageView]; 
} 

在那里,我发现,设置框架是一个问题,我不知道对于这样奇怪的计算框架起源的原因。但不知何故,它为我工作,并按要求完美运作。

如果有人知道此行为背后的原因,请指导我。

0

我想你可以玩Clip Subviews单元格或实际表格的属性。 您只需将UIImageView放入单元格(不确定是否可以将其指定为背景属性,但您可以尝试)并将其高度设置为比单元格高度大。但唯一的问题是你可能会遇到图像重叠。您可以尝试通过设置实际索引将视图置于选定状态的位置来解决此问题。

好,测试了一下。看看这个代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellId = @"cellIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId]; 
    if (!cell) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId] autorelease]; 
     cell.clipsToBounds = NO; 
     cell.textLabel.textColor = [UIColor whiteColor]; 
     cell.textLabel.backgroundColor = [UIColor clearColor]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 

     CGRect frame = cell.bounds; 
     UIImageView *background = [[UIImageView alloc] initWithFrame:frame]; 
     background.image = [UIImage imageNamed:@"CellBackground"]; 
     cell.backgroundView = background; 
     [background release]; 

     frame.size.height = 50.0; 
     UIImageView *selected = [[UIImageView alloc] initWithFrame:frame]; 
     selected.tag = 13; 
     selected.image = [UIImage imageNamed:@"CellSelectedBackground"]; 
     [cell insertSubview:selected atIndex:1]; 
     selected.hidden = YES; 
     [selected release]; 
    } 
    cell.textLabel.text = [NSString stringWithFormat:@"cell %d", indexPath.row]; 

    return cell; 
} 

这里主要的事情是cell.clipsToBounds = NO;[cell insertSubview:selected atIndex:1];。但是为单元格创建子类并处理所有内容和选择会更好。虽然看到你的照片,你已经使用了分类细胞:)