swift - 如何从解析中检索 geoPoints 并将其存储在 tableView 中?

标签 swift uitableview parse-platform

嗨,我有一个应用程序,它在解析服务器中存储带有名称的地理点,我能够将数据检索到控制台,但我无法用它们填充 tableView。我怎么能这样做呢?谢谢!

这是我用来存储数据的代码:

let geoPoint = PFObject(className: "location")

geoPoint["location"] = activePlace
geoPoint["spot"] = places
geoPoint.saveInBackgroundWithBlock { (succes, error) -> Void in
    print("place has been saved")
}

这是我用来检索它的代码:

let query = PFQuery(className: "location")
query.findObjectsInBackgroundWithBlock { (object, error) in
   
    
    if error != nil {
        
        print(error)
        
    }else {
        
        print(object)
        self.tableView.reloadData()
        
    }
}

这就是我确定我的 tableView 的方式:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    
    return 1
    
}

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

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
    
    cell.textLabel?.text = places[indexPath.row]["name"]
  
    
    return cell
    
    
    
}

这就是“地点”的确定方式:

var places = [Dictionary<String,String>()]

这是我在解析服务器上的内容:

This in from the parse server

感谢您的帮助!

编辑:这是我在控制台中得到的:

Optional([<location: 0x7fd383e34940, objectId: K4E1Dn0qqV, localId: (null)> {
    ACL = "<PFACL: 0x7fd383f2e070>";
    location = "-1";
    spot =     (
                {
            lat = 90;
            lon = 90;
            name = place;
        },
                {
            lat = "37.3313521358974";
            lon = "-122.030724242466";
            name = house;
        }
    );
}, <location: 0x7fd383f30600, objectId: D4tdDJcA4x, localId: (null)> {
    ACL = "<PFACL: 0x7fd383f14370>";
    location = "-1";
    spot =     (
                {
            lat = 90;
            lon = 90;
            name = place;
        },
                {
            lat = "37.3313521358974";
            lon = "-122.030724242466";
            name = house;
        }
    );
}, <location: 0x7fd383f328c0, objectId: aEyiGvNmze, localId: (null)> {
    ACL = "<PFACL: 0x7fd383f32c00>";
    location = "-1";
    spot =     (
                {
            lat = 90;
            lon = 90;
            name = place;
        },
                {
            lat = "37.3313521358974";
            lon = "-122.030724242466";
            name = house;
        }
    );
}, <location: 0x7fd383f32f80, objectId: r9DVe2KJde, localId: (null)> {
    ACL = "<PFACL: 0x7fd383f33320>";
    location = "-1";
    spot =     (
                {
            lat = 90;
            lon = 90;
            name = place;
        },
                {
            lat = "37.3313521358974";
            lon = "-122.030724242466";
            name = house;
        }
    );
}, <location: 0x7fd383f336a0, objectId: 9LwM4ATHFC, localId: (null)> {
    ACL = "<PFACL: 0x7fd383f14140>";
    location = "-1";
    spot =     (
                {
            lat = 90;
            lon = 90;
            name = place;
        },
                {
            lat = "37.3313521358974";
            lon = "-122.030724242466";
            name = house;
        },
                {
            lat = "37.3246293922446";
            lon = "-122.023606373578";
            name = other;
        }
    );
}, <location: 0x7fd383f33c00, objectId: QM8rg2NKNf, localId: (null)> {
    ACL = "<PFACL: 0x7fd383f34040>";
    location = "-1";
    spot =     (
                {
            lat = 90;
            lon = 90;
            name = place;
        },
                {
            lat = "37.3313521358974";
            lon = "-122.030724242466";
            name = house;
        },
                {
            lat = "37.3246293922446";
            lon = "-122.023606373578";
            name = other;
        }
    );
}, <location: 0x7fd383f343c0, objectId: NjlsykJtTf, localId: (null)> {
    ACL = "<PFACL: 0x7fd383f34690>";
    location = "-1";
    spot =     (
                {
            lat = 90;
            lon = 90;
            name = "GO to map to add spot";
        }
    );
}, <location: 0x7fd383f34a10, objectId: qdOPtRVzew, localId: (null)> {
    ACL = "<PFACL: 0x7fd383f34c70>";
    location = "-1";
    spot =     (
    );
}, <location: 0x7fd383f34ff0, objectId: DC5ZW7WRNO, localId: (null)> {
    ACL = "<PFACL: 0x7fd383f35250>";
    location = "-1";
    spot =     (
    );
}, <location: 0x7fd383f355d0, objectId: rCZIaHlceC, localId: (null)> {
    ACL = "<PFACL: 0x7fd383f358a0>";
    location = "-1";
    spot =     (
                {
            lat = 90;
            lon = 90;
            name = "GO to map to add spot";
        }
    );
}, <location: 0x7fd383f35c20, objectId: 2LI1u9KyNt, localId: (null)> {
    ACL = "<PFACL: 0x7fd383f35ef0>";
    location = "-1";
    spot =     (
                {
            lat = 90;
            lon = 90;
            name = "GO to map to add spot";
        }
    );
}, <location: 0x7fd383f36270, objectId: YKd0q9uQcY, localId: (null)> {
    ACL = "<PFACL: 0x7fd383f365b0>";
    location = "-1";
    spot =     (
                {
            lat = 90;
            lon = 90;
            name = "GO to map to add spot";
        },
                {
            lat = "37.3263892401666";
            lon = "-122.019776452882";
            name = "new one";
        }
    );
}, <location: 0x7fd383f36930, objectId: rGd3ChyOcV, localId: (null)> {
    ACL = "<PFACL: 0x7fd383f36d60>";
    location = "-1";
    spot =     (
                {
            lat = 90;
            lon = 90;
            name = "GO to map to add spot";
        },
                {
            lat = "37.3263892401666";
            lon = "-122.019776452882";
            name = "new one";
        },
                {
            lat = "37.3247014690037";
            lon = "-122.020914298787";
            name = "mc laren";
        }
    );
}, <location: 0x7fd383f37110, objectId: aunhPqs1Ww, localId: (null)> {
    ACL = "<PFACL: 0x7fd383f375a0>";
    location = "-1";
    spot =     (
                {
            lat = 90;
            lon = 90;
            name = "GO to map to add spot";
        },
                {
            lat = "37.3263892401666";
            lon = "-122.019776452882";
            name = "new one";
        },
                {
            lat = "37.3247014690037";
            lon = "-122.020914298787";
            name = "mc laren";
        },
                {
            lat = "37.3246335613548";
            lon = "-122.023146659295";
            name = boom;
        }
    );
}, <location: 0x7fd383f37950, objectId: CtWCmwuZHv, localId: (null)> {
    ACL = "<PFACL: 0x7fd383f37c60>";
    location = "-1";
    spot =     (
                {
            lat = 90;
            lon = 90;
            name = "GO to map to add spot";
        }
    );
}, <location: 0x7fd383cc37a0, objectId: AHHsEyrCJk, localId: (null)> {
    ACL = "<PFACL: 0x7fd383e228d0>";
    location = "-1";
    spot =     (
                {
            lat = 90;
            lon = 90;
            name = "GO to map to add spot";
        },
                {
            lat = "37.3306871385499";
            lon = "-122.03026490349";
            name = "20405\U201320449 Mariani Ave";
        }
    );
}])

编辑2:这是整个tableView代码:

import UIKit
import Parse

var rideSpots = [""]

//test start

class Place {
    var ACL : String
    var location : String
    
    init(ACL : String, location : String) {
        self.ACL = ACL
        self.location = location
    }
}
let places2 : [Place] = []
//test stop

var places = [Dictionary<String,String>()]

var activePlace = -1

class TableViewController: UITableViewController {
    
    @available(iOS 8.0, *)
    func companyNameUpdatedAlert(title: String, error: String, indexPath: Int) {
        
        let alert = UIAlertController(title: title, message: error, preferredStyle: UIAlertControllerStyle.Alert)
        
        alert.addTextFieldWithConfigurationHandler { (textField) -> Void in
            
            textField.placeholder = "Enter new text"
            
        }
        
        alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in
            
            let lat = places[indexPath]["lat"]!
            
            let lon = places[indexPath]["lon"]!
            
            places.removeAtIndex(indexPath)
            
            places.insert(["name" : alert.textFields![0].text!, "lat" : lat, "lon" : lon], atIndex: indexPath)
            
            self.tableView.reloadData()
            
            NSUserDefaults.standardUserDefaults().setObject(places, forKey: "places")
            NSUserDefaults.standardUserDefaults().synchronize()
            
            
        }))
        
        self.presentViewController(alert, animated: true, completion: nil)
        
        
    }
    
    @available(iOS 8.0, *)
    override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
        
        let changeText = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Change spot name" , handler: { (action:UITableViewRowAction, indexPath:NSIndexPath) -> Void in
            
            self.companyNameUpdatedAlert("Update text", error: "enter text below", indexPath: indexPath.row)
            
        })
     /*
        let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete" , handler: { (action:UITableViewRowAction, indexPath:NSIndexPath) -> Void in
            
            places.removeAtIndex(indexPath.row)
            
            tableView.reloadData()
            
            NSUserDefaults.standardUserDefaults().setObject(places, forKey: "places")
            NSUserDefaults.standardUserDefaults().synchronize()
 
        })
        */
        return [changeText]
        
        
    }
    
    override func viewDidLoad() {
        
        
            
            super.viewDidLoad()
        
        //retrieve the data from parse start
        
        //first start of test retrieving
        
        let query = PFQuery(className: "location")
        query.findObjectsInBackgroundWithBlock { (objects, error) in
            
            
            if error != nil {
                
                print(error)
                
            }else {
                
                
                
                print(object)
                
                for object in (objects)! {
                    
                    // Retrieve data needed and add to places (example ACL and location)
                    let ACL : String = object["ACL"] as! String
                    let location : String = object["location"] as! String
                    let place : Place = Place(ACL : ACL, location : location)
                    self.places.append(place)
                }
                
                self.tableView.reloadData()
                
            }
        }
        
        
        //end of test retrieving

    /*
        let query = PFQuery(className: "location")
        query.findObjectsInBackgroundWithBlock { (object, error) in
            
            
            if error != nil {
                
                print(error)
                
            }else {
                
                print(object)
                self.tableView.reloadData()
                
            }
        }
    */
    
            //retrieve the data from parse stop
            
            
            if places.count == 1 {
                
                places.removeAtIndex(0)
                
                places.append(["name":"GO to map to add spot","lat":"90","lon":"90"])
                
                
                
                
            }
            if NSUserDefaults.standardUserDefaults().objectForKey("places") != nil {
                
                places = NSUserDefaults.standardUserDefaults().objectForKey("places") as! [Dictionary]
                
                
            }
            
        }
    
    
    override func didReceiveMemoryWarning() {
        
        didReceiveMemoryWarning()
        
    }
    
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        
        return 1
        
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return places.count
        
        
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
        
        cell.textLabel?.text = places[indexPath.row]["name"]
      
        
        return cell
        
        
        
    }
    
    override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
        
        activePlace = indexPath.row
        
        return indexPath
        
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        
        if segue.identifier == "newPlace" {
            
            activePlace = -1
            
            
        }
        
    }
    
    override func viewWillAppear(animated: Bool) {
        
    tableView.reloadData()
        
        
    }

}

最佳答案

经过一番审查后,我会创建一个名为地方的对象而不是字典。这样你就可以创建一个对象数组而不是字典,这很奇怪。

class Place {
    var ACL : String
    var location : String

    init(ACL : String, location : String) {
        self.ACL = ACL
        self.location = location
    }
}

添加

var places : [Place] = []

在查询中,而不是仅仅打印对象并重新加载 tableView。每次重新加载时,您的数据源都没有改变。

let query = PFQuery(className: "location")
query.findObjectsInBackgroundWithBlock { (objects, error) in


    if error != nil {

        print(error)

    }else {

        print(object)

        for object in objects {
            // Retrieve data needed and add to places (example ACL and location)
            let ACL : String = object["ACL"] as String
            let location : String = object["location"] as String
            let place : Place = Place(ACL : ACL, location : location)
            self.places.append(place)
        }

        self.tableView.reloadData()

    }
}

关于swift - 如何从解析中检索 geoPoints 并将其存储在 tableView 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36404257/

相关文章:

ios - Realm 多对一关系示例

iOS TableView 点击事件不起作用

ios - 添加更多单元格后,某些表格 View 单元格变得不可见。为什么?

webserver - 将 WebStorm 与 Parse.com 一起使用

ios - 来自用户的 key 类型无效,预期映射,但得到 *_User

ios - Swift 2 IOS 9 = clang : error: linker command failed with exit code 1 (use -v to see invocation)

swift - 从一组数据(coreData 图像)创建 Zip 文件并通过电子邮件通过 .zip 文件共享

ios - 在 Swift iOS 中接听电话

swift - 为什么这个函数的参数仍然是一个元组?

ios - Objective-C - iPhone : different DetailViewControllers for TableView?