【发布时间】:2016-10-07 19:07:26
【问题描述】:
我想根据图像链接是否可用于该单元格行,将 UIImageView 隐藏在特定单元格的 UITableview 单元格中。
如果某个单元格的后端无法提供图像 url,那么我想“禁用” UIImageview 并回收其空间以扩展标签。 (UILabel 放在左边,UIImageView 放在右边。 因此,如果没有图像,那么如果标签文本有更多文本,我希望标签文本向右扩展。)
我尝试了两种解决方案: 1.通过设置宽度约束将UIImageView的宽度改为0。
let widthConstraint = NSLayoutConstraint(item: cell.articleImageView as UIView,
attribute: NSLayoutAttribute.Width,
relatedBy: .Equal,
toItem: nil,
attribute: NSLayoutAttribute.NotAnAttribute,
multiplier: 0,
constant: 0)
cell.myImageView.addConstraint(widthConstraint)
2。从单元格中删除 imageview。
cell.myImageView.removeFromSuperView()
但是当我滚动浏览和返回时,其他单元格的图像也会受到影响并被隐藏。 myImageView.hidden = true 也是如此。
顺便说一句,我使用 tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) 来检索单元格。并且 tableview 和 cell 是在 storyboard 中创建的。
任何帮助如何回收隐藏在单元格内的 UIImageview 占用的空间,但在滚动时不会影响其他行?
【问题讨论】:
-
你能贴出行法的单元格吗?你在做什么很好。将宽度约束设置为 0 应该可以完成这项工作。如果其他单元格正在影响,那么您在 cellForRow 方法中做错了。
-
让 cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) 为!我的表视图单元。如果存在 url,则使用 AlamofireImage 加载图像,否则将 widthConstraint 设置为 0。如果在滚动期间宽度为 0,Alamofire 会崩溃,但对于大约 20 的宽度,Alamofire 会加载但所有其他图像现在的宽度为 20。对于 hidden = true 的效果相同.无需 Alamofire 即可重现。这意味着,对 UIImageView 的任何更改都会影响之后的其他人。但是..,如果我执行 imageview.image = nil,那么只有该图像被隐藏,正如预期的那样(但我想回收图像空间)。
标签: ios uitableview uiimageview hide