Swift - 如何在UITableViewCell内部使用CollectionViewCell打开另一个viewcontroller [英] Swift - how to open another viewcontroller with CollectionViewCell inside UITableViewCell

查看:244
本文介绍了Swift - 如何在UITableViewCell内部使用CollectionViewCell打开另一个viewcontroller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很新的iOS / Swift,我在一个小项目。在这个项目中,我在ViewController里面有一个UITableView。而我还有另一个文件定制的CollectionViewCell在UITableViewCell的边。
当用户单击collectionview中的单元格时,我将要打开另一个ViewController,并从该collectionviewcell获取数据。
这是我的uitableview swift文件:

  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!
覆盖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(命名为poster.jpg)
cell.name.text = names [indexPath.row]
返回单元格
}

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
如果让navVC:UINavigationController = UIApplication.sharedApplication()。keyWindow?.rootViewController as? UINavigationController {
navVC.pushViewController(distinationViewController,animated:true)
}
}

这只是一种方法,我不知道你想要推送哪个视图或者你的名称数组包含什么,所以请相应地更改这些内容。



从uiapplication获取root navigationcontroller并执行推送。


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)
}

How i can do it?

解决方案

You can do it like this :

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.

get root navigationcontroller from uiapplication and perform push on it.

这篇关于Swift - 如何在UITableViewCell内部使用CollectionViewCell打开另一个viewcontroller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆