2016-12-13 150 views
0

我正在從本站獲取SWAPI(星球大戰API) - >https://swapi.co/來填充我的tableview單元格,但是我對如何去關於抓取字符並將其全部顯示爲空數組。 變量「people」起作用,它捕捉那些確切的字符並在控制檯中顯示他們的數據。 我會怎麼做在viewDidLoad()中做到這一點?這是我迄今爲止所擁有的。我也正在迅速做到這一點。 A從SWAPI的resultsarray填充表格單元

import UIKit 

class PeopleViewController: UITableViewController { 

// var people = ["Luke Skywalker", "Leia Organa", "Han Solo", "C-3PO", "R2-D2"] 

var people = [Person]() 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    let url = NSURL(string: "http://swapi.co/api/people/") 
    // Create an NSURLSession to handle the request tasks 
    let session = NSURLSession.sharedSession() 
    // Create a "data task" which will request some data from a URL and then run a completion handler after it is done 
    let task = session.dataTaskWithURL(url!, completionHandler: { 
     data, response, error in 
     print("in here") 
     print(data) 

     do { 
      if let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary { 
       print(jsonResult) 
       if let results = jsonResult["results"] { 
        let resultsArray = results as! NSArray 
        print(resultsArray.count) 
        print(resultsArray.firstObject) 
       } 
      } 
     } catch { 
      print("Something went wrong") 
     } 
    }) 
    task.resume() 
} 
override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // return the count of people 
    return people.count 
} 
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    // Create a generic cell 
    let cell = UITableViewCell() 
    // set the default cell label to the corresponding element in the people array 
    cell.textLabel?.text = people[indexPath.row] 
    // return the cell so that it can be rendered 
    return cell 
} 

} 
+0

如果我理解正確的話,我想你需要從你的結果數組取數據,創建'人在你的完成處理程序中把它放到你的'人'數組中。然後告訴你的表重新加載它的數據。 –

回答

0

你有正確的想法。你只需要填入你的人數組,然後告訴表視圖重新加載自身(在主線程)

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    let url = NSURL(string: "http://swapi.co/api/people/") 
    // Create an NSURLSession to handle the request tasks 
    let session = NSURLSession.sharedSession() 
    // Create a "data task" which will request some data from a URL and then run a completion handler after it is done 
    let task = session.dataTaskWithURL(url!, completionHandler: { 
     data, response, error in 
     print("in here") 
     print(data) 

     do { 
      if let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary { 
       print(jsonResult) 
       if let results = jsonResult["results"] { 
        let resultsArray = results as! NSArray 
        print(resultsArray.count) 
        print(resultsArray.firstObject) 
        //------------------------------------------------------ 
        //Install the array in people and tell the table view to 
        //reload 
        DispatchQueue.main.async() { 
         //Replace the code below with code create Person 
         //objects from your data. 
         people = resultsArray.map(){Person(name:$0)} 
         tableView.reloadData() 
        } 
        //------------------------------------------------------ 
       } 
      } 
     } catch { 
      print("Something went wrong") 
     } 
    }) 
    task.resume() 
} 
相關問題