swift - 表视图委托内的集合视图
问题描述
我在表格行中有一个带有集合视图的表格视图。接下来是结构:
MainViewController.swift:
class MainViewController: UIViewController {
@IBOutlet weak var customTable: UITableView!
func callSegue() {
performSegue(withIdentifier: "customSegue", sender: self)
}
override func viewDidLoad() {
customTable(UINib(nibName: "CustomTableCell", bundle: nil), forCellReuseIdentifier: "TipsTableCell")
}
}
extension MainViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableCell", for: indexPath) as! CustomTableCell
//Fill cell with my data
return cell
}
}
CustomTableCell.swift
class CustomTableCell.swift: UITableViewCell {
@IBOutlet var collectionView: UICollectionView!
override func awakeFromNib() {
super.awakeFromNib()
self.collectionView.dataSource = self
self.collectionView.delegate = self
self.collectionView.register(UINib.init(nibName: "CustomTableCell", bundle: nil), forCellWithReuseIdentifier: "CustomTableCell")
}
}
extension CustomTableCell: UICollectionViewDataSource, UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return dataArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomTableCell", for: indexPath) as! CustomTableCell
cell.label1.text = dataArray[indexPath.item]
return cell
和我的 CustomCollectionvCell.swift
class CustomCollectionvCell: UICollectionViewCell {
@IBOutlet weak var label1: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
我需要这样的东西:当我点击 label1.text == "Something" 的单元格时,我需要在 MainViewController 中调用 "callSegue" 函数。
解决方案
用来closures
解决。
添加一个closure
in并在点击 in方法CustomTableCell
时调用它,即collectionViewCell
collectionView(_:didSelectItemAt:)
class CustomTableCell: UITableViewCell {
var handler: (()->())?
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.handler?()
}
}
在MainViewController
,设置closure
while出队CustomTableCell
方法tableView(_:cellForRowAt:)
,即
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableCell", for: indexPath) as! CustomTableCell
cell.handler = {[weak self] in
self.callSegue() //here.....
}
return cell
}
还要交叉检查segue
您customSegue
的storyboard
.
推荐阅读
- arduino - 在软串行上运行 HM10 是否启用低能耗特性?如果是这样,在这种情况下如何调整 UUID 和 GATT 配置文件?
- security - docker-machine(通用驱动程序)将TCP端口2376打开到互联网是否安全?
- c# - 如何通过在 ASP.NET WebForms 中选择下拉值向现有表的新可空列添加记录?
- c# - 如何将光标设置在绑定到数据表的datagrid中空行的第一个单元格上
- recursion - 在 F# 中使用免费的 monad 是否意味着更长的启动时间和有限的指令?
- php - 使用重定向时我无法将数据存储到会话
- php - Laravel 5.4 用户配置文件 NotFoundHttpException
- magento2 - Magento 2 + 如何在顶部位置包含 Bootstrap CSS
- asp.net - 使用 http 链接提取 Windows 文件属性
- javascript - 滚动“水平滚动的 div 元素”也会使父滚动