A
A
Aleksandr Govorukhin2017-08-24 07:05:04
Swift
Aleksandr Govorukhin, 2017-08-24 07:05:04

How to make UIImageView dynamic in swift 3?

Hello!
The question is, in fact, in the title. There are images different in width and height, how to generate a cell depending on the size of the image?

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! MainTableViewCell
        
        let myRecipe = recipe[indexPath.row]
        
        let img = NSKeyedUnarchiver.unarchiveObject(with: myRecipe.imageRecipe! as Data) as! NSArray
        
        cell.nameRecipe.text = myRecipe.nameRecipe
        cell.imageRecipe.image = UIImage(data: img[0] as! Data)
        

        return cell
    }

288957b3b1ef4ad5a88e46c28525e274.pngebc5fcdd7b3341e3a4fe652b9c363959.png
And that is not quite a beautiful display.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Tikhonov, 2017-08-26
@SnapSh0t

Friend, it's that simple.
First, viewDidLoadyou make the table dynamic cell size

override func viewDidLoad() {  
    super.viewDidLoad()

    self.tableView.estimatedRowHeight = 80 // примерная высота ячейки
    self.tableView.rowHeight = UITableViewAutomaticDimension // определяет высоту в зависимости от контента
}

Next, you arrange the image's constraints so that they are attached to the edges of the cell
KI5ky.pngQuestion on stackoverflow

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question