我試圖從位于 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 方法中呼叫委托函式。但是委托回傳零。
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。這個設定有什么問題。請幫幫我。
uj5u.com熱心網友回復:
執行以下簡單步驟,這可能會對您有所幫助
1- 在您的cellForRowAt方法中將委托分配給您的單元格
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let customCell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as! CustomTableViewCell
customCell.delegate = self
return customCell
}
2- 在您的CustomTableViewCell類中,使用以下代碼更新您的didSelectItemAt方法
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let del = self.delegate
{
del.passTheCurrent(tableIndex: 0, collectionViewIndex: indexPath.item)
}
}
基本上我們現在正在做的第一件事是將委托分配給每個索引上的單元格。第二個表格單元格類首先我們檢查委托確認然后將資料傳遞給父控制器類。??
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/372530.html
