Saturday, June 25, 2022
HomeiOS Developmentios - Delay when dynamically adjusting desk peak

ios – Delay when dynamically adjusting desk peak


I’m presently beginning to discover ways to construct my very own iOS Apps with Swift. My present undertaking ought to assist set up recipes. For this, the App shows a desk with the some dishes (inside a Desk View Controller firstly). As soon as the consumer clicks at a cell, the small print of that dish (from my CoreDate) must be displayed inside a View Controller embedded inside a scroll view. To date so good… That is how my VieController for the clicked dish ought to appear to be:

Storyboard Picture of my element View

Inside this View Controller, I’ve a desk with the components wanted for the dish. This desk view ought to embed all of the cells which describe the components, so long as they collectively don’t exceed a maximal peak threshold of 300. On this case, all dishes are seen with out scrolling. If a peak of 300 will not be sufficient to show all components, the peak of the desk must be set to 300 and the consumer can scroll via the desk of components. So the peak of the desk relies on the dimensions and content material of the cells, which is why I can not simply outline it on the very begin (at ViewDidLoad).

Now, once I click on on the dish of my selection, the next display screen seems:

Instantly after clicking dish

And after ready possibly half a second, the view is up to date appropriately:

Up to date Display screen

So the issue is: the label “Zubereitung” proper beneath my desk solely seems with a sure delay. I think about that could be as a result of the display screen masses and initially works with the preliminary worth I set my desk peak to be contained in the storyboard(being 300). So the label “Zubereitung” stays 300 beneath the desk. Solely once I alter the peak of the desk (on this case to a price beneath 300) can my label take its desired place.

That is why I needed to ask, how I may keep away from this downside. Or if there’s a smarter or higher solution to alter the desk dimension dynamically with the content material of my desk? Presently I do it by overwriting the peak constraint contained in the perform ViewDidAppear.

The code of the ViewController representing the chosen dish is the next:

import UIKit

class MyDishViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    
    @IBOutlet weak var nameOfDish: UILabel!
    @IBOutlet weak var imageOfDish: UIImageView!
    @IBOutlet weak var categoryOfDish: UILabel!
    @IBOutlet weak var complexityOfDish: UILabel!
    @IBOutlet weak var time: UILabel!
    
    @IBOutlet weak var ingredientTable: UITableView!
    
    @IBOutlet weak var tableIngredientHeight: NSLayoutConstraint!
    
    var dish : Dish? = nil

    override func viewDidLoad() {
        tremendous.viewDidLoad()
        
        ingredientTable.delegate = self
        ingredientTable.dataSource = self
        
        ingredientTable.rowHeight = UITableView.automaticDimension
        ingredientTable.estimatedRowHeight = 60//0
        
        if let textual content = dish?.nameDish{
            nameOfDish.textual content = textual content
        }
        
        if let knowledge = dish?.imageDish {
            imageOfDish.picture = UIImage(knowledge: knowledge)
        }
        
        if let class = dish?.class{
            categoryOfDish.textual content = class
        }
        
        if let complexity = dish?.complexity{
            complexityOfDish.textual content = complexity
        }
    }
    
    
    override func viewDidAppear(_ animated: Bool) {
        tremendous.viewDidAppear(animated)
        let peak = self.ingredientTable.contentSize.peak
        if peak > 300 {
            self.tableIngredientHeight.fixed = 300
        }
        else{
            self.tableIngredientHeight.fixed = peak
        }
    }
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection part: Int) -> Int {
        if let components = dish?.components{
            let strains = components.cut up(whereSeparator: .isNewline)
            return strains.rely
        }
        else{
            return 0
        }
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                
        let cell = tableView.dequeueReusableCell(withIdentifier: "ingredientCell", for: indexPath) as! listingTableViewCell
        
        if let components = dish?.components{
            let strains = components.cut up(whereSeparator: .isNewline)
            let merchandise = strains[indexPath.row]
            cell.merchandise?.textual content = String(merchandise)
        }
        return cell
    }
}


Thanks very a lot!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments