gpt4 book ai didi

ios - 数据未从 JSON 加载到第二个 View Controller 上

转载 作者:行者123 更新时间:2023-12-01 21:56:20 25 4
gpt4 key购买 nike

我终于能够将 API 中的数据加载到 tableview 中。当我切换到一个角色时,我想在下一个 View Controller 中获取关于他的数据。但是当我切换到下一个 VC 时,我没有收到任何错误,但我的数据为零。请帮助我,告诉我我做错了什么,我的错误在哪里?

我有 数据.swift

struct PagedCharacters: Decodable {
let info: Info
let results: [Results]

}
struct Info: Decodable {
let count: Int
let pages: Int
let next: String
let prev: String
}

struct Results: Decodable {
let id: Int
let name: String
let status: String
let species: String
let image: String
let url: String
let type: String
}

我的主要VC:
Viewcontroller.swift
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var uiTableView: UITableView!
var characters:[Results]?
var nextPageUrl:String!

override func viewDidLoad() {
super.viewDidLoad()
getIntitalRickAndMortyData()


self.uiTableView.dataSource = self
self.uiTableView.delegate = self

// Do any additional setup after loading the view.
}






func getIntitalRickAndMortyData(){
characters = []
//here first page is next page
nextPageUrl = "https://rickandmortyapi.com/api/character/"
getRickAndMortyData()
}

func getRickAndMortyData() {

//construct the url, use guard to avoid nonoptional
guard let urlObj = URL(string: nextPageUrl) else
{ return }

//fetch data
URLSession.shared.dataTask(with: urlObj) {[weak self](data, response, error) in

//to avoid non optional in JSONDecoder
guard let data = data else { return }

do {
//decode object
let downloadedRickAndMorty = try JSONDecoder().decode(PagedCharacters.self, from: data)
self?.characters?.append(contentsOf: downloadedRickAndMorty.results)
self?.nextPageUrl = downloadedRickAndMorty.info.next

DispatchQueue.main.async {
self?.uiTableView.reloadData()
}
print(self?.characters as Any)

} catch {
print(error)

}

}.resume()

}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.characters?.count ?? 0
}

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



if let count = characters?.count, count>1{
let lastElement = count - 1
if indexPath.row == lastElement {
//call get api for next page
getRickAndMortyData()

}
}


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


cell.textLabel?.text = "Name: " + (self.characters?[indexPath.row].name ?? "default")

return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let character = characters?[indexPath.row]
performSegue(withIdentifier: "toCharacterDetailVC", sender: self)
print(character?.name)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

if segue.identifier == "toCharacterDetailVC" {
if let destVC = segue.destination as? CharacterDetailViewController {
destVC.character = sender as? Results

}
}



}
}

和第二个 VC CharacterDetailViewController
import UIKit


class CharacterDetailViewController: UIViewController {


@IBOutlet var statusLabel: UILabel!
@IBOutlet var speciesLabel: UILabel!
@IBOutlet var typeLabel: UILabel!

public var character: Results?


override func viewDidLoad() {

title = character?.name
statusLabel.text = character?.status
speciesLabel.text = character?.species
typeLabel.text = character?.type == "" ? "Not Available" : character?.type
print(character?.name)
super.viewDidLoad()



}
}

最佳答案

var character: Results? {
didSet {
title = character?.name
statusLabel.text = character?.status
speciesLabel.text = character?.species
typeLabel.text = character?.type == "" ? "Not Available" : character?.type
}
}

关于ios - 数据未从 JSON 加载到第二个 View Controller 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61171871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com