Swift Parse-本地数据存储项未出现在tableView中 [英] Swift Parse - local datastore items not appearing in tableView

查看:103
本文介绍了Swift Parse-本地数据存储项未出现在tableView中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我昨天的问题的后续内容

在我的表格代码中:

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
}

所以我不确定我在做什么错,但是似乎查询正在工作,但是什么也没进入数组.有什么想法吗?

我确实要注意,解析对象是在假设viewcontroller#2上创建的,而查询是在tableview所在的viewcontroller#1上运行的.这有什么不同吗?我应该运行查询并尝试在同一控制器上创建对象后立即追加吗?

解决方案

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

self.tableView.reloadData()

for object in light {循环之外

我认为您的数据可以添加到数组中,它只是表视图需要知道何时完成.

编辑***

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!)")
    }
}
}

This is a followup of my question from yesterday yesterdays post

I have successfully saved and object in the local datastore using parse, and and trying to add that object to an array to store them so i can display the contents in a table view. the query is running fine, but it appears to me that nothing is being appended into the array, so nothing shows in the table view. here's my code.

localData.swift file

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!
}

I then declare this globally:

var arrayToPopulateCells = [localData]()

this is my parse query:

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!)")
        }
    }
}

in my table code:

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

and

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
}

so im not sure what i'm doing wrong, but it seems that the query is working but nothing is going into the array. any ideas?

i do want to note that the parse object is created on lets say viewcontroller #2, and the query is run on viewcontroller #1 where the table view is. does this make a difference? should i run the query and try to append right after the object is made on the same controller?

解决方案

I think your problem is you need to call

self.tableView.reloadData()

outside the for object in light { loop

I think your data is being added to the array ok, its just the table view needs to know when its done.

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!)")
    }
}
}

这篇关于Swift Parse-本地数据存储项未出现在tableView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆