微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将collectionview的索引路径访问到tableview

如何解决如何将collectionview的索引路径访问到tableview

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

我想将collectionview的相同索引路径传递给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("")
}

}

解决方法

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

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

func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {

     return "Your array .count"
 }

在collectionView上的 didSelect 委托方法中,将数据从 addvenderArray [indexPath.row] 发送到“您的数组” ,然后重新加载表格视图。 / p>

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必须从该盘中获取实例并提供结构

,

看起来您只需要移动此代码

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

进入集合视图的didDeselectRowAt方法

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。