【问题标题】:How to set title in title bar based on which cell is viewed from collectionview cells in xcode ios如何根据从xcode ios中的collectionview单元格查看的单元格在标题栏中设置标题
【发布时间】: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 的外观。
  • 已编辑,注意标题名称的变化以及图标下方添加的一些描述

标签: ios swift xcode view


【解决方案1】:

首先在 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。这样会更好理解和阅读

【讨论】:

  • 您好,我打算根据其中显示的图像名称设置奖牌详细视图控制器中的标题栏。
  • 你的意思是图片的标题?
  • 是图片的标题
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-17
    • 1970-01-01
    • 2015-11-30
    • 2015-02-14
    相关资源
    最近更新 更多