【发布时间】:2022-01-08 19:34:50
【问题描述】:
我正在尝试从 Tableview 单元格内的 Collectionview 单元格导航到另一个 ViewController。
我正在尝试使用委托方法来实现,但它没有导航到预期的视图控制器。
这是我迄今为止开发的代码。我在这里使用 xib 设置。
// ViewController.swift
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate,CustomTableViewCellDelegate {
@IBOutlet weak var tableView: UITableView!
var customTableViewCell = CustomTableViewCell()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
customTableViewCell.delegate = self
tableView.delegate = self
tableView.dataSource = self
self.tableView.register(UINib(nibName: "CustomTableViewCell", bundle: nil), forCellReuseIdentifier: "CustomTableViewCell")
}
override func viewWillAppear(_ animated: Bool) {
navigationController?.navigationBar.barTintColor = UIColor.black
navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]
self.navigationController?.navigationBar.tintColor = UIColor(red: 211/255, green: 86/255, blue: 50/255, alpha: 1.0)
}
//Delegate method
func passTheCurrent(tableIndex: Int, collectionViewIndex: Int) {
print("collectionViewIndex \(collectionViewIndex)")
let selectpile = ObjectSceneViewCtrl()
self.navigationController?.pushViewController(selectpile, animated: true)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 120
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let customCell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as! CustomTableViewCell
return customCell
}
}
这是我定义委托方法的 CustomTableViewCell。我在 collectionview didSelectItemAt 方法中调用委托函数。但是委托返回 nil。
import UIKit
protocol CustomTableViewCellDelegate {
func passTheCurrent(tableIndex: Int, collectionViewIndex: Int)
}
class CustomTableViewCell: UITableViewCell {
@IBOutlet weak var collectionView: UICollectionView!
var delegate: CustomTableViewCellDelegate?
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
self.collectionView.dataSource = self
self.collectionView.delegate = self
self.collectionView.register(UINib.init(nibName: "CollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "CollectionViewCell")
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
extension CustomTableViewCell : UICollectionViewDelegate {}
extension CustomTableViewCell : UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 15
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath as IndexPath) as! CollectionViewCell
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
delegate?.passTheCurrent(tableIndex: 0, collectionViewIndex: indexPath.item)
}
}
当我设置断点时,委托返回 nil。这个设置有什么问题。请帮帮我。
【问题讨论】:
标签: ios swift uitableview uicollectionview appdelegate