【问题标题】:how to access index path of the collectionview into tableview如何在tableview中访问collectionview的indexpath
【发布时间】:2020-08-18 10:24:55
【问题描述】:

我在视图控制器中有一个collectionview 和tableview。 collectionview 包含厨师的集合,它位于屏幕的顶部。Collection 视图是水平滚动的。 Tableview 包含厨师提供的食物。 问题是 Tableview 显示了所有厨师提供的所有菜肴。 我只想在 tableview 中显示所选厨师的食物。

我想将collectionview的同一个indexpath传递给tableview视图如何实现。因为我使用的两个数组都是同一结构的一部分

我的代码如下:

扩展 MenuViewController : UITableViewDelegate,UITableViewDataSource{

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
         return menuArray1.count
     }
   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let  cell = menuTableview.dequeueReusableCell(withIdentifier: "MenuTableViewCell", for: indexPath) as? MenuTableViewCell
    cell?.menuNameLabel.text = menuArray1[indexPath.row].prodName
    cell?.discountLabel.text = menuArray1[indexPath.row].offerPercent
    cell?.menuQuantityLabel.text = menuArray1[indexPath.row].menuDescription
    cell?.originalPriceLabel.text = menuArray1[indexPath.row].originalPrice
    cell?.discountPriceLabel.text = menuArray1[indexPath.row].sellingPrice
    cell?.vegNonVegImageview.image = #imageLiteral(resourceName: "veg_thumb")
    cell?.menuImage.setImageFromURl(stringImageUrl: menuArray1[indexPath.row].prodImg)
    cell?.timeToDeliverLabel.text = menuArray1[indexPath.row].preparationTime
  //  cell?.menuImage.setImageFromURl(stringImageUrl: menuArray[indexPath.row].prodImg)

    
    cell?.menuImage.setImageFromURl(stringImageUrl: menuArray1[indexPath.row].prodImg)
    cell?.addButton.layer.borderWidth = 1
    cell?.addButton.layer.borderColor = UIColor.red.cgColor

    cell?.selectionStyle = .none

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

// 返回 self.view.frame.height * 0.33 返回 self.view.frame.height * 0.12

}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
     print("chiefFlag\(chiefFlag)")
}

} 集合视图代码:

扩展 MenuViewController : UICollectionViewDelegate,UICollectionViewDataSource{

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return addvenderArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "menuCollectionViewCell", for: indexPath)
    as! menuCollectionViewCell
    
    
    menuTableview.reloadData()
    
    cell.nameLabel.text = addvenderArray[indexPath.row].venderName
    cell.profileImage.setImageFromURl(stringImageUrl: addvenderArray[indexPath.row].vendorImg)
    cell.ratingLabel.text = addvenderArray[indexPath.row].rating
    cell.ratingImageView.image = #imageLiteral(resourceName: "chef_rating")
    cell.profileImage.makeCircular()
   
    cell.profileImage.backgroundColor = .white
    
    **var obj = addvenderArray[indexPath.row]
    self.menuArray1 = obj.menu
    menuTableview.reloadData()**
  

  **// Above code is my logic  addvenderArray structure contains the menuArray1.But it gets null value**
    
    return cell
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    print("")
}

}

【问题讨论】:

    标签: ios swift collections tableview


    【解决方案1】:

    使用要发送到表视图的特定数据模型实例创建一个数组。

    当在 api 调用后出现此所需屏幕时,将第一个索引数据发送到该数组并重新加载表视图。

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
         return "Your array .count"
     }
    

    在 collectionView 上的 didSelect 委托方法将数据从 addvenderArray[indexPath.row] 发送到 "Your Array" 并重新加载 tableview。

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let  cell = menuTableview.dequeueReusableCell(withIdentifier: "MenuTableViewCell", for: indexPath) as? MenuTableViewCell
    cell?.menuNameLabel.text = YourArray[indexPath.row].prodName
    

    }

    YourArray 必须从 dishes 中获取实例并提供结构

    【讨论】:

      【解决方案2】:

      看起来你只需要移动这段代码

      var obj = addvenderArray[indexPath.row]
      self.menuArray1 = obj.menu
      menuTableview.reloadData()
      

      进入你的集合视图的 didDeselectRowAt 方法

      【讨论】:

        猜你喜欢
        • 2018-09-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多