Swift - 如何在 UITableViewCell 中使用 CollectionViewCell 打开另一个视图控制器

Swift - how to open another viewcontroller with CollectionViewCell inside UITableViewCell(Swift - 如何在 UITableViewCell 中使用 CollectionViewCell 打开另一个视图控制器)
本文介绍了Swift - 如何在 UITableViewCell 中使用 CollectionViewCell 打开另一个视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的是 iOS/Swift 的新手,而且我在做一个小项目.在这个项目中,我在 ViewController 中有一个 UITableView.我在 UITableViewCell 中有另一个文件自定义 CollectionViewCell.我希望当用户单击 collectionview 中的一个单元格时,它将打开另一个 ViewController 并从此 collectionviewcell 获取数据.这是我的 uitableview swift 文件:

I'm really new in iOS/Swift and i'm in a small project. In this project i have a UITableView inside ViewController. And i have another file custom CollectionViewCell in side UITableViewCell. I want when user click a cell in collectionview it will open another ViewController and it get data from this collectionviewcell. This is my uitableview swift file:

class IndexRow: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
var names:[String] = ["Movie 1","Movie 2","Movie 3","Movie 4","Movie 5","Movie 6"]
@IBOutlet weak var collectionView: UICollectionView!
override func awakeFromNib() {
    super.awakeFromNib()
    collectionView.registerClass(indexOneMovie.self, forCellWithReuseIdentifier: "onemovie")
    let nib = UINib(nibName: "indexOneMovie",bundle: nil)
    collectionView.registerNib(nib, forCellWithReuseIdentifier: "onemovie")
    self.collectionView.backgroundColor = UIColor.clearColor()
    self.collectionView.delegate = self
    self.collectionView.dataSource = self
    print("Hello")
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return names.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = self.collectionView.dequeueReusableCellWithReuseIdentifier("onemovie", forIndexPath: indexPath) as! indexOneMovie
    cell.poster.image = UIImage(named: "poster.jpg")
    cell.name.text = names[indexPath.row]
    return cell
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    print(indexPath.item)
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    let itemsPerRow:CGFloat = 2
    let hardCodedPadding:CGFloat = 0
    let itemWidth = (collectionView.bounds.width / itemsPerRow) - hardCodedPadding
    let itemHeight = collectionView.bounds.height - (hardCodedPadding)
    return CGSize(width: itemWidth, height: itemHeight)
}

我该怎么做?

推荐答案

你可以这样:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    print(indexPath.item)
    let name = names[indexPath.item]
    let distinationViewController = DistinationViewController()
    distinationViewController.name = name
    if let navVC: UINavigationController = UIApplication.sharedApplication().keyWindow?.rootViewController as? UINavigationController {
    navVC.pushViewController(distinationViewController, animated: true)
    }
}

这只是一种方法,我不知道您要推送哪个视图或您的名称数组包含什么,因此请相应地更改这些内容.

This is just a way to do it i dont know which view you want to push or what your names array contains so kindly change those things accordingly.

从 uiapplication 获取 root navigationcontroller 并对其执行推送.

get root navigationcontroller from uiapplication and perform push on it.

这篇关于Swift - 如何在 UITableViewCell 中使用 CollectionViewCell 打开另一个视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)