Swift Parse - 本地数据存储项目未出现在tableView中

时间:2015-09-07 16:20:05

标签: ios swift parse-platform local-datastore

这是我昨天yesterdays post

的问题的后续跟进

我已使用parse在本地数据存储区中成功保存并对象,并尝试将该对象添加到数组中以存储它们,以便我可以在表视图中显示内容。查询运行正常,但在我看来,没有任何内容被附加到数组中,所以在表视图中没有显示任何内容。这是我的代码。

localData.swift文件

import Foundation

struct localData {
var date: String!
var latt: NSNumber!
var lattDelta: NSNumber!
var locality: String!
var longi: NSNumber!
var longiDelta: NSNumber!
var name: String!
var note: String!
}

然后我在全球宣布:

var arrayToPopulateCells = [localData]()

这是我的解析查询:

func performQuery() {
    let query = PFQuery(className: "ParseLighthouse")

    query.fromLocalDatastore()
    query.whereKey("User", equalTo: PFUser.currentUser()!)
    query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
        if error == nil {
            // The find succeeded.
            println("Successfully retrieved \(objects!.count) lighthouses.")
            // Do something with the found objects
            if let light = objects as? [PFObject] {
                for object in light {
//                        println(object.objectId)
//                        println(object.objectForKey("Name"))
//                        println(object.objectForKey("Locality"))
                    var singleData = localData()
                    singleData.name = object["Name"] as! String
                    singleData.note = object["Note"] as! String
                    singleData.date = object["Date"] as! String
                    singleData.latt = object["Latt"] as! NSNumber
                    singleData.longi = object["Longi"] as! NSNumber
                    singleData.lattDelta = object["LattDelta"] as! NSNumber
                    singleData.longiDelta = object["LongiDelta"] as! NSNumber
                    singleData.locality = object["Locality"] as! String


                    self.arrayToPopulateCells.append(singleData)



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

在我的表格代码中:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return arrayToPopulateCells.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {



//        var lighthouse = self.lighthouses[indexPath.row]
    var data = self.arrayToPopulateCells[indexPath.row]

    //setting the prototype cell to link with the identifier set in attributes earlier.
    let cell = tableView.dequeueReusableCellWithIdentifier("locationCell") as! lighthouseCell

    let row = indexPath.row
    cell.cellName.text = data.name
    cell.cellPlace.text = data.locality
//        cell.cellCoordinates.text = "\(lighthouse.latt)" + ", " + "\(lighthouse.longi)"
//        cell.cellNote.text = lighthouse.note
    cell.cellDate.text = "\(data.date)"



    return cell
}

所以我不确定我做错了什么,但似乎查询正在运行,但没有任何内容进入数组。任何想法?

我确实想要注意,parse对象是在let say sayconcontroller#2上创建的,查询是在viewcontroller#1上运行的,其中表视图是。这会有所作为吗?我应该运行查询并尝试在同一个控制器上创建对象后立即追加?

1 个答案:

答案 0 :(得分:0)

我认为您的问题是需要致电

self.tableView.reloadData()

for object in light {循环之外

我认为你的数据正被添加到数组中,只是表格视图需要知道它何时完成。

EDIT ***

func performQuery() {
let query = PFQuery(className: "ParseLighthouse")

query.fromLocalDatastore()
query.whereKey("User", equalTo: PFUser.currentUser()!)
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
    if error == nil {
        // The find succeeded.
        println("Successfully retrieved \(objects!.count) lighthouses.")
        // Do something with the found objects
        if let light = objects as? [PFObject] {
            for object in light {
                var singleData = localData()
                singleData.name = object["Name"] as! String
                singleData.note = object["Note"] as! String
                singleData.date = object["Date"] as! String
                singleData.latt = object["Latt"] as! NSNumber
                singleData.longi = object["Longi"] as! NSNumber
                singleData.lattDelta = object["LattDelta"] as! NSNumber
                singleData.longiDelta = object["LongiDelta"] as! NSNumber
                singleData.locality = object["Locality"] as! String
                self.arrayToPopulateCells.append(singleData)
            }
            self.tableView.reloadData()
        }
    } else {
        // Log details of the failure
        println("Error: \(error!) \(error!.userInfo!)")
    }
}
}