此问题的解释之后,将遵循代码。

我有一个UIViewController,它包含一个UITableView,其中的UITableViewCell显示一幅,两幅或零幅图像。对于一幅和两幅图像,像元的高度始终为72,但没有任何图像时,它的高度必须为零。我的表格单元格中有两个UIView,其中第一个包含一个UIImageView,而第二个包含其中的两个。我要隐藏还是显示相关的图片,具体取决于该单元格包含一幅还是两幅图像。如果该单元格的图像为零,则两个UIView都将被隐藏,但是它们当然仍然是设置的约束的一部分,因此最终的单元格仍为72像素高,而我需要将其折叠。

有人告诉我,创建高度可调整大小的UITableViewCell的唯一方法是在情节提要中设置约束,而以编程方式即时处理约束值会引起布局问题。我不知道任何建议是否正确,因此我尝试了以下操作(均未成功):

  • 设置对我的UIViewNSLayoutConstraint的显式引用,该引用控制我的ResizableImageCell调用中的高度,这会导致Unable to simultaneously satisfy constraints错误;和
  • 设置对所有NSLayoutConstraint的显式引用,这些引用可控制ResizableImageCell调用中的高度;和/或
  • 添加和删除UIView,这两种方法都可以正常工作,直到您开始滚动表格为止,然后在将手指从屏幕上抬起之后表格开始上下跳跃。

  • 这是控制ViewController的当前代码:
    import UIKit
    
    class ViewController: UIViewController
    {
        @IBOutlet weak var tableView: UITableView!
    
        let imageNames = [ "room", "street", "tree" ]
    
    
    
        override func viewDidLoad ()
        {
            super.viewDidLoad()
    
            tableView.delegate = self
            tableView.dataSource = self
            tableView.rowHeight = UITableViewAutomaticDimension
            tableView.estimatedRowHeight = 44.0
        }
    
    
    
        func getRandomInt ( from start: Int, to end: Int ) -> Int
        {
            return Int( arc4random_uniform( UInt32( ( end + 1 ) - start ) ) ) + start
        }
    }
    
    
    
    extension ViewController: UITableViewDataSource
    {
        @available( iOS 2.0, * )
        public func tableView ( _ tableView: UITableView, cellForRowAt indexPath: IndexPath ) -> UITableViewCell
        {
            let cell = tableView.dequeueReusableCell( withIdentifier: "ResizableImageCell" ) as! ResizableImageCell
            let numImages = getRandomInt( from: 0, to: 2 )
    
            cell.view1.isHidden = numImages != 1
            cell.view2.isHidden = numImages != 2
    
            var rand = getRandomInt( from: 0, to: imageNames.count - 1 )
    
            switch numImages
            {
            case 1:     cell.image1.image = UIImage( named: imageNames[ rand ] )
            case 2:     cell.image2.image = UIImage( named: imageNames[ rand ] )
            default:    ()
            }
    
            guard numImages == 2 else { return cell }
    
            rand = getRandomInt( from: 0, to: imageNames.count - 1 )
    
            cell.image3.image = UIImage( named: imageNames[ rand ] )
    
            return cell
        }
    
    
    
        func tableView ( _ tableView: UITableView, numberOfRowsInSection section: Int ) -> Int
        {
            return 64
        }
    }
    

    这是控制我定制的ResizableImageCell单元的当前代码
    import UIKit
    
    class ResizableImageCell: UITableViewCell
    {
        @IBOutlet weak var view1: UIView!
        @IBOutlet weak var view2: UIView!
    
        @IBOutlet weak var image1: UIImageView!
        @IBOutlet weak var image2: UIImageView!
        @IBOutlet weak var image3: UIImageView!
    
    
    
        override func awakeFromNib ()
        {
            super.awakeFromNib()
        }
    }
    

    这是现有情节提要的屏幕截图:

    ios - 如何设置具有0到2张图像的UITableViewCells上可调整高度的约束?-LMLPHP

    感谢您的阅读。

    最佳答案

    我通过使用heighForRow函数解决了这个问题。

    在ViewController中:

     func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    
        //Check if you have to display images or not
    
        //return 75 or CGFloat.ulpOfOne
    }
    

    也许使用此解决方案,您必须在let numImages = getRandomInt( from: 0, to: 2 )函数之前创建cellForRowAtIndexPath并将64个结果存储在Array中。

    编辑:
    Josh Homann的重要说明:

    不要返回0的高度,否则您的rect会退化,因此控制台中可能会出现一些自动布局错误。返回CGFloat.ulpOfOne,它很小,但仍然不为零。

    关于ios - 如何设置具有0到2张图像的UITableViewCells上可调整高度的约束?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41505470/

    10-14 16:58
    查看更多