Home > OS >  Swift: TableView is showing some of the arrays data but not all
Swift: TableView is showing some of the arrays data but not all

Time:11-24

I have two tableviews inside my stack view. I am resizing them depending on the amount of data that is retrieved from Firestore. The issue I am facing is whilst the tableview is resize the top table view "ingredientsTV" shows all the data where as the "instructionsTV" only shows some of the data. My array.count displays the correct number of items in the array but them items are not getting displayed.

//Code for resize tableviews

 override func viewWillLayoutSubviews() {
        super.updateViewConstraints()
        self.ingredientsTVHeight?.constant = self.ingredientsTV.contentSize.height
        self.instructionsTVHeight.constant = self.instructionsTV.contentSize.height
        self.ingredientsTV.contentInset = UIEdgeInsets(top: 0, left: -20, bottom: 0, right: 0)
        self.instructionsTV.contentInset = UIEdgeInsets(top: 0, left: -20, bottom: 0, right: 0)
    }

//setupview, called in viewdidload
    //MARK: Functions
    private func setupView() {
        ingredientsTV.delegate = self
        ingredientsTV.dataSource = self
        instructionsTV.delegate = self
        instructionsTV.dataSource = self
        recipeImage.layer.cornerRadius = 5
        recipeNameLbl.text = recipe.name
        prepTimeLbl.text = recipe.prepTime
        cookTimeLbl.text = recipe.cookTime
        servesLabel.text = recipe.serves

        if let url = URL(string: recipe.imageUrl) {
            recipeImage.kf.setImage(with: url)
            recipeImage.layer.cornerRadius = 5
        }
        
    }

//MARK: Tableview functions
extension PocketChefRecipeDetailsVC {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if (tableView == self.ingredientsTV) {
            return recipe.ingredients.count
        }else {
            return recipe.method.count
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if (tableView == self.ingredientsTV) {
            let cell = ingredientsTV.dequeueReusableCell(withIdentifier: "ingredientsCell", for: indexPath) as? ingredientsCell
            
            cell?.ingredientsLbl.text = recipe.ingredients[indexPath.row]
            
            return cell!
        }else {
            let cellB = instructionsTV.dequeueReusableCell(withIdentifier: "instructionsCell", for: indexPath) as? InstructionsCell
            
            cellB?.instructionsLbl.text = recipe.method[indexPath.row]
            return cellB!
        }
    }
}

*Recipe data is getting passed from previous view controller

CodePudding user response:

I'm going to take a shot in the dark and say that maybe your stack view needs to be re laid out after you reload data.

Make sure to call

// after ingredientsTV.reloadData() and instructionsTV.reloadData() gets called

stackview.setNeedsLayout()

CodePudding user response:

You can try adding a fixed height to each row and see if it has any populated data or not. Add this to your extension

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100
}

Additionally, you can also set the background color of the cell in cellForRowAt() just to ensure the rows are visible.

Note: Do check if your stack view's constraints are set for all 4 sides.

  • Related