如何使用从 Parse 获取的字符串填充 UILabel 文本

How to populate UILabel text with Strings fetched from Parse

我在 Parse 中有一些字符串 class 我想获取并填充一些 UILabel 的文本:

我在自定义 class 中实现了以下方法来查询这些字符串:

class Book: NSObject {

    var query = PFQuery(className: "Books")
    var titleString: String!


    func fetchTitleString(){
        query.whereKeyExists("Title")
        query.findObjectsInBackgroundWithBlock {
            (objects: [AnyObject]?, error: NSError?) -> Void in

            if error == nil {
                // The find succeeded.
                println("Successfully retrieved \(objects!.count) book Titles.")
                // Do something with the found objects
                if let objects = objects as? [String] {
                    for object in objects {
                        self.titleString = object
                    }
                }
            } else {
                // Log details of the failure
                println("Error: \(error!) \(error!.userInfo!)")
            }
        }
    }
}

Now the UILabels I need to populate are in a detailViewController that gets pushed to the view in a collectionView:didSelectItemAtIndexPath when a cell is selected.这是代码:

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        let cell = collectionView.cellForItemAtIndexPath(indexPath) as! MyCollectionViewCell

        var books : Array<Book>!

        let book = self.books[indexPath.row]
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewControllerWithIdentifier("DetailViewController") as! DetailViewController

            controller.titleSelected = book.titleString
            self.navigationController?.pushViewController(controller, animated: true)

        println("user tapped on thumbnail # \(indexPath.row)")

    }

那个 titleSelected 字符串变量表示我的 detailViewController 中的 UILabels 文本之一。它会用从我的解析查询中获取的任何字符串对象来填充标签文本。但是当我 运行 时它只是空白。我应该做什么或调整以实现我想要做的事情?

更新

这是我的 detailViewController:

class DetailViewController: UIViewController, ARNImageTransitionZoomable {

    @IBOutlet weak var imageView: UIImageView!

    @IBOutlet weak var titleLabel: UILabel!

    var titleSelected: String!

    deinit {
        println("deinit DetailViewController")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        imageView.layer.cornerRadius = 3.0
        imageView.clipsToBounds = true


    }

    override func viewWillAppear(animated: Bool) {
        self.titleLabel.text = titleSelected

    }

我认为您的问题是您在这一行的开头将 PFObject as String 强制转换

 if let objects = objects as? [String]

将此更改为

 if let objects = objects as? [PFObject]
 {
   for oneObj in objects
   {
    var titleFromParse = oneObj["Title"] as! String
       // then do whatever you want with titleFromParse
       // for example insert to array:  self.books.append(titleFromParse)
         // reloadData() for the collectionView
   }

 } 

而且我认为这行没有必要

 query.whereKeyExists("Title")

而且我认为你应该全局声明这一行

var books: Array<book>!

回答 从 NSObject 中移除查询函数 class**

 func fetchTitleString()
{
    let query = PFQuery(className: "Books")
     query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) -> Void in

        if error == nil
        {
            if let objects = objects as? [PFObject]
            {
                 print("Successfully retrieved \(objects.count) book Titles.")

                    for oneObj in objects
                    {
                            let titleFromParse = oneObj["Title"] as! String
                            let SingleBook = Book()
                            SingleBook.titleString = titleFromParse
                            self.arrayOfStrings.append(SingleBook)

                    } 


               }
        }

        else
        {
            // Log details of the failure 
            print("Error: \(error!) \(error!.userInfo)")
        } 
    } 
}