我正在爲我想要的應用程序製作獎章頁面。我的應用程序設置的方式是,當點擊一枚獎章時,將打開一張包含該獎章圖片的頁面。如何在標題欄中設置標題,基於從xcode中的collectionview單元查看哪個單元格ios
如果可能的話,我也想在添加說明每一個金牌在他們看來控制器。我如何實現這一目標?
下面的代碼我有一個樣本:
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
編輯:
那麼你的代碼有什麼問題? – ozgur
我想爲每枚獎牌的導航欄上的每個獎章和自定義標題添加說明。 –
把描述放在哪裏?請明確點。您可以向我們展示您希望UI的外觀如何。 – ozgur