2

我有一个 UITableView,其中每个单元格在其 contentView 中包含一个 UIView,我们称它为 V。V 也有子视图,一个 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 就会变得混乱。

有什么意见、建议吗?

4

1 回答 1

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 框架。

于 2013-02-28T02:03:30.317 回答