パース辞書(文字列と場所を含む)でtableViewを作成しようとしていますが、データを解析してコンソールにダウンロードするように管理していますが、 、場所は(マップビューで)このように追加されるのtableView:私のtableViewの完全なコードはこれですどのように私はparse辞書でtableViewを設定できますか?
let query = PFQuery(className: "location")
query.findObjectsInBackgroundWithBlock { (object, error) in
if error != nil {
print(error)
}else {
print(object)
self.tableView.reloadData()
}
}
:
let geoPoint = PFObject(className: "location")
geoPoint["location"] = activePlace
geoPoint["spot"] = places
geoPoint.saveInBackgroundWithBlock { (succes, error) -> Void in
print("place has been saved")
}
、それがdowloadedさは/このようなコンソールに追加しました
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()
//retreve the data from parse start
let query = PFQuery(className: "location")
query.findObjectsInBackgroundWithBlock { (object, error) in
if error != nil {
print(error)
}else {
print(object)
self.tableView.reloadData()
}
}
//retreve 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
//return places2.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"]
//cell.textLabel!.text = places2[indexPath.row].location
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()
}
}
これは簡単ですが、何らかの理由で私はそれを理解することができないので、どんな助けでも大歓迎です。
ありがとうございます!
APIから受け取った場所を保存する必要があります。 'print(object)'と言うところでは、 'places = object'を追加しようとしましたか?(reloadDataを呼び出す前に) – Bensge
これを実行すると、「[PFObject]型の値を代入できませんか? ['Dictionary]'と入力する@Bensge –