【问题标题】:How to show Parse array in a TableView in Swift Xcode 6.4如何在 Swift Xcode 6.4 的 TableView 中显示 Parse 数组
【发布时间】:2015-07-10 13:20:48
【问题描述】:

如何将对象数组放入 theArray 以便在 tableView 中显示结果?由于查询是异步的,我不能简单地说 theArray = objects。那么,如何从查询中截取数组呢?

编辑:

Ashish 的回答解决了这个问题,但由于 Xcode 的建议,我不得不稍微更改一下代码。

现在,我试图打开它,因为我需要,

var new = object.objectId
println("first: \(new)")
if let tryToUnwrap = new as? NSString {
println("second: \(tryToUnwrap)")
}

我向下转换为 NSString 因为在 Swift 中字符串不是警告:从字符串转换?到不相关的类型'NSString'总是失败

代码有效,但它是什么意思?我怎么能摆脱这个?

import UIKit

import Parse


class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var myTableView: UITableView!

    var theArray : [PFObject] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        //MARK: PFQUERY
        //***********************************************************************
        var query = PFQuery(className:"TestObject")

        query.findObjectsInBackgroundWithBlock {
            (objects: [AnyObject]?, error: NSError?) -> Void in

            if error == nil {
                // The find succeeded.
                println("Successfully retrieved \(objects!.count) scores.")

                // Do something with the found objects

                self.theArray = (objects as? [PFObject])!

                if self.theArray.count > 0 {
                    self.myTableView.reloadData()
                }

                if let objects = objects as? [PFObject] {
                    for object in objects {

//                        var new = object.objectId
//                        println("first: \(new)")
//                        
//                        if let tryToUnwrap = new as? NSString {
//                            println("second: \(tryToUnwrap)")
//                        }

                        println("here the object id: \(object.objectId)")

                    }
                }
            } else {
                // Log details of the failure
                println("Error: \(error!) \(error!.userInfo!)")
            }
        }
        //***********************************************************************
        //        if let string = dict.objectForKey("SomeKey") as? String {
        //            // If I get here, I know that "SomeKey" is a valid key in the dictionary, I correctly
        //            // identified the type as String, and the value is now unwrapped and ready to use.  In
        //            // this case "string" has the type "String".
        //            println(string)
        //        }

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



    //MARK: UITableViewDataSource (required to conform to UITableViewDataSource)
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return theArray.count

    }

    //MARK: UITableViewDataSource (required to conform to UITableViewDataSource)
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        var cell : TaskCell = tableView.dequeueReusableCellWithIdentifier("myCell") as! TaskCell

        cell.objectIdLabel.text = theArray[indexPath.row].objectId

        return cell
    }


    //MARK: UITableViewDelegate (not required)
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        println("section is: \(indexPath.section) row is: \(indexPath.row)")
    }

}

【问题讨论】:

    标签: arrays swift uitableview parse-platform


    【解决方案1】:

    获取后重新加载表格即可。

        var query = PFQuery(className:"TestObject")
        //        query.whereKey("playerName", equalTo:"Sean Plott")
        query.findObjectsInBackgroundWithBlock {
            (objects: [AnyObject]?, error: NSError?) -> Void in
    
            if error == nil {
                // The find succeeded.
                println("Successfully retrieved \(objects!.count) scores.")
                // Do something with the found objects
                theArray = objects as? [PFObject]
                if theArray.count > 0 {
                      myTableView.reloadData()
                }
                if let objects = objects as? [PFObject] {
                    for object in objects {
                        println(object.objectId)
                    }
                }
            } else {
                // Log details of the failure
                println("Error: \(error!) \(error!.userInfo!)")
            }
        }
    

    【讨论】:

    • 很好!更新了问题,Xcode 强迫我改变你的代码,现在它可以工作了!你能帮我解开警告吗?
    【解决方案2】:

    Parse 也有它自己的视图控制器 PFQueryTableViewController,它包含一个查询,然后设置表格视图中的单元格。我认为这可能对您的代码有用。

    请参阅https://parse.com/docs/ios/guide#user-interface-pfquerytableviewcontroller 了解更多信息。

    【讨论】:

    • 有用,但是当我新建文件/cocoaTouch 文件时,无法从类菜单中选择PFQueryTableViewController(而不是UITableViewController)
    • 您是否尝试将#import 添加到您的 bridgingheader.h 文件中?
    • 这篇文章可能对你有所帮助,stackoverflow.com/questions/27708057/…
    • 我在桥接文件中添加了#import ,如果需要也可以拖动 ParseUI 框架副本,但仍然无法选择。你用过那个 PFQueryTableViewController 吗?你是如何赢得这场战斗的?
    • 他们没有给出可能的指示,但我会检查回购!
    【解决方案3】:

    对于您的编辑,您是否想要代码:

    var new = object.objectId
    println("first: \(new)")
    if let tryToUnwrap:String = new {
    println("second: \(tryToUnwrap)")
    }
    

    【讨论】:

    • no :( ,也许你忘记了 { ,但它给了我这个问题:条件绑定中的绑定值必须是可选类型
    • 太棒了!编辑成功了!拜托,你能解释一下我的错误吗?我认为向下转换为 NSString 是正确的!
    • object.objectId 返回“字符串?”类型的值“if let”然后解包该可选并将其设置为“tryToUnwrap”,这用于检查“new”的值是否不为零。这能回答你的问题吗?
    • 不确定,因为在我看来,如果不是语法,我无法理解我们两种解决方案之间的区别:),让我们看看:是不是类似于“因为 new 是字符串?类型,让我们检查一下如果它是 nil 使用 if let” 现在从右边读取“如果 new 不是 nil 它将是 String 类型,并将 tryToUnwrap 设置为其值”所以这个解决方案也应该适用于整数?它们是 Struct 作为字符串,对吧?
    • 是的,所以你可以说“if new != nil { var tryToUnwrap = new as!String }
    猜你喜欢
    • 2019-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多