多个CollectionView - Swift

时间:2016-05-31 01:02:28

标签: ios swift uicollectionview

我现在正在努力工作5个小时,我已经改变了很多代码(我还是初学者,这也是我学习swift行为的方式),但我现在被困住了,我真的需要你的帮助。

我有一个小应用程序,有3个按钮,但其中2个segue到不同(单独)CollectionView(在同一页面上不是两个collectionView,相同的视图控制器,但在故事板内的不同视图控制器上)。

enter image description here

当我第一次创建CollectionView时,一切正常,但是在我获得第二个之后,第一个仍然可以正常工作,但第二个只是给我空白的黑屏(在模拟器中)。

我试图通过更改代码中的大量内容来解决这个问题,而且我从来没有遇到任何错误,我可以在模拟器中正常运行应用程序,但在第二个Collection View上总是会出现黑屏。

在第二个CollectionView中我和第一个相同(连接按钮,插座,ViewControllers与我创建的类),但它没有在第二个CollectionView中显示图像,所以可能错误就是在ViewController中的某处.swift和我现在正在寻找和更改代码几个小时,但没有运气,所以我问你是否可以请求帮助。

希望我没有搞砸太多:

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

@IBOutlet weak var collectionView: UICollectionView!

@IBOutlet weak var collectionViewTrening: UICollectionView!

let imageArray = [UIImage(named: "slika1"),
                  UIImage(named: "slika2"),
                  UIImage(named: "slika3"),
                  UIImage(named: "slika4"),
                  UIImage(named: "slika5"),
                  UIImage(named: "slika6"),
                  UIImage(named: "slika7"),
                  UIImage(named: "slika8"),
                  UIImage(named: "slika9"),
                  UIImage(named: "slika10"),
                  UIImage(named: "slika11"),
                  UIImage(named: "slika12"),
                  UIImage(named: "slika13")]

let vjezbe = ["Zgibovi: 30 sekundi", "Trbušnjaci: 30 sekundi"]

let vjezbeSlike = [UIImage(named: "Vjezba-zgibovi"),
                   UIImage(named: "Vjezba-trbusnjaci")]


override func viewDidLoad()  {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.imageArray.count
}

func collectionViewTrening(collectionViewTrening: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.vjezbeSlike.count
}


func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CollectionViewCell

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

    return cell

}

func collectionViewTrening(collectionViewTrening: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionViewTrening.dequeueReusableCellWithReuseIdentifier("cellTrening", forIndexPath: indexPath) as! TreningCollectionViewCell

    cell.imageViewTrening?.image = self.vjezbeSlike[indexPath.row]

    return cell

}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    self.performSegueWithIdentifier("showImage", sender: self)
}

func collectionViewTrening(collectionViewTrening: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    self.performSegueWithIdentifier("showImageTrening", sender: self)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showImage" {

        let indexPaths = self.collectionView!.indexPathsForSelectedItems()!
        let indexPath = indexPaths[0] as NSIndexPath
        let vc = segue.destinationViewController as! NewViewController
        vc.image = self.imageArray[indexPath.row]!

   } else if segue.identifier == "showImageTrening" {
        let indexPaths = self.collectionViewTrening!.indexPathsForSelectedItems()!
        let indexPath = indexPaths[0] as NSIndexPath
        let vc  = segue.destinationViewController as! TreningViewController
        vc.imageTrening = self.vjezbeSlike[indexPath.row]!
    }
}

}

感谢。 ;)

0 个答案:

没有答案