【问题标题】:Duplicates in tableview section Swift表视图部分 Swift 中的重复项
【发布时间】:2021-05-30 00:19:53
【问题描述】:

我正在尝试将一些数据分类到 tableView 中。披萨归入披萨类别,汉堡归入汉堡等,但我每个项目都重复(看这里Picture。可能是什么问题?

结构:

struct Food {
    
    var photoKeyRestaurant: String
    var foodName: String
    var foodDescription: String
    var restaurantName: String
    var priceFood: Int
    var typeFood: String
    
    
}

表格视图

func numberOfSections(in tableView: UITableView) -> Int {
        return food.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return food[section].foodName.count
        
        
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "FoodTableViewCell", for: indexPath) as! FoodTableViewCell
        
        
        
        //   cell.delegate = self
        let mancare = food[indexPath.section]
        let storageRef = Storage.storage().reference()
        let photoRef = storageRef.child(mancare.photoKeyRestaurant)
        cell.foodImage.sd_setImage(with: photoRef)
        cell.descriptionLabel.text = mancare.foodDescription
        cell.foodNameLabel.text = mancare.foodName
        cell.priceLabel.text = "\(mancare.priceFood) lei"
        cell.foodImage.layer.borderWidth = 1
        cell.foodImage.layer.masksToBounds = false
        cell.foodImage.layer.borderColor = UIColor.black.cgColor
        cell.foodImage.layer.cornerRadius = cell.foodImage.frame.height/2
        cell.foodImage.clipsToBounds = true
        
        //Fac ca imaginea sa fie cerc  - finish
        return cell
}
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 120
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
    
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return food[section].typeFood
    }

如果有人问,我正在从 Firestore 中检索数据,但那里没有问题,因为数据已成功检索。

有人知道可能是什么问题吗?谢谢

【问题讨论】:

    标签: swift tableview


    【解决方案1】:

    有趣的是,您的cellForRowAt 代码实际上从未提及indexPath.row。它仅指indexPath.section。如何获取和显示与 this 行相对应的数据,如果您不以某种方式参考 的问题,您应该在cellForRowAt 中配置这些数据>这是什么行?

    也许还有一些我看不到的其他机制正在发生,但在我看来,仅这一事实就解释了为什么每个部分只是一遍又一遍地显示相同的数据,而不是每一行的不同数据。

    【讨论】:

      【解决方案2】:

      这段代码有两个大问题。

      1. 节的数据数组有误。
      2. 在函数cellForRowAt 中你不能返回indexPath.section

      首先。您需要使用二维数组,例如:

      var foodCategorys: [FoodCategory] = [[categoryName, Food],[categoryName, Food]]
      

      所以你需要另一个结构来包含这个二维数组:

      struct FoodCategory {
          var categoryName: String
          var listFood: [Food] = [] // Food is the struct that you currently have
      }
      

      所以tableView中显示的数据是一个数组FoodCategory。使用 numberOfSections 将返回数组 FoodCategory 的计数。 numberOfRowsInSection 会将 listFood 的计数返回到 FoodCatogory。

      func numberOfSections(in tableView: UITableView) -> Int {
         return foodCategorys.count //array of listFood
      }
      
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
          return foodCategorys[section].listFood.count
      }
      

      第二个,在cellForRowAt,当你想获取列表数据的索引时。您需要致电foods[indexPath.section].listFood[indexPath.row]。函数需要row 而不是section

      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
              let cell = tableView.dequeueReusableCell(withIdentifier: "FoodTableViewCell", for: indexPath) as! FoodTableViewCell
              //cell.delegate = self
              let mancare = foods[indexPath.section].listFood[indexPath.row] //Remeber that, the array food we are using in this line is array of FoodCategory
      
      

      我认为你需要用关键词“uitableview swift中的部分”进行研究,或者观看这个视频https://www.youtube.com/watch?v=AHY09z-XS9s

      所以,就是这样。我希望你能理解 tableview 和 section 是如何工作的

      【讨论】:

      • 您好,感谢您的回复,我在声明 foodCategorys 时出错:“在范围内找不到 'categoryName'”
      • 嗨,[[categoryName, Food],[categoryName, Food]],我只是描述一下这种情况下的二维数组。
      • 您好[[categoryName, Food],[categoryName, Food]],我只是描述了在这种情况下二维数组的情况。您需要了解您将向 tableView 显示列表类别(例如:列表类别有 2 个项目,包括:咖啡和比萨饼)。对于咖啡项目,您的 categoryName 是“咖啡”,并在该项目中列出食物,例如:“capuchino,expreso”。使用 TableView,section 将显示列表类别,row 将显示此类别中的食物列表。如果我所说的让您感到困惑,那么您可以观看视频链接并重写代码。它适用于您的情况。
      猜你喜欢
      • 2019-06-07
      • 1970-01-01
      • 2018-12-26
      • 1970-01-01
      • 1970-01-01
      • 2016-01-06
      • 1970-01-01
      • 2016-09-27
      • 1970-01-01
      相关资源
      最近更新 更多