简体   繁体   English

ios swift tableview 不显示自定义单元格

[英]ios swift tableview not showing custom cells

I am trying to create a table view with custom cells from Storyboard layout in an iOS app.我正在尝试使用 iOS 应用程序中 Storyboard 布局中的自定义单元格创建表格视图。

But for some reason the table cells are not being shown.但由于某种原因,表格单元格没有显示出来。 When I tried to set debug breakpoints I found that the debugger is reaching this function当我尝试设置调试断点时,我发现调试器正在达到此功能

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int

but it never reaches this function -但它永远不会达到这个功能 -

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

Here is my viewcontroller code -这是我的视图控制器代码 -

extension NavigationViewController: UITableViewDataSource, UITableViewDelegate, SideMenuControllerDelegate {

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SideMenuTableItem", for: indexPath as IndexPath) as! SideMenuTableItem
        cell.setItemData(items[indexPath.row])
        return cell
    }

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

    func setupTableViews() {
        menuTable.register(SideMenuTableItem.self, forCellReuseIdentifier: "SideMenuTableItem")

    }
}

class SideMenuTableItem: UITableViewCell {

    @IBOutlet weak var menuImage: UIImageView!
    @IBOutlet weak var menuLabel: UILabel!

    var data: MenuItem?

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    func setItemData(_ item: MenuItem) {
        data = item
        menuLabel.text = data?.title
        if data?.icon_res != nil {
            menuImage.image = UIImage(named: (data?.icon_res)!)
        }
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

I have checked in the storyboard that I have set the reusable identifier to the table prototype cell and also connected the datasource and the delegate properties to the tableview我已经检查了故事板,我已将可重用标识符设置为表格原型单元格,并将数据源和委托属性连接到表格视图

细胞标识符 数据源,委托引用 其他 tableview 属性 调试结果

and I am calling the setupTableViews() method inside my viewDidLoad() function after creating the items array But still I am not able to get the cells to appear in my view at all.并且我在创建 items 数组后在我的 viewDidLoad() 函数中调用 setupTableViews() 方法但是我仍然无法让单元格出现在我的视图中。 Can anyone suggest what am I missing here or what's wrong with my code, or how can I further debug this issue任何人都可以建议我在这里缺少什么或我的代码有什么问题,或者我如何进一步调试这个问题

import UIKit
import SideMenuSwift

class NavigationViewController: UIViewController {

    @IBOutlet weak var navigationContainer: UIView!
    @IBOutlet weak var emailButton: UIButton!
    @IBOutlet weak var phoneButton: UIButton!
    @IBOutlet weak var userAvatar: UIImageView!
    @IBOutlet weak var userProfile: UIButton!
    @IBOutlet weak var userName: UILabel!
    @IBOutlet weak var menuTable: UITableView!

    var service: AuthenticationService!
    var cdc: CoreDataController!
    var items: [MenuItem] = []
    var currentUser: User?

    override func viewDidLoad() {
        super.viewDidLoad()
        setupSidebar()
        initSidebarData()
        setupUserHeader()
        setupTableViews()
    }

    func setupUserHeader() {
        if currentUser != nil {
            if currentUser?.name != nil {
                userName.text = currentUser?.name
            } else if currentUser?.role != nil {
                userName.text = "urTutors " + (currentUser?.role ?? "")
            }
            if currentUser?.avatarUrl != nil {
                userAvatar.downloaded(from: (currentUser?.avatarUrl)!)
            }
        }
    }

    func initSidebarData() {
        service = AuthenticationServiceProvider()
        cdc = CoreDataController()
        items = cdc.getNavigationData()
        currentUser = cdc.getUserData()
    }

    func setupSidebar() {
        self.view.backgroundColor = UIColor.hexColor("#fff")
        navigationContainer.backgroundColor = UIColor.hexColor("#2a2a2a")
        SideMenuController.preferences.basic.statusBarBehavior = .hideOnMenu
        SideMenuController.preferences.basic.position = .above
        SideMenuController.preferences.basic.direction = .left
        SideMenuController.preferences.basic.enablePanGesture = true
        SideMenuController.preferences.basic.menuWidth = 275
        sideMenuController?.delegate = self
    }

    static func createViewController() -> NavigationViewController {
        let sb = UIStoryboard(name: "StudentHomeModuleStoryboard", bundle: nil)
        let vc = sb.instantiateViewController(withIdentifier: "NavigationViewController")
        return vc as! NavigationViewController
    }
}

--UPDATE-- - 更新 -

updated setupTableLayout function -更新的 setupTableLayout 功能 -

func setupTableViews() {
        let bundle = Bundle(for: type(of: self))
        let cellNib = UINib(nibName: "SideMenuTableItem", bundle: bundle)
        menuTable.register(cellNib, forCellReuseIdentifier: "SideMenuTableItem")
        menuTable.register(SideMenuTableItem.self, forCellReuseIdentifier: "SideMenuTableItem")
        menuTable.reloadData()
    }

网点

After breaking into chat on this, we found that there were two issues.就这个问题闯入聊天后,我们发现有两个问题。

The first issue was the missing reloadData call mentioned above.第一个问题是上面提到的缺少reloadData调用。 That was causing cellForRow to not be called.这导致cellForRow不被调用。 Adding reloadData corrected that issue, but then the custom cell class's outlets were nil, causing a crash in setItemData .添加reloadData纠正了该问题,但随后自定义单元格类的出口为零,导致setItemData崩溃。

The second issue was that register(_:forCellReuseIdentifier:) was being called in code, but the custom cell was already setup as part of the Interface Builder UITableView declaration.第二个问题是在代码中调用了register(_:forCellReuseIdentifier:) ,但是自定义单元格已经设置为 Interface Builder UITableView声明的一部分。 Calling register again on the custom class re-registered the reuseIdentifier , disconnecting the outlets set up in the storyboard.在自定义类上再次调用register重新注册了重用reuseIdentifier ,断开了故事板中设置的插座。

Removing the register call and adding reloadData solved all issues.删除register调用并添加reloadData解决了所有问题。

You are never calling setupTableViews() .您永远不会调用setupTableViews() You'r code should look like this:您的代码应如下所示:

class NavigationViewController: UIViewController, SideMenuControllerDelegate {

override func viewDidLoad() {
        super.viewDidLoad()

        setupTableViews()
    }

func setupTableViews() {
menuTable.reloadData()
    }
}

extension NavigationViewController: UITableViewDataSource, UITableViewDelegate {

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SideMenuTableItem", for: indexPath as IndexPath) as! SideMenuTableItem
        cell.setItemData(items[indexPath.row])
        return cell
    }

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

You are never calling the function, nor calling viewDidLoad.您永远不会调用该函数,也不会调用 viewDidLoad。 This should help.这应该有帮助。 Also, where is the rest of your view controller code (is this all of it? It should not be!).此外,您的视图控制器代码的其余部分在哪里(这就是全部吗?不应该!)。

You don't need to register your cell because you requested it and make sure you reloadData().您不需要注册您的单元格,因为您已请求它并确保您 reloadData()。

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM