【发布时间】:2016-08-28 17:37:48
【问题描述】:
我正在为我想到的应用制作奖牌页面。我有这样一种方式设置应用程序,当点击奖牌时,它会打开一个包含该奖牌图片的页面。
如果可能,我还希望在他们的视图控制器中为每个奖牌添加描述。我该如何做到这一点?
这是我的代码示例:
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.
}
}
感谢您的帮助
编辑:
【问题讨论】:
-
那么你的代码有什么问题?
-
我想为每个奖牌添加描述,并在导航栏上为每个奖牌添加自定义标题。
-
把描述放在哪里?请明确点。您可以向我们展示您希望 UI 的外观。
-
已编辑,注意标题名称的变化以及图标下方添加的一些描述