Swift 如何使用解析字典填充tableView?
我试图用解析字典(包含字符串和位置)填充tableView,我设法上传数据进行解析并将其下载到控制台,但是我在将其添加到tableView时遇到了很多问题,位置是这样添加的(在地图视图中): 并将其下载/添加到控制台,如下所示:Swift 如何使用解析字典填充tableView?,swift,uitableview,parse-platform,Swift,Uitableview,Parse Platform,我试图用解析字典(包含字符串和位置)填充tableView,我设法上传数据进行解析并将其下载到控制台,但是我在将其添加到tableView时遇到了很多问题,位置是这样添加的(在地图视图中): 并将其下载/添加到控制台,如下所示: let query = PFQuery(className: "location") query.findObjectsInBackgroundWithBlock { (object, error) in if error
let query = PFQuery(className: "location")
query.findObjectsInBackgroundWithBlock { (object, error) in
if error != nil {
print(error)
}else {
print(object)
self.tableView.reloadData()
}
}
我的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
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
//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()
}
}
导入UIKit
导入解析
var=[“”]
//测试启动
//班级{
//var-ACL:String
//变量位置:字符串
//初始化(ACL:String,位置:String){
//self.ACL=ACL
//self.location=位置
// }
//}
//let places2:[地点]=[]
//测试停止
var places=[Dictionary()]
var activePlace=-1
类TableViewController:UITableViewController{
@可用(iOS 8.0,*)
func companyNameUpdatedAlert(标题:字符串,错误:字符串,索引XPath:Int){
let alert=UIAlertController(标题:标题,消息:错误,首选样式:UIAlertControllerStyle.alert)
alert.addTextFieldWithConfigurationHandler{(textField)->中的Void
textField.placeholder=“输入新文本”
}
addAction(UIAlertAction)(标题:“确定”,样式:。默认,处理程序:{(操作)->Void in
让lat=places[indepath][“lat”]!
让lon=places[indexPath][“lon”]!
places.removeAtIndex(indexPath)
places.insert([“name”:alert.textFields![0].text!,“lat”:lat,“lon”:lon],atIndex:indexath)
self.tableView.reloadData()
NSUserDefaults.standardUserDefaults().setObject(places,forKey:“places”)
NSUserDefaults.standardUserDefaults().synchronize()
}))
self.presentViewController(警报、动画:true、完成:nil)
}
@可用(iOS 8.0,*)
重写func tableView(tableView:UITableView,editActionsErrorwatIndeXPath:NSIndexPath)->[UITableViewRowAction]{
让changeText=UITableViewRowAction(样式:UITableViewRowActionStyle.Default,标题:“更改点名称”,处理程序:{(操作:UITableViewRowAction,索引XPath:NSIndexPath)->在
self.companyNameUpdateAlert(“更新文本”,错误:“在下面输入文本”,indexPath:indexPath.row)
})
/*
让deleteAction=UITableViewRowAction(样式:UITableViewRowActionStyle.Default,标题:“Delete”,处理程序:{(操作:UITableViewRowAction,索引XPath:NSIndexPath)->Void in
places.removeAtIndex(indexPath.row)
tableView.reloadData()
NSUserDefaults.standardUserDefaults().setObject(places,forKey:“places”)
NSUserDefaults.standardUserDefaults().synchronize()
})
*/
返回[更改文本]
}
重写func viewDidLoad(){
super.viewDidLoad()
//从解析开始检索数据
let query=PFQuery(类名:“位置”)
query.findObjectsInBackgroundWithBlock{(对象,错误)位于
如果错误!=nil{
打印(错误)
}否则{
打印(对象)
self.tableView.reloadData()
}
}
//从解析停止中检索数据
如果places.count==1{
places.removeAtIndex(0)
places.append([“name”:“转到地图添加点”,“lat”:“90”,“lon”:“90”])
}
如果NSUserDefaults.standardUserDefaults().objectForKey(“位置”)!=nil{
places=NSUserDefaults.standardUserDefaults().objectForKey(“places”)as![字典]
}
}
重写函数didReceiveMemoryWarning(){
我收到了记忆警告()
}
重写func numberOfSectionsInTableView(tableView:UITableView)->Int{
返回1
}
重写func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回位置。计数
//返回位置2.count
}
重写func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
将cell=tableView.dequeueReusableCellWithIdentifier(“cell”,forIndexPath:indexPath)设为UITableViewCell
cell.textlab?.text=places[indexPath.row][“name”]
//cell.textlab!.text=places2[indexPath.row]。位置
返回单元
}
重写func tableView(tableView:UITableView,willSelectRowAtIndexPath-indepath:nsindepath)->nsindepath{
activePlace=indexPath.row
返回索引XPath
}
覆盖函数prepareforsgue(segue:UIStoryboardSegue,sender:AnyObject?){
如果segue.identifier==“newPlace”{
activePlace=-1
}
}
覆盖功能视图将出现(动画:Bool){
tableView.reloadData()
}
}
这听起来很简单,但由于某些原因我无法理解,因此非常感谢您的帮助
谢谢 因为您试图创建一个对象来表示较大PFObject的较小部分,所以我建议您创建一个places结构
struct Places {
var someData: String!
var someMoreData: String!
}
//Class level variable of places
var places: [Places]?
然后,当您从查询中接收到对象时,可以创建位置并将其附加到数组中
var placeToAdd = Places(someData: object["name"], someMoreData: object["anotherField"])
places.append(placeToAdd)
最后,在创建单元格时,可以适当地访问信息
cell.textLabel!.text = places[indexPath.row].name
你需要拯救t
cell.textLabel!.text = places[indexPath.row].name