gpt4 book ai didi

ios - 如何从 UICollectionView Cell 转到另一个 ViewController Swift

转载 作者:行者123 更新时间:2023-11-28 12:06:18 25 4
gpt4 key购买 nike

假设我有一个用户单元。我需要为此用户启动一个 ChatViewController。所以,我有 UICollectionView 显示为原始(1 个单元格 = 1 行)。如何从单元格转到 ChatViewController?

我根本不使用 InterfaceBuilder。只能通过代码方式。

我计划使用 didSelectItemAt,但我没有足够的快速编程技能来弄清楚如何使用此函数来达到我的目的。当我尝试在 didSelectItemAt 函数中编写 prepare for segueperformSegue with Identifier 时,Xcode 没有提供正确的自动完成功能。我的代码如下:

import UIKit

class MyList: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

override init(frame: CGRect) {
super .init(frame: frame)
setUpLocalizedUserList()

}


//Click on User Cell
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("Click")

}


//Localized User List (Collection View)
private func setUpLocalizedUserList(){
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 20, right: 0)
layout.minimumLineSpacing = 0
layout.itemSize = CGSize(width: UIScreen.main.bounds.width, height: 80)

let userListFrame = CGRect(x: 0, y: 0, width: Int(UIScreen.main.bounds.width), height: Int(UIScreen.main.bounds.height) - 160)
let myCollectionView:UICollectionView = UICollectionView(frame: userListFrame, collectionViewLayout: layout)

myCollectionView.dataSource = self
myCollectionView.delegate = self
myCollectionView.register(LocalizedUserCell.self, forCellWithReuseIdentifier: "userCell")
myCollectionView.register(GroupChatCell.self, forCellWithReuseIdentifier: "groupCell")
myCollectionView.backgroundColor = UIColor.white

addSubview(myCollectionView)
}

//Number of Section in User List
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2
}

//Lists' Sizes
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return (section == 0) ? 5 : 4
}

//Cells content here
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

if indexPath.section == 0 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "userCell", for: indexPath) as! LocalizedUserCell
return cell
}else{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "groupCell", for: indexPath) as! GroupChatCell
return cell
}
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

也许这很重要 - 我的用户单元格在另一个单元格中。

最佳答案

您不能从 inSide collectionViewCell 进行转场,因为当前 func 需要一个 UIViewController ,请在此处查看我对表格单元格的回答,相同的逻辑 TableCellNavigate

关于ios - 如何从 UICollectionView Cell 转到另一个 ViewController Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49303191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com