单击 UICollectionViewCell 时如何转换到新控制器(以编程方式)
How to transition to a new controller when clicking on UICollectionViewCell (programatically)
我已经在导航控制器内设置了一个带有 UICollectionViewCells 的 ViewController。我希望能够单击单元格,然后根据选择的单元格(每个单元格有不同的控制器)将用户带到一个新的控制器。我希望导航栏仍然出现在新控制器中,并有一个后退按钮,可以将用户带回到原来的 ViewController。我在初始视图控制器中有以下代码来设置集合视图单元格:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: playlistCellId, for: indexPath) as! playlistCoverCell
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 100, height: 100)
}
我还在 viewDidLoad 中正确注册了单元格。选择单元格时使用什么函数执行操作?
你必须使用:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.row == 0 {
let viewController = UIViewController() // or your custom view controller
self.navigationController?.pushViewController(viewController, animated: true)
}
else if indexPath.row == 1 {
// and so on....
}
}
Tells the delegate that the item at the specified index path was
selected. The collection view calls this method when the user
successfully selects an item in the collection view. It does not call
this method when you programmatically set the selection.
你可以试试函数中的UICollectionViewDelegate
enter image description here
可以使用indexPath获取当前点击的元素;
推送到下一个 viewController 你必须有 navigationViewController,如果 navigationController 是 nil,你可以尝试协议或块。抱歉,我的英文不好,可能语法有误。
我已经在导航控制器内设置了一个带有 UICollectionViewCells 的 ViewController。我希望能够单击单元格,然后根据选择的单元格(每个单元格有不同的控制器)将用户带到一个新的控制器。我希望导航栏仍然出现在新控制器中,并有一个后退按钮,可以将用户带回到原来的 ViewController。我在初始视图控制器中有以下代码来设置集合视图单元格:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: playlistCellId, for: indexPath) as! playlistCoverCell
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 100, height: 100)
}
我还在 viewDidLoad 中正确注册了单元格。选择单元格时使用什么函数执行操作?
你必须使用:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.row == 0 {
let viewController = UIViewController() // or your custom view controller
self.navigationController?.pushViewController(viewController, animated: true)
}
else if indexPath.row == 1 {
// and so on....
}
}
Tells the delegate that the item at the specified index path was selected. The collection view calls this method when the user successfully selects an item in the collection view. It does not call this method when you programmatically set the selection.
你可以试试函数中的UICollectionViewDelegate enter image description here
可以使用indexPath获取当前点击的元素; 推送到下一个 viewController 你必须有 navigationViewController,如果 navigationController 是 nil,你可以尝试协议或块。抱歉,我的英文不好,可能语法有误。