如何根据xcode ios中的集合视图单元格查看哪个单元格在标题栏中设置标题

时间:2016-08-28 17:37:49

标签: ios swift xcode view

我正在为我想到的应用制作奖牌页面。我设置了这样的应用程序,当点击一枚奖牌时,它会打开一个包含该奖章图片的页面。

enter image description here

如果可能的话,我还希望在他们的视图控制器中添加每个奖章的描述。我如何实现这一目标?

以下是我的代码示例:

Medal.swift

import UIKit

class Medals: UICollectionViewController

{

    // Defining the array
    let imageArray = [UIImage(named: "1"), UIImage(named: "2")]

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

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

    //Allows you to populate the cells you've created
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        // cell was from the identifier we named earlier

        // we let collectionviewcell handle the connection to UIcollectionviewcell
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CollectionViewCell

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


        return cell

    }

    // How many cells do we want to have inside the collection view? Just count the number of images assigned
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageArray.count
    }

    override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        self.performSegueWithIdentifier("showImage", 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! MedalDetail
            vc.image = self.imageArray[indexPath.row]!
        }
    }

}

CollectionViewCell.swift

import UIKit

class CollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var imageView: UIImageView!


}

medaldetails.swift

import UIKit

class MedalDetail: UIViewController {

    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var setTitle: UINavigationItem!

    var image = UIImage()
    var medalTitle = UINavigationItem()

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

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


}

谢谢你的帮助

编辑:

这就是我想要点击时奖牌页面的样子: enter image description here

2 个答案:

答案 0 :(得分:0)

首先在Medals ViewController中创建medalDescriptions数组,然后在didSelectItemAtIndexPath调用performSegue到MedalDetail viewController

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    let index = indexPath.row
    self.performSegueWithIdentifier("SegueMedalDetail", sender: nil)
}

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

        let medalDetailViewController = segue.destinationViewController as! MedalDetail
        medalDetailViewController.medalDescription = medalDescriptions[index]
    }
}

在MedalDetail中创建medalDescription并在同一个viewController中更改导航栏标题

override func viewDidAppear(animated: Bool) {
    self.navigationController?.navigationBarHidden = false
    self.navigationItem.title = medalDescription

    self.navigationController!.navigationBar.tintColor = UIColor.whiteColor()
    let attributes = [
        NSForegroundColorAttributeName: UIColor.whiteColor(),
        NSFontAttributeName: UIFont(name: "Helvetica", size: 50)!
    ]
    self.navigationController?.navigationBar.titleTextAttributes = attributes

}

为了将来不要仅仅使用名称命名控制器或其他类,最后添加ViewController或View。例如,MedalDetailViewController。理解和阅读

会更好

答案 1 :(得分:0)

没关系,我发现了另一种方法。我为每个奖牌实例化了单独的视图控制器,因此我可以为每个视图控制器提供不同的属性。