【问题标题】:UIImageView one of cell contentView's views is not expanding along with its parent viewUIImageView 单元格 contentView 的视图之一没有与其父视图一起扩展
【发布时间】:2013-02-28 01:49:58
【问题描述】:

我有一个 UITableView,其中每个单元格在其 contentView 中包含一个 UIView,我们称之为 V。 V 也有 subViews,一个 UIImageView 和 UILabel.UIImage 只是一个白色的圆角矩形 我希望我的单元格(连同 UIImageView)在选中时展开和缩小。我在 didSelectRowAtIndexPath 和 heightForRowAtIndexPath 方法中添加了一些代码。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSNumber * key = [NSNumber numberWithInt:indexPath.row];
    UITableViewCell *cell = [cells objectAtIndex:indexPath.row];
    UIView * v = [[[cell contentView] subviews] objectAtIndex:0];
    UIImageView  * image = [[v subviews] objectAtIndex:0]; 

    _tappedIndex = [key intValue];
    _expanded = [_tappedCells objectForKey:key] == nil;
    NSLog(@"view's old params: %@", [GGStackPanel printFrameParams:v]); //prints out the frame params
    NSLog(@"image's old params: %@", [GGStackPanel printFrameParams:image]);
    if (_expanded)
    {
        [_tappedCells setObject:key forKey:key];
        v.frame = CGRectMake(v.frame.origin.x, v.frame.origin.y, v.frame.size.width, v.frame.size.height*1.5);
        image.frame = CGRectMake(image.frame.origin.x, image.frame.origin.y, image.frame.size.width, v.frame.size.height + 73);
    }
    else
    {
        [_tappedCells removeObjectForKey:key];
        v.frame = CGRectMake(v.frame.origin.x, v.frame.origin.y, v.frame.size.width, v.frame.size.height/1.5 );
        image.frame = CGRectMake(image.frame.origin.x, image.frame.origin.y, image.frame.size.width, v.frame.size.height - 113);
    }
    NSLog(@"view's new params: %@", [GGStackPanel printFrameParams:v]);
    NSLog(@"image's new params: %@", [GGStackPanel printFrameParams:image]);

    [tableView beginUpdates];
    [tableView endUpdates];
}

- (CGFloat)tableView:(UITableView *)tv heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *v = [_cells objectAtIndex:indexPath.row];
    CGFloat height = v.frame.size.height;
    if (indexPath.row == _tappedIndex)
    {
        if (_expanded)
        {
            height = v.frame.size.height * 1.5;
        }

        else
        {
            height = v.frame.size.height/1.5;
        }
    }
    return height;
}

单元格框架和 V 正在扩展,我记录了它们的框架参数。但是 imageView 始终保持不变,即使您在每次选择单元格时更改其框架。在 didSelectRowAtIndexPath 方法中它的框架发生了变化,但是当你再次点击同一个单元格时,它说它的框架与上次相比没有变化。

如果我放置另一个 UIView 而不是 imageView,它会随其父 View 和单元格展开和缩小。因此,我想出了一个非常奇怪的解决方案:剪下圆角矩形,将狭窄的顶部和底部部分保留为 imageView。然后在中间放一个空白的UIView,颜色和矩形一样。现在我的“矩形”正在扩展,因为空白 UIView 扩展并且这两个 imageViews 上下移动。我不喜欢这种解决方案,因为如果您将手机切换到横向模式,或者尝试从横向模式扩展单元格并返回纵向模式,UI 就会变得混乱。

有什么建议吗?

【问题讨论】:

  • 你能在 IB 中完成所有这些,设置单元格视图层次结构的内容模式和自动大小属性吗?

标签: iphone ios uiview uitableview uiimageview


【解决方案1】:

如果您使用 initWithImage:,UIImage 视图的大小与其图像相同。我假设您在 IB 中添加图像,所以我猜测 IB 使用该方法来填充图像视图。如果您无法在扩展后添加图像(使用 setImage 会导致图像填充图像视图的大小),那么我只会使用带有圆角的 UIView。您不需要进行切片,只需使用带圆角的图层即可。这就是我为使单元格具有分组外观所做的工作,方法是在单元格内放置一个圆角白色矩形。这是子视图的 init 方法:

-(id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        self.backgroundColor = [UIColor whiteColor];
        CALayer *bgLayer = self.layer;
        [bgLayer setMasksToBounds:YES];
        [bgLayer setCornerRadius:8];
    }
    return  self;
}

如果您这样做,请务必导入 QuartzCore 框架。

【讨论】:

  • 感谢您的解决方案,为我工作。我是iOS开发的菜鸟,不熟悉QuartzCore框架。你知道,我尝试在示例项目中以编程方式更改 UIImageView 的高度。我实际上可以这样做,如果它不受 tableCell 的限制,我只需在 IB 中将图像模式设置为“缩放以填充”。所以 UIImageView 的框架是可变的。仍然无法弄清楚为什么在我的情况下没有发生调整大小,其中 imageView 在一个视图中,它在单元格的 contentView 中。
猜你喜欢
  • 1970-01-01
  • 2017-07-25
  • 1970-01-01
  • 2012-03-12
  • 1970-01-01
  • 1970-01-01
  • 2015-12-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多