从 API 调用获取数据并在 UItableView 行点击时显示另一个 viewController
Get data from API call and show another viewController on UItableView row click
我是 Swift 的新手,所以我寻求帮助。我使用 API call.When 用户单击一行在 tableview 上显示字符列表,我想调用 API ,获取数据并在新的 viewController
中显示
API 调用 url 看起来像 :
https://rickandmortyapi.com/api/character/{user_clicked_row_charactor_id}
id = 3 的示例响应
{
"id": 3,
"name": "Summer Smith",
"status": "Alive",
"species": "Human",
"type": "",
"gender": "Female",
}
因此每个字符都是必需的。告诉或指导如何实施?
抱歉我的英语不好。
我的代码:
class UsersTableViewController: UITableViewController {
var characters = [Results]()
override func viewDidLoad() {
super.viewDidLoad()
LoadCharacters()
}
func LoadCharacters() {
let urlString = "https://rickandmortyapi.com/api/character/"
if let url = URL(string: urlString)
{
let session = URLSession(configuration: .default)
let task = session.dataTask(with: url) { (data, responce, error) in
if error != nil {
print(error!)
return
}
if let safeData = data {
self.parseJson(usersData: safeData)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
task.resume()
self.tableView.reloadData()
}
}
func parseJson(usersData: Data) {
let decoder = JSONDecoder()
do {
let decodedData = try decoder.decode(JSONData.self, from: usersData)
characters = decodedData.results
print(decodedData.results[0].name)
} catch {
print(error)
}
}
struct JSONData: Decodable {
let results: [Results]
}
struct Results: Decodable {
let name: String
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return characters.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "user", for: indexPath)
let guys = characters[indexPath.row]
cell.textLabel?.text = guys.name
return cell
}
}
如果我正确理解你的问题,你需要实现 table 视图方法的 didSelectRowAt,然后将必要的数据传递给新的视图控制器:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let character = characters[indexPath.row]
let charactersViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: "characterVC") as! CharacterDetailViewController
charactersViewController.character = character
present(charactersViewController.character, animated: true)
}
为此,您需要有另一个视图控制器来显示点击后的角色。在身份检查器中为该视图控制器提供故事板 ID "characterVC"。视图控制器还需要一个变量来接收字符对象,也许:
class CharacterDetailViewController: UIViewController {
var character: Character?
override func viewDidLoad() {
super.viewDidLoad()
if let character = character {
// show the character on the view
}
}
}
我是 Swift 的新手,所以我寻求帮助。我使用 API call.When 用户单击一行在 tableview 上显示字符列表,我想调用 API ,获取数据并在新的 viewController
中显示
API 调用 url 看起来像 :
https://rickandmortyapi.com/api/character/{user_clicked_row_charactor_id}
id = 3 的示例响应
{
"id": 3,
"name": "Summer Smith",
"status": "Alive",
"species": "Human",
"type": "",
"gender": "Female",
}
因此每个字符都是必需的。告诉或指导如何实施?
抱歉我的英语不好。 我的代码:
class UsersTableViewController: UITableViewController {
var characters = [Results]()
override func viewDidLoad() {
super.viewDidLoad()
LoadCharacters()
}
func LoadCharacters() {
let urlString = "https://rickandmortyapi.com/api/character/"
if let url = URL(string: urlString)
{
let session = URLSession(configuration: .default)
let task = session.dataTask(with: url) { (data, responce, error) in
if error != nil {
print(error!)
return
}
if let safeData = data {
self.parseJson(usersData: safeData)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
task.resume()
self.tableView.reloadData()
}
}
func parseJson(usersData: Data) {
let decoder = JSONDecoder()
do {
let decodedData = try decoder.decode(JSONData.self, from: usersData)
characters = decodedData.results
print(decodedData.results[0].name)
} catch {
print(error)
}
}
struct JSONData: Decodable {
let results: [Results]
}
struct Results: Decodable {
let name: String
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return characters.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "user", for: indexPath)
let guys = characters[indexPath.row]
cell.textLabel?.text = guys.name
return cell
}
}
如果我正确理解你的问题,你需要实现 table 视图方法的 didSelectRowAt,然后将必要的数据传递给新的视图控制器:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let character = characters[indexPath.row]
let charactersViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: "characterVC") as! CharacterDetailViewController
charactersViewController.character = character
present(charactersViewController.character, animated: true)
}
为此,您需要有另一个视图控制器来显示点击后的角色。在身份检查器中为该视图控制器提供故事板 ID "characterVC"。视图控制器还需要一个变量来接收字符对象,也许:
class CharacterDetailViewController: UIViewController {
var character: Character?
override func viewDidLoad() {
super.viewDidLoad()
if let character = character {
// show the character on the view
}
}
}