【发布时间】: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