我有一些麻煩,從一個collectionView以編程方式顯示viewController(沒有故事板)。我會認爲這很容易,但我有一些困難,因爲我對swift有點新穎。我相信collectionViewCells默認情況下不是委託,因此我嘗試在collectionView類中實現didSelectItemAt,但仍然沒有運氣。點擊該單元格時,我收到一個打印語句,只是沒有任何顯示繼續。請參閱下面的collectionViewCell代碼,並提前致謝!從collectionView顯示viewController沒有故事板
// collectionViewCell class
class PlanningCell: BaseCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.dataSource = self
cv.delegate = self
return cv
}()
//collectionViewCell variables
var plannedPlaces = [CurrentPlanner]()
let cellId = "cellId"
var basePlanningCell: BasePlanningCell!
override func setupViews() {
super.setupViews()
addSubview(collectionView)
collectionView.register(BasePlanningCell.self, forCellWithReuseIdentifier: cellId)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return plannedPlaces.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! BasePlanningCell
cell.currentPlanner = plannedPlaces[indexPath.item]
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("did tap")
let plannersDetailVC = PlanningPlaceDetailsVC()
plannersDetailVC.navigationTitle = plannedPlaces[(indexPath.row)].planningPlace
// below I am receiving the use of unresolved identifier "show" error
show(plannersDetailVC, sender: self)
}
}
謝謝,但它現在給我的錯誤消息「價值的類型'PlanningCell'沒有會員'存在'」 – user3708224
@ user3708224更新了我的答案 –
好的,謝謝。我會研究這些選項。 – user3708224