sk1*_*123 1 uitableview pushviewcontroller ios uicollectionviewcell swift
我在 tableView 单元格中有一个 collectionView。我想点击 collectionView 单元格并使用传入的信息转到 detailTableView。我没有很多工作。做了一些研究,它指出 tableView 单元格既没有故事板也没有导航控制器。我如何能够将数据传递到 detailTableView。这是我的实现,可能不正确
class PopularTableViewCell: UITableViewCell {
var books = [CKRecord]()
var detailViewController: DetailViewController!
}
extension PopularTableViewCell: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let record = books[indexPath.row]
let storyBoard = UIStoryboard(name: "Home", bundle: nil)
if let detailVC = storyBoard.instantiateViewController(withIdentifier: "DetailVC") as? DetailViewController {
detailVC.bookRecord = record
detailViewController.navigationController?.pushViewController(detailVC, animated: true)
}
}
}
Run Code Online (Sandbox Code Playgroud)
您应该从包含 tableview 的根视图控制器推送视图控制器:
self.navigationController.pushViewController(detailVC, animated: true)
Run Code Online (Sandbox Code Playgroud)
集合视图中的数据可以通过自定义委托传递,例如:
protocol CustomDelegate: class {
func didSelectItem(record: CKRecord)
}
weak var delegate: CustomDelegate?
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let record = books[indexPath.row]
delegate?.didSelectItem(record: record)
}
Run Code Online (Sandbox Code Playgroud)
然后在 tableview 单元格中另一个委托:
extension TableViewCell: CustomDelegate {
func didSelectItem(record: CKRecord) {
delegate?.didSelectItem(record: record)
}
}
Run Code Online (Sandbox Code Playgroud)
最后在视图控制器中:
extension ViewController: CustomDelegate {
func didSelectItem(record: CKRecord) {
let storyBoard = UIStoryboard(name: "Home", bundle: nil)
if let detailVC = storyBoard.instantiateViewController(withIdentifier: "DetailVC") as? DetailViewController {
detailVC.bookRecord = record
self.navigationController?.pushViewController(detailVC, animated: true)
}
}
}
Run Code Online (Sandbox Code Playgroud)
PS:不要忘记连接代表,例如:
cell.delegate = self
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2704 次 |
| 最近记录: |