【问题标题】:Swift: TableView is showing some of the arrays data but not allSwift:TableView 显示了一些数组数据,但不是全部
【发布时间】:2022-01-01 12:51:32
【问题描述】:

我的堆栈视图中有两个表视图。我正在根据从 Firestore 检索到的数据量调整它们的大小。我面临的问题是,当表格视图调整顶部表格视图的大小时,“ingredientsTV”显示了所有数据,而“instructionsTV”只显示了一些数据。我的 array.count 显示了数组中正确数量的项目,但这些项目没有显示出来。

//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!
        }
    }
}

*配方数据是从前一个视图控制器传递过来的

【问题讨论】:

    标签: ios swift google-cloud-firestore tableview


    【解决方案1】:

    您可以尝试为每一行添加一个固定高度,看看它是否有任何填充数据。将此添加到您的扩展程序中

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

    此外,您还可以在cellForRowAt() 中设置单元格的背景颜色,以确保行可见。

    注意:请检查是否为所有 4 个面设置了堆栈视图的约束。

    【讨论】:

      【解决方案2】:

      我打算在黑暗中试一试,说也许你的堆栈视图需要在你重新加载数据后重新布局。

      一定要打电话

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

      【讨论】:

      • 您的堆栈视图是如何布局的?你是在代码中还是在 IB 中这样做的?如果您是在 IB 中完成的,您可以发送限制条件的屏幕截图吗?
      • 只是为了调试它,尝试将顶部、前导、尾随和底部约束固定到视图控制器的视图。
      猜你喜欢
      • 2020-11-27
      • 1970-01-01
      • 1970-01-01
      • 2018-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-12
      • 1970-01-01
      相关资源
      最近更新 更多