2016-08-28 76 views
-1

我正在爲我想要的應用程序製作獎章頁面。我的應用程序設置的方式是,當點擊一枚獎章時,將打開一張包含該獎章圖片的頁面。如何在標題欄中設置標題,基於從xcode中的collectionview單元查看哪個單元格ios

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


} 

謝謝您H ELP

編輯:

這就是我想要的金牌頁面看起來像點擊時: enter image description here

+0

那麼你的代碼有什麼問題? – ozgur

+0

我想爲每枚獎牌的導航欄上的每個獎章和自定義標題添加說明。 –

+0

把描述放在哪裏?請明確點。您可以向我們展示您希望UI的外觀如何。 – ozgur

回答

0

首先在獎牌創造的medalDescriptions陣列的ViewController然後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中更改navbar標題

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 

} 

對於未來的不只是他們的名字添加視圖控制器或查看在最後命名的控制器或其他類。例如,MedalDetailViewController。這將是更好的理解和閱讀

+0

嗨,我打算做的是根據顯示在其中的圖像的名稱設置獎章詳細視圖控制器中的標題欄。 –

+0

你的意思是圖片的標題? – aatalyk

+0

是圖像的標題 –

0

沒關係,我找到了另一種方式來做到這一點。我爲每個獎牌實例化了單獨的視圖控制器,因此每個視圖控制器可以具有不同的屬性。

相關問題