如何解决如何快速将collectionview单元格索引值传递给nextviewcontroller使用推送导航
我在 tableview 单元格中使用 collectionview,
我需要将选定的 collectionview 单元格值传递给下一个视图控制器,如何?
代码:这里是tableview和collectionview单元格的代码
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CategoryNewTableCell",for: indexPath) as! CategoryNewTableCell
let indexData = self.activeCategories?[indexPath.row]
cell.selectionStyle = .none
cell.catNameLbl.text = indexData?.details?.first?.title
cell.subCategories = indexData?.sub_categories
cell.clcSeller.reloadData()
}
class CategoryNewTableCell: UITableViewCell,UICollectionViewDelegate,UICollectionViewDataSource{
@IBOutlet weak var clcSeller: UICollectionView!
public var subCategories : Array<Sub_categories>?
func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
return subCategories?.count ?? 0
}
func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SubCatCollectionCell",for: indexPath) as! SubCatCollectionCell
let subCategory = self.subCategories?[indexPath.item]
cell.lblTitle.text = langType == .en ? subCategory?.details?.first?.title : subCategory?.details?[1].title
return cell
}
func collectionView(_ collectionView: UICollectionView,didSelectItemAt indexPath: IndexPath) {
let vc = StoryBoard.main.instantiateViewController(withIdentifier: "SearchResultVC") as! SearchResultVC
vc.subCatId = sub_categories?[indexPath.row].slug ?? ""
self.navigationController?.pushViewController(vc,animated: true)
}
}
在这里,如果我使用 didSelectItemAt
作为 collectionview 将其选定的单元格值发送到下一个视图控制器
错误:
“CategoryNewTableCell”类型的值没有成员“navigationController”
如果我在主类中给按钮动作然后能够推送但价值不去
class CategoryNewVC: UIViewController {
@IBAction func didselectcollectionviewBTn(_ sender: UIButton) {
let vc = StoryBoard.main.instantiateViewController(withIdentifier: "SearchResultVC") as! SearchResultVC
vc.subCatId = //here how to pass value
self.navigationController?.pushViewController(vc,animated: true)
}
}
这里如何将选定的 collectionview 单元格值传递给 SearchResultVC
请帮忙
编辑
根据我添加的以下答案:仍然 didSelectItemAt
没有被调用,为什么请帮忙
class CategoryNewTableCell: UITableViewCell,UICollectionViewDataSource{
override func awakeFromNib() {
super.awakeFromNib()
self.clcSeller.delegate = self
self.clcSeller.dataSource = self
//
解决方法
您可以使用协议将数据发送到您的 nextviewcontroller。
protocol CategorySelectionDelegate {
func get(category: Sub_categories)
}
在您的 CategoryNewTableCell 中声明委托,并在您的 collectionviewcell 的 didSelectItemAt 方法中使用它,如下所示:
class CategoryNewTableCell:
UITableViewCell,UICollectionViewDelegate,UICollectionViewDataSource{
@IBOutlet weak var clcSeller: UICollectionView!
public var subCategories : Array<Sub_categories>?
var delegate: CategorySelectionDelegate?
func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
return subCategories?.count ?? 0
}
func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SubCatCollectionCell",for: indexPath) as! SubCatCollectionCell
let subCategory = self.subCategories?[indexPath.item]
cell.lblTitle.text = langType == .en ? subCategory?.details?.first?.title : subCategory?.details?[1].title
return cell
}
func collectionView(_ collectionView: UICollectionView,didSelectItemAt indexPath: IndexPath) {
delegate?.get(category: sub_categories?[indexPath.row])
}
}
采用接收类中的协议
class CategoryNewVC: UIViewController,CategorySelectionDelegate {
func get(category: Sub_categories) {
let vc = StoryBoard.main.instantiateViewController(withIdentifier: "SearchResultVC") as! SearchResultVC
vc.subCatId = category.slug ?? ""
self.navigationController?.pushViewController(vc,animated: true)
}
}
,
- 在 tableViewCell 子类中声明回调。
FnOnce
- 像这样在您的
class CategoryNewTableCell: UITableViewCell { var onSelectSubcategory: ((_ subcategoryID: String) -> Void)? }
中分配此回调。
cellForRow
- 像这样从 collectionView
cell.subCategories = indexData?.sub_categories cell.onSelectSubcategory = { [weak self] (subcategoryID) in let vc = StoryBoard.main.instantiateViewController(withIdentifier: "SearchResultVC") as! SearchResultVC vc.subCatId = subcategoryID self?.navigationController?.pushViewController(vc,animated: true) }
调用此回调。
didSelectItem
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。