Dynamic UITableView with images

前端 未结 1 1584
深忆病人
深忆病人 2021-01-21 10:59

There are similar questions, but non of the answers worked for me. In a dynamic table I want to display images that have different heigh. Each cell has a UIImageView

相关标签:
1条回答
  • 2021-01-21 11:31

    In your ImageTableViewCell.swift

    import UIKit
    
    class ImageTableViewCell: UITableViewCell {
    
        @IBOutlet weak var customImageView: UIImageView!
    
        internal var aspectConstraint : NSLayoutConstraint? {
            didSet {
                if oldValue != nil {
                    customImageView.removeConstraint(oldValue!)
                }
                if aspectConstraint != nil {
                    customImageView.addConstraint(aspectConstraint!)
                }
            }
        }
    
        override func prepareForReuse() {
            super.prepareForReuse()
            aspectConstraint = nil
        }
    
        func setPostedImage(image : UIImage) {
    
            let aspect = image.size.width / image.size.height
    
            aspectConstraint = NSLayoutConstraint(item: customImageView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: customImageView, attribute: NSLayoutAttribute.height, multiplier: aspect, constant: 0.0)
    
            customImageView.image = image
        }
    }
    

    And into your TableTableViewController.swift your cellForRowAt method will be:

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ImageTableViewCell", for: indexPath) as! ImageTableViewCell
    
        let image = imageArr[indexPath.row]
        cell.setPostedImage(image: image!)
        return cell
    }
    

    And declare your imageArr this way:

    let imageArr = [UIImage(named: "1.jpg"), UIImage(named: "2.jpg")]
    

    And your compete code will be:

    import UIKit
    
    class TableTableViewController: UITableViewController {
    
        let imageArr = [UIImage(named: "1.jpg"), UIImage(named: "2.jpg")]
        override func viewDidLoad() {
            super.viewDidLoad()
    
            tableView.estimatedRowHeight = 100
            tableView.rowHeight = UITableViewAutomaticDimension
        }
    
        override func numberOfSections(in tableView: UITableView) -> Int {
    
            return 1
        }
    
        override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
            return 2
        }
    
        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "ImageTableViewCell", for: indexPath) as! ImageTableViewCell
    
            let image = imageArr[indexPath.row]
            cell.setPostedImage(image: image!)
            return cell
        }
    }
    

    And THIS will be your result.

    EDIT:

    To fix constraint issue set aspectConstraint priority to 999 and aspectConstraint will be:

    internal var aspectConstraint : NSLayoutConstraint? {
        didSet {
            if oldValue != nil {
                customImageView.removeConstraint(oldValue!)
            }
            if aspectConstraint != nil {
                aspectConstraint?.priority = 999  //add this
                customImageView.addConstraint(aspectConstraint!)
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题