简体   繁体   English

在 UITableview 单元格中折叠 UIImageview,当它不返回图像时

[英]Collapse UIImageview in UITableview cell, when it doesn't return an image

Summary of the problem问题总结

I have a UITableView in which I want to Collapse it's UIImageView cell part, whenever it doesn't return an image, as seen below我有一个 UITableView,我想在其中折叠它的 UIImageView 单元格部分,只要它不返回图像,如下所示

first image of tableview cell (the expected result) tableview 单元格的第一张图像(预期结果)

正确的形式 second image of tableview cell (the no image result) tableview 单元格的第二张图像(无图像结果)

无图像形式

Background including what I've already tried背景包括我已经尝试过的

Below is the piece of code that I have been working on.下面是我一直在研究的一段代码。 Also notice that I'm using SDWebImage to get and set the image in the app (any method in tableview delegates, SDWebImage protocols etc that I'm missing just point me),还要注意,我正在使用 SDWebImage 来获取和设置应​​用程序中的图像(tableview 委托中的任何方法、SDWebImage 协议等我缺少的只是指出我),

The comment out part is what I have done, ie used constraints to try the collapse of UIimageView not it messes up the whole tableView upon scrolling注释掉的部分是我所做的,即使用约束来尝试折叠 UIimageView,而不是在滚动时弄乱整个 tableView

Code代码

Here is piece of code I'm working on,这是我正在处理的一段代码,

[cell.i_imageView sd_setImageWithURL:[NSURL URLWithString: item.PostimageUrl] placeholderImage:[UIImage imageNamed:@"no-image"] options:SDWebImageRefreshCached completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {

        if(!image)
        {
                    //cell.i_imageView.hidden = YES;

            NSLog(@"why no image des:%@",error);
            NSLog(@"why no image url:%@",imageURL);
        }

//            cell.imageHolderAspectConstraint.active = TRUE;
//            cell.imageHolderZeroImageConstraint.active = FALSE;
//        }
//        else
//        {
//            cell.imageHolderAspectConstraint.active = FALSE;
//            cell.imageHolderZeroImageConstraint.active = TRUE ;
//
//            [cell layoutIfNeeded];
//            [cell setNeedsLayout];
//        }

        [cell.i_imageView sd_removeActivityIndicator];

    }];

Any help in this matter is highly appreciated, cheers.对此事的任何帮助表示高度赞赏,干杯。

Edit编辑

Here is the image of storyboard section, where do I need to embed stackview这是故事板部分的图像,我需要在哪里嵌入堆栈视图

在此处输入图片说明

在此处输入图片说明

Add your ImageView in StackView .StackView添加您的ImageView Now hide your ImageView when image is not available.现在当图像不可用时隐藏你的 ImageView。

Note : UIStackView automatically handle height of cell.注意: UIStackView 自动处理单元格的高度。

Edit:编辑:

As of now your UI is something like this.到目前为止,您的用户界面是这样的。

在此处输入图片说明

Now select your image holding view and click on embedded in and select UIStackView.现在选择您的图像持有视图并单击嵌入并选择 UIStackView。

在此处输入图片说明

Now assign the same constraints that you have given to holding view to stackView.现在,将您为保持视图赋予的相同约束分配给 stackView。

Now your hierarchy is like below现在你的层次结构如下

在此处输入图片说明

Finally you need to do below code in CellForRow:最后,您需要在 CellForRow 中执行以下代码:

cell.vImageHoldingView.hidden = !image;
  • Embed if in a stackView如果在 stackView 中嵌入
  • Attach stackview anchors to the desired anchors将堆栈视图锚点附加到所需的锚点
  • Hide image view if needed.如果需要,隐藏图像视图。

在此处输入图片说明

StackView automatically collapses its hidden arrangedSubViews StackView 自动折叠其隐藏的arrangedSubViews视图

Note: If you set the image nil , it will collapse again, because stackView manages imageView height according to its image.注意:如果设置了图像nil ,它会再次折叠,因为 stackView 根据其图像管理 imageView 高度。

If there is no image, make the ImageView height to 0 .如果没有图像,则将ImageView高度设为0 Remaining things will get handled if it has a proper constraints.如果有适当的约束,剩下的事情就会得到处理。

make picture bottom constraint as outlet .使图片底部约束作为出口。 Like this像这样

var bottomViewConstraints: NSLayoutConstraint! 

and give value on collapse and expand.并赋予折叠和扩展的价值。 like this像这样

bottomViewConstraints.constant = 0 // expand 
bottomViewConstraints.constant = 60 // collapse  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 快速单击UITableView中的单元格时崩溃部分 - Collapse section when click cell in UITableView in swift UITableView单元格背景颜色滚动时会折叠 - UITableView Cell Background Color Collapse when Scroll 在iOS中选择单元格时,UIImageView没有覆盖UITableView中的正确单元格 - UIImageView is not covering correct cell in UITableView when cell is selected in iOS 图像不适合UIImageView - image doesn't fit in UIImageView 在UITableView中批量插入时,第一个单元格的insertRowAnimation不起作用 - insertRowAnimation for the first cell doesn't work when batch inserting in UITableView 当UITableView滚动到不可见的单元格时,UITextField不会获得光标 - UITextField doesn't get cursor when UITableView scrolles to not visible cell UITableView 选中的单元格在滚动时不会保持选中状态 - UITableView selected cell doesn't stay selected when scrolled UIImageView添加到单元格内容中,在滚动浏览UITableView时随机显示 - UIImageView added to cell contentView displaying randomly when scrolling through UITableView 从URL异步将图像加载到单元格中的UIImageView时不会在初始加载时加载 - Async loading image from URL into UIImageView in cell doesn't load on initial load 使用透明度更新UITableView单元格中的UIImageView - Update UIImageView in a UITableView cell with transparency
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM