swift 用于在导航控制器上显示表视图控制器的操场

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift 用于在导航控制器上显示表视图控制器的操场相关的知识,希望对你有一定的参考价值。

import PlaygroundSupport
import UIKit

struct Pokemon {
    let id: UInt
    let name: String
}

class PokedexViewController: UITableViewController {
    let pokemons: [Pokemon] = [
        Pokemon(id: 1, name: "Bulbasaur"),
        Pokemon(id: 2, name: "Ivysaur"),
        Pokemon(id: 3, name: "Venusaur"),
        Pokemon(id: 4, name: "Charmander"),
        Pokemon(id: 5, name: "Charmeleon"),
        Pokemon(id: 6, name: "Charizard"),
        Pokemon(id: 7, name: "Squirtle"),
        Pokemon(id: 8, name: "Wartortle"),
        Pokemon(id: 9, name: "Blastoise")
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return pokemons.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: "PokemonTableViewCell")
        
        let pokemon = pokemons[indexPath.row]
        cell.textLabel?.text = pokemon.name
        
        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let pokemon = pokemons[indexPath.row]
        let viewController = PokemonViewController(frame: tableView.frame, pokemon: pokemon)
        
        navigationController?.pushViewController(viewController, animated: true)
    }
    
}

class PokemonViewController: UIViewController {
    private let pokemon: Pokemon
    
    init(frame: CGRect, pokemon: Pokemon) {
        self.pokemon = pokemon
        super.init(nibName: nil, bundle: nil)
        
        self.title = self.pokemon.name
        self.view = UIView(frame: frame)
        self.view.backgroundColor = UIColor.white
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

let rootViewController = PokedexViewController()
rootViewController.title = "Pokedex"

let navigationController = UINavigationController(rootViewController: rootViewController)
PlaygroundPage.current.liveView = navigationController.view

以上是关于swift 用于在导航控制器上显示表视图控制器的操场的主要内容,如果未能解决你的问题,请参考以下文章

在 ios swift 中导航到其他屏幕时,不鼓励在分离的视图控制器上显示视图控制器

使用swift在选项卡式视图中的导航栏上添加按钮

导航控制器工具栏大小和位置 - iOS Swift

Swift:删除嵌入在导航控制器中的表格视图控制器页脚下方的空间

如何在导航控制器 SWIFT 中显示视图控制器

在 Swift 的导航栏中设置图像