我正在以 2 页的形式提出申请https://pokeapi.co/api/v2/pokemon将有使用 Api 的主要页面和详细信息页面。 我在主页上创建了一个包含图片和名称的表格。但是我卡在详情页无法继续了
我正在尝试创建一个详细信息页面,我需要在其中填写 DidSelect 部分。当我点击表格视图中的任何单元格时。我要显示单元格中口袋妖怪的图片和详细特征。
我要在详情页展示 -姓名 -图像 -统计 -能力 -身高
主视图控制器
import UIKit
class MainViewController: UIViewController {
private var pokemons: [GetPokemonsResponse.Pokemon] = []
private var activityIndicatorView: UIActivityIndicatorView!
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(UINib(nibName: "PokemonTableViewCell", bundle: nil), forCellReuseIdentifier: "cell")
self.tableView.delegate = self
self.tableView.dataSource = self
activityIndicatorView = UIActivityIndicatorView(style: .large)
activityIndicatorView.center = tableView.center
tableView.addSubview(activityIndicatorView)
activityIndicatorView.startAnimating()
getPokemons()
}
private func getPokemons() {
PokeAPIService.shared.getPokemons { response in
self.pokemons = response.results
DispatchQueue.main.async {
self.activityIndicatorView.stopAnimating()
self.tableView.reloadData()
}
}
}
}
extension MainViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return pokemons.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath) as? PokemonTableViewCell {
let pokemon = pokemons[indexPath.row]
cell.nameLabel.text = pokemons[indexPath.row].name
cell.loadImage(from: PokeAPIService.shared.getPokemonSprites(id: pokemon.pokemonID))
return cell
}
return UITableViewCell()
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
}
DetailViewController
class DetailViewController: UIViewController {
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var detailImageView: UIImageView!
@IBOutlet weak var heightLabel: UILabel!
@IBOutlet weak var abilitiesLabel: UILabel!
@IBOutlet weak var statsLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
}
PokeApi服务
import Foundation
class PokeAPIService {
static let shared = PokeAPIService()
let limitNumber = 40
func getPokemons(completion: @escaping (GetPokemonsResponse) -> Void) {
guard let url = URL(string: "https://pokeapi.co/api/v2/pokemon/?offset=0&limit=\(limitNumber)") else { return }
URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data else { return }
do {
let response = try JSONDecoder().decode(GetPokemonsResponse.self, from: data)
completion(response)
} catch let error {
print(error.localizedDescription)
}
}.resume()
}
func getPokemon(by id: String, completion: @escaping (GetPokemonResponse) -> Void) {
guard let url = URL(string: "https://pokeapi.co/api/v2/pokemon/\(id)/") else { return }
URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data else { return }
do {
let response = try JSONDecoder().decode(GetPokemonResponse.self, from: data)
completion(response)
} catch let error {
print(error.localizedDescription)
}
}.resume()
}
func getPokemonSprites(id: String) -> URL? {
URL(string: "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/\(id).png")
}
func getPokemonOfficialArtwork(id: String) -> URL?{
URL(string: "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/other/official-artwork/\(id).png")
}
}
型号
struct GetPokemonResponse: Codable {
let abilities: [Ability]?
let baseExperience: Int?
let forms: [Species]?
let gameIndices: [GameIndex]?
let height: Int?
let heldItems: [JSONAny]?
let id: Int?
let isDefault: Bool?
let locationAreaEncounters: String?
let moves: [Move]?
let name: String?
let order: Int?
let pastTypes: [JSONAny]?
let species: Species?
let sprites: Sprites?
let stats: [Stat]?
let types: [TypeElement]?
let weight: Int?
enum CodingKeys: String, CodingKey {
case abilities
case baseExperience = "base_experience"
case forms
case gameIndices = "game_indices"
case height
case heldItems = "held_items"
case id
case isDefault = "is_default"
case locationAreaEncounters = "location_area_encounters"
case moves, name, order
case pastTypes = "past_types"
case species, sprites, stats, types, weight
}
}
// MARK: - Ability
struct Ability: Codable {
let ability: Species?
let isHidden: Bool?
let slot: Int?
enum CodingKeys: String, CodingKey {
case ability
case isHidden = "is_hidden"
case slot
}
}
// MARK: - Species
struct Species: Codable {
let name: String?
let url: String?
}
// MARK: - Stat
struct Stat: Codable {
let baseStat, effort: Int?
let stat: Species?
enum CodingKeys: String, CodingKey {
case baseStat = "base_stat"
case effort, stat
}
}