2016-10-07 7 views
0

私がParseからクエリしたオブジェクトをUITableViewで使用できる配列にプッシュしようとするのに少し問題があります。Swift 3の配列にオブジェクトを追加する

ここに私のコードです。

var locations = [AnyObject]() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Query the Locations class. 
    let query = PFQuery(className:"Location") 

    query.findObjectsInBackground { 
     (objects: [PFObject]?, error: Error?) -> Void in 
     if error == nil { 
      if let objects = objects { 
       for object in objects { 
        self.locations.append(object) 
       } 
       self.venueTable.reloadData() 
      } 
     } else { 
      // Log details of the failure 
      print("Error: (error!) (error!.userInfo)") 
     } 
    } 

} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    return locations.count 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let locationCell = tableView.dequeueReusableCell(withIdentifier: "locationCell", for: indexPath) 

    let location = locations[indexPath.row] 

    locationCell.textLabel?.text = location 

    return locationCell 
} 

locationCell

答えて

3

にそれを押すとループのために、場所がそれにアクセスする方法がわから解析データの完全である、ではなく、あなたがた場所に設定されたタイプが[ANYOBJECT]ので、ウォンの後文字列ではないため、ラベルのテキストプロパティを設定しようとすると動作しません。

代わりに[PFObject]に設定し、PFObjectの関数objectForKeyを使用して、取得したオブジェクトから関連する文字列値を取得します。

例:

var locations = [PFObject]() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Query the Locations class. 
    let query = PFQuery(className:"Location") 

    query.findObjectsInBackground { 
    (objects: [PFObject]?, error: Error?) -> Void in 
     if error == nil { 
      if let objects = objects { 

       self.locations = objects 

       self.venueTable.reloadData() 

      } 

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

    } 

} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    return locations.count 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let locationCell = tableView.dequeueReusableCell(withIdentifier: "locationCell", for: indexPath) 

    let location = locations[indexPath.row] 

    locationCell.textLabel?.text = location.objectForKey("property name here") as? String 

    return locationCell 
} 
関連する問題