我的 Collection View 包含多个以编程方式创建的自定义单元格类。有趣的部分是我将有一个包含另一个 collectionview 的单元格。那么如何点击进入这个collectionview来呈现一个新的view controller呢?下面是关于如何创建单元格的示例代码。
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 **
}
那么对于这个设置,如何点击单元格并在数组中呈现一个带有用户信息的新 Controller ?
最佳答案
您必须在内部 collectionViewCell 的 didSelect 上将委托(delegate)或命中通知传递给 viewController,并且在该方法中您可以呈现新的 View Controller 。
查看链接
Swift: How to use "didSelectItemAtIndexPath" in UITableViewController(contains UICollectionView)?
关于ios - UIcollectionview Custom Cell class to doselectitem at,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50315012/