使用来自 SWAPI 的结果数组的结果填充 table 单元格
Populate table cell with results from resultsarray from SWAPI
我正在努力从该站点获取 SWAPI(星球大战 API)-->https://swapi.co/ 以填充到我的 tableview 单元格中,但我有点对如何抓取字符并用空数组显示它们感到困惑。
变量 "people" 有效,它会抓取那些确切的字符并在控制台中显示它们的数据。
我将如何在 viewDidLoad() 中执行此操作?这是我到目前为止所拥有的。我也在 swift 2.3 中这样做。 A
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
}
}
你的想法是正确的。您只需要填充您的 people 数组,然后告诉 table 视图重新加载自身(从主线程)
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:[=10=])}
tableView.reloadData()
}
//------------------------------------------------------
}
}
} catch {
print("Something went wrong")
}
})
task.resume()
}
我正在努力从该站点获取 SWAPI(星球大战 API)-->https://swapi.co/ 以填充到我的 tableview 单元格中,但我有点对如何抓取字符并用空数组显示它们感到困惑。 变量 "people" 有效,它会抓取那些确切的字符并在控制台中显示它们的数据。 我将如何在 viewDidLoad() 中执行此操作?这是我到目前为止所拥有的。我也在 swift 2.3 中这样做。 A
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
}
}
你的想法是正确的。您只需要填充您的 people 数组,然后告诉 table 视图重新加载自身(从主线程)
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:[=10=])}
tableView.reloadData()
}
//------------------------------------------------------
}
}
} catch {
print("Something went wrong")
}
})
task.resume()
}