将集合视图单元链接到新控制器

时间:2017-01-11 09:43:36

标签: ios swift uicollectionview

我以编程方式创建了CollectionView并将图像传递给它们。现在,在点击每个图像视图时,我需要分别打开下一个每个视图控制器。我是Swift的新手,我在Swift 3中找不到可能的解决方案。 我试图为第四个“todolistbutton”imageview做这件事,但它给我看错了。任何人都可以帮助我在Swift 3? 我的代码是:

import UIKit

class MeBottomViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {



@IBOutlet weak var collectionView: UICollectionView!


let imageArray = [UIImage(named: "guidebutton"), UIImage(named: "mybellybutton"), UIImage(named: "appointmentbutton"), UIImage(named: "todolistbutton"), UIImage(named: "duedatebutton"), UIImage(named: "myweightbutton")]


override func viewDidLoad() {
    super.viewDidLoad()

    let tap = UITapGestureRecognizer(target: self, action: #selector(tappedme))
    imageArray[3].addGestureRecognizer(tap)    // Error: Value of type 'UIImage?' has no member 'addGestureRecognizer'

    imageArray[3].userInteractionEnabled = true   // Error: Value of type 'UIImage?' has no member 'userInteractionEnabled'


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return 6
}


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cells = collectionView.dequeueReusableCell(withReuseIdentifier: "cells",  for: indexPath as IndexPath ) as! SecondCollectionViewCell

    cells.imageView?.image = self.imageArray[indexPath.row]

    return cells
}

func tappedme() {


    let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "ToDoTableViewController") as! ToDoTableViewController

    self.navigationController?.pushViewController(secondViewController, animated: true)


}
}

3 个答案:

答案 0 :(得分:0)

无法UITapGestureRecognizer添加到UIImage。所以你需要  在 UITapGestureRecognizer 上添加UIImageView,如下所示:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cells = collectionView.dequeueReusableCell(withReuseIdentifier: "cells",  for: indexPath as IndexPath ) as! SecondCollectionViewCell
    cells.imageView?.image = self.imageArray[indexPath.row]

    // Tap gesture
    let tap = UITapGestureRecognizer(target: self, action: #selector(tappedme))
    cells.imageView!.addGestureRecognizer(tap)

    return cells
}

然后让空viewDidLoad

override func viewDidLoad() {
    super.viewDidLoad()
}

答案 1 :(得分:0)

  

你不需要设置任何手势

    import UIKit

class MeBottomViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {



@IBOutlet weak var collectionView: UICollectionView!


let imageArray = [UIImage(named: "guidebutton"), UIImage(named:    "mybellybutton"), UIImage(named: "appointmentbutton"), UIImage(named:  "todolistbutton"), UIImage(named: "duedatebutton"), UIImage(named:  "myweightbutton")]


override func viewDidLoad() {
super.viewDidLoad()




}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

return 6
}


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cells = collectionView.dequeueReusableCell(withReuseIdentifier: "cells",  for: indexPath as IndexPath ) as! SecondCollectionViewCell

cells.imageView?.image = self.imageArray[indexPath.row]

return cells
}


}



func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) 

 { 

let secondViewController =   self.storyboard?.instantiateViewController(withIdentifier: "ToDoTableViewController") as! ToDoTableViewController 

self.navigationController?.pushViewController(secondViewController, animated: true)


}

答案 2 :(得分:0)

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) 

 { 

let secondViewController =   self.storyboard?.instantiateViewController(withIdentifier: "ToDoTableViewController") 

?.pushViewController(secondViewController, animated: true)
}

@ matloob Hasnain。现在它终于奏效了。我不得不删除! ToDoTableViewController。