Ios UIcollectionview自定义单元格类到位于的didselectitem
my collectionview包含多个以编程方式创建的自定义单元格类。有趣的是,我将有一个单元格,其中包含另一个collectionview。那么,如何单击此collectionview以显示新的视图控制器呢?下面是关于如何创建单元格的示例代码Ios UIcollectionview自定义单元格类到位于的didselectitem,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,my collectionview包含多个以编程方式创建的自定义单元格类。有趣的是,我将有一个单元格,其中包含另一个collectionview。那么,如何单击此collectionview以显示新的视图控制器呢?下面是关于如何创建单元格的示例代码 class MainView: UIViewController { ** main view to display UIcollectionview ** let cell = collectionView.dequeueReusableCell(
class MainView: UIViewController {
** main view to display UIcollectionview **
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: userCellIdent, for: indexPath) as! UserContainerViewCell
cell.Users = users * to display users array*
return cell
}
class UserContainerViewCell: UICollectionViewCell{
* the uicollectionview that inside a cell , also contains extension to UIcollectioview datasource and delegate to display the cells within this cell*
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ident, for: indexPath) as! userCell
let user = featureUsers![indexPath.row]
cell.config(withUser: user)
return cell
}
}
class userCell: UICollectionViewCell {
func config(withUser: UserProfile) { }
** cell class to display the info from the user array **
}
因此,通过此设置,如何单击单元格并在阵列中显示包含用户信息的新控制器?您可以尝试
class UserContainerViewCell: UICollectionViewCell {
var myInstance: MainView!
}
class MainView: UIViewController {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: userCellIdent, for: indexPath) as! UserContainerViewCell
cell.myInstance = self
}
}
然后在didSelectItemAtRow中
您必须在内部collectionViewCell的didSelect上向viewController传递委托或hit通知,并且在该方法中,您可以呈现新的视图控制器 查看链接
兄弟,有什么样的代码或指南吗?因为我对swiftI非常陌生,我已经编辑了它。这将告诉你如何传递一个委托。你必须用两种方式来做。第一种是将委托传递给上层单元格类,然后传递给vc。希望你成功。嗨,我想我不想发表主要观点。我想展示一个新的视图,可以显示我传入的用户信息。
let vc = myInstance.storyboard?.instantiateViewController(withIdentifier: "infoView") as! InfoController
vc.providesPresentationContextTransitionStyle = true;
vc.definesPresentationContext = true;
vc.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
myInstance.present(vc, animated: false, completion: nil)