PerfomSegue在UITableViewCell类中
我有TableView
和TableView
里面我已经添加了UICollectionView
。 现在,当用户点击任何UICollectionView
,我想传递数据到下一个viewController
但不幸的是,我不能使用UITableViewCell类中执行segue,我已经select了项目。 有什么方法可以在这里执行吗?
整个代码
class UserLibraryViewController: UIViewController { extension UserLibraryViewController : UITableViewDelegate { } extension UserLibraryViewController : UITableViewDataSource { func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return myBooksGenre[section] } func numberOfSectionsInTableView(tableView: UITableView) -> Int { return myBooksGenre.count } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 1 } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("userBooksTableViewCell") as! UserLibraryTableViewCell cell.tableIndexPath = indexPath return cell } }
TableViewCell
class UserLibraryTableViewCell: UITableViewCell { @IBOutlet weak var collectionView: UICollectionView! var tableIndexPath: NSIndexPath? } extension UserLibraryTableViewCell : UICollectionViewDataSource { func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 12 } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier("userBooksCollectionViewCell", forIndexPath: indexPath) as! UserLibraryCollectionViewCell return cell } func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { print("\(self.tableIndexPath!.section) ---- \(indexPath.item) \(tableSectionHeader[self.tableIndexPath!.section]) ") } } extension UserLibraryTableViewCell : UICollectionViewDelegateFlowLayout { func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { let itemsPerRow:CGFloat = 4 let hardCodedPadding:CGFloat = 5 let itemWidth = (collectionView.bounds.width / itemsPerRow) - hardCodedPadding let itemHeight = collectionView.bounds.height - (2 * hardCodedPadding) return CGSize(width: itemWidth, height: itemHeight) } }
创build一个协议,然后在TableViewCell
中创build它的实例并在UserLibraryViewController
实现该协议,然后在CollectionView
的didSelectItemAtIndexPath
中使用该委托实例调用此方法。
protocol CustomCollectionDelegate { func selectedCollectionCell(indexPath: NSIndexPath) } class UserLibraryTableViewCell: UITableViewCell { @IBOutlet weak var collectionView: UICollectionView! var tableIndexPath: NSIndexPath? var delegate: CustomCollectionDelegate? }
现在在CollectionView
的didSelectItemAtIndexPath
中调用这个方法。
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { print("\(self.tableIndexPath!.section) ---- \(indexPath.item) \(tableSectionHeader[self.tableIndexPath!.section]) ") self.delegate?.selectedCollectionCell(indexPath) }
现在在UserLibraryViewController
实现这个协议,并在cellForRowAtIndexPath
设置委托。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("userBooksTableViewCell") as! UserLibraryTableViewCell cell.tableIndexPath = indexPath cell.delegate = self return cell } extension UserLibraryViewController : CustomCollectionDelegate { func selectedCollectionCell(indexPath: NSIndexPath) { self.performSegueWithIdentifier("Identifier", sender: indexPath) } }
注意:我已经添加了NSIndexPath
作为参数的协议方法,您可以使用自定义对象或Dictionary/Array
对其进行更改。
- iOS Swift:UIPageViewController – 以编程方式翻页
- 使用Swift和Firebase更改用户电子邮件和密码
- iOS7 UITextView contentsize.height替代
- 多播委托 – 从iOS中的UI控件接收通知的多个类
- 从数据库Firebase中删除用户时出错
- 如何检测键盘类型的变化与新键盘类型的大小,类型,建议栏高度?
- 控制video(MPMoviePlayerController)在视图中的位置
- 如何用UINavigationController和Interface Builder创build一个UIViewController?
- 在CoreData中使用ValueTransformer加密用户的敏感信息