首页 > 解决方案 > 以编程方式在 TabBarController 内创建 TableViewController 不会呈现标签

问题描述

我想以编程方式创建一个带有两个的 tabBarView tableViewControllers

在我的AppDelegate.swift文件中,我在顶部添加了以下内容:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.makeKeyAndVisible()

        let mainVC = MainTabBarController()
        window?.rootViewController = mainVC
        return true
    }

我的MainTabBarController.swift文件包含以下内容:

import Foundation
import UIKit



class MainTabBarController : UITabBarController {
    override func viewDidLoad() {
        super.viewDidLoad()
        setUpTabBar()
    }

    func setUpTabBar() {

        let firstTableViewController =  UINavigationController(rootViewController: FirstTableViewController())
        firstTableViewController.tabBarItem.image = UIImage(named: "first")
        firstViewController.tabBarItem.selectedImage = UIImage(named: "first-selected")

        let secondViewController = UINavigationController(rootViewController: SecondTableViewController())
        SecondTableViewController.tabBarItem.image = UIImage(named: "second")
        SecondTableViewController.tabBarItem.selectedImage = UIImage(named: "second-selected")

        viewControllers = [firstViewController, SecondViewController]

        guard let items = tabBar.items else { return }
        for item in items {
            item.imageInsets = UIEdgeInsets(top: 4, left: 0, bottom: -4, right: 0)
        }
    }
}

我有FirstTableViewController.swift以下内容:

import Foundation
import UIKit

class FirstTableViewController : UITableViewController {

    let reuseIdentifier = "firstId"

    override func viewDidLoad() {
        setUpNavigationBar()
        setUpTableView()
    }

    private func setUpTableView() {
        self.tableView.register(FirstTableViewCell.self, forCellReuseIdentifier: reuseIdentifier)
    }

    func setUpNavigationBar() {
        navigationItem.title = "First"

        navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.darkGray, NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20)]
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as! FirstTableViewCell
        return cell
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 7
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

}

这里是FirstTableViewCell.swift

import Foundation
import UIKit

class FirstTableViewCell: UITableViewCell {
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setUp()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    let cellView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        view.setCellShadow()
        return view
    }()

    let firstItemLabel: UILabel = {
        let label = UILabel()
        label.text = "Name"
        label.textColor = .black
        label.font = UIFont.boldSystemFont(ofSize: 16)
        return label
    }()

    func setUp() {
        addSubview(cellView)
        cellView.addSubview(firstItemLabel)
    }
}

在当前的视图中FirsTableView,我看到的只是一个没有填充数据的空 tableView,即使我应该"Name"在每个单元格中看到:https ://imgur.com/a/Ecenn3O 。

所以我尝试了一些不同的东西SecondTableViewController.swift

import Foundation
import UIKit

class SecondTableViewController : UIViewController, UITableViewDelegate, UITableViewDataSource {

    let reUseIdentifier: String = "secondId"

    let tableView : UITableView = {
        let tb = UITableView()
        tb.separatorStyle = .none
        tb.allowsSelection = false
        return tb
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        setUpNavigationBar()
        setUpTableView()
    }

    func setUpNavigationBar() {
        navigationItem.title = "Second"

        navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.darkGray, NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20)]
    }


    func setUpTableView() {
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(SecondTableViewCell.self, forCellReuseIdentifier: reUseIdentifier)
        view.addSubview(tableView)
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 7
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: reUseIdentifier, for: indexPath) as! SecondTableViewCell
        return cell
    }

}

哪里SecondTableViewCell.swift非常类似于FirstTableViewCell.swift

import UIKit

class SecondTableViewCell: UITableViewCell {

    let cellView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        return view
    }()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setUp()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setUp() {
        addSubview(cellView)
        cellView.addSubview(secondTableViewCellLabel)
    }


    let secondTableViewCellLabel: UILabel = {
        let label = UILabel()
        label.text = "Name"
        label.textColor = .darkGray
        label.font = UIFont.boldSystemFont(ofSize: 16)
        return label
    }()
}

这只会呈现黑色屏幕,上面的选项卡名称为:https ://imgur.com/veTNln6 。

我不熟悉以编程方式创建视图,我不知道还需要做什么才能让 tabBar 按预期显示两个 tableView。

我按照这两个视频作为参考:

任何帮助将不胜感激。

标签: iosswift

解决方案


推荐阅读