菜鸟教程小白 发表于 2022-12-12 09:12:38

iphone - UIImageView 单元格 contentView 的 View 之一没有与其父 View 一起扩展


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

<pre><code>- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSNumber * key = ;
    UITableViewCell *cell = ;
    UIView * v = [[ subviews] objectAtIndex:0];
    UIImageView* image = [ objectAtIndex:0];

    _tappedIndex = ;
    _expanded = == nil;
    NSLog(@&#34;view&#39;s old params: %@&#34;, ); //prints out the frame params
    NSLog(@&#34;image&#39;s old params: %@&#34;, );
    if (_expanded)
    {
      ;
      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
    {
      ;
      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(@&#34;view&#39;s new params: %@&#34;, );
    NSLog(@&#34;image&#39;s new params: %@&#34;, );

    ;
    ;
}

- (CGFloat)tableView:(UITableView *)tv heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *v = ;
    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;
}
</code></pre>

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

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

<p>有什么意见、建议吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>如果您使用 initWithImage:,UIImageView 的大小与其图像相同。我假设您在 IB 中添加图像,所以我猜测 IB 使用该方法来填充 ImageView 。如果您无法在扩展后添加图像(使用 setImage 会导致图像填充 ImageView 的大小),那么我只会使用带有圆角的 UIView。您不需要进行切片,只需使用带圆角的图层即可。这就是我为使单元格具有分组外观所做的工作,方法是在单元格内放置一个圆角白色矩形。这是 subview 的 init 方法:</p>

<pre><code>-(id)initWithCoder:(NSCoder *)aDecoder {
    if (self = ) {
      self.backgroundColor = ;
      CALayer *bgLayer = self.layer;
      ;
      ;
    }
    returnself;
}
</code></pre>

<p>如果您这样做,请务必导入 QuartzCore 框架。</p></p>
                                   
                                                <p style="font-size: 20px;">关于iphone - UIImageView 单元格 contentView 的 View 之一没有与其父 View 一起扩展,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/15126065/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/15126065/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iphone - UIImageView 单元格 contentView 的 View 之一没有与其父 View 一起扩展