getJuiceDescription方法总是返回相同的结果

时间:2016-01-11 01:38:00

标签: ios swift

我正在尝试为警报对话框中的每个特定点击设置自定义字符串。它适用于标题,但是当使用相同的代码尝试在警报对话框中放置自定义消息时它不起作用,它总是返回" Iono"。我的代码下面是我正在谈论的图片。

的ViewController

import UIKit

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

@IBOutlet var collectionView: UICollectionView!

var galleryItems: [GalleryItem] = []
var listOfDescriptions: [String] = []
var listOfJuiceDescriptions: [String] = []

// MARK: -
// MARK: - View Lifecycle

override func viewDidLoad() {
    super.viewDidLoad()
    initGalleryItems()
    collectionView.reloadData()
    populateList()
    self.view.backgroundColor = UIColor.lightGrayColor()
}

private func initGalleryItems() {

    var items = [GalleryItem]()
    let inputFile = NSBundle.mainBundle().pathForResource("items", ofType: "plist")

    let inputDataArray = NSArray(contentsOfFile: inputFile!)

    for inputItem in inputDataArray as! [Dictionary<String, String>] {
        let galleryItem = GalleryItem(dataDictionary: inputItem)
        items.append(galleryItem)
    }

    galleryItems = items
}
private func populateList() {
    listOfDescriptions.append("Valhaha");
    listOfDescriptions.append("Unicorns blood");
    listOfDescriptions.append("Rasberry");
        listOfDescriptions.append("Orange");
}
private func getDescription(position: Int) -> String {
    if (position<listOfDescriptions.count) {
    return listOfDescriptions[position]
    } else {
        return "Custom Label"
    }
}

private func populateJuices() {
    listOfJuiceDescriptions.append("warm vanilla")
    listOfJuiceDescriptions.append("Fruit Punch")
    listOfJuiceDescriptions.append("uhh rasberry nigga")
    listOfJuiceDescriptions.append("so you never tasted an orange before?")
}

private func getJuiceDescription(position: Int) -> String {
    if (position<listOfJuiceDescriptions.count) {
        return listOfJuiceDescriptions[position]
    }
    else {
        return "Iono"
    }
}

// MARK: -
// MARK: - UICollectionViewDataSource

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return galleryItems.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("GalleryItemCollectionViewCell", forIndexPath: indexPath) as! GalleryItemCollectionViewCell

    cell.setGalleryItem(galleryItems[indexPath.row])
    cell.itemNameLabel.text=getDescription(indexPath.row)
    return cell

}

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

    let commentView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "GalleryItemCommentView", forIndexPath: indexPath) as! GalleryItemCommentView

    commentView.commentLabel.text = "Supplementary view of kind \(kind)"

    return commentView
}

// MARK: -
// MARK: - UICollectionViewDelegate

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    let alert = UIAlertController(title: getDescription(indexPath.row), message: getJuiceDescription(indexPath.row), preferredStyle: .Alert)

    let alertAction = UIAlertAction(title: "Dismiss", style: .Destructive, handler: nil)
    alert.addAction(alertAction)

    self.presentViewController(alert, animated: true, completion: nil)
}

// MARK: -
// MARK: - UICollectionViewFlowLayout

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    let picDimension = self.view.frame.size.width / 4.0
    return CGSizeMake(picDimension, picDimension + 31)
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
    let leftRightInset = self.view.frame.size.width / 14.0
    return UIEdgeInsetsMake(0, leftRightInset, 0, leftRightInset)
}
}

enter image description here

1 个答案:

答案 0 :(得分:1)

您正在调用getJuicesDescription函数来获取说明。此函数从listOfJuiceDescriptions数组中获取描述。 listOfJuiceDescriptions数组是在populateJuices函数中创建的。

但是,我没有在代码中看到调用populateJuices的任何地方。我在populateList中看到viewDidLoad的来电,但没有致电populateJuices

这意味着当调用getJuicesDescription时,数组为空,而if语句返回Iono