无法在侧边菜单中填充表格视图单元格

Can not populate tableview cells in side menu

我正在创建一个将从左侧出现的侧边菜单。这个菜单有一个表格视图,每个单元格在按下时都会显示一个新的 ViewController。

我想从所有视图控制器访问此侧边菜单,因此无论我在哪个视图控制器中它看起来都一样。

我猜这与 delegatedatasource 有关,但我无法将它们设置为 self。至少我不知道在哪里

我的侧边菜单:

import UIKit

private let reuseIdentifier = "SideMenuCell"

class SideMenu: UIViewController, UITableViewDelegate, UITableViewDataSource {

    let menuItems = ["One", "Two", "Three", "Four", "Five"]
    let blackView = UIView()

    let tableview: UITableView = {
        let table = UITableView()
        table.backgroundColor = .white
        table.separatorColor = .clear
        return table
    }()

    let container: UIView = {
       let con = UIView()
        con.backgroundColor = Colors.main
        return con
    }()

    @objc func showSettings() {

        if let window = UIApplication.shared.keyWindow {

            let menuWidth = window.frame.width * 0.7

            blackView.backgroundColor = UIColor(white: 0, alpha: 0.5)

            blackView.addTapGestureRecognizer {

                self.handleDismiss()
            }

            tableview.register(MenuOptionCell.self, forCellReuseIdentifier: reuseIdentifier)

            window.addSubview(blackView)
            window.addSubview(container)
            window.addSubview(tableview)

            tableview.anchor(left: container.leftAnchor, bottom: container.bottomAnchor, right: container.rightAnchor, height: window.frame.height * 0.85)

            container.frame = CGRect(x: 0, y: 32, width: 0, height: window.frame.height)

            blackView.frame = window.frame
            blackView.alpha = 0

            UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
                self.blackView.alpha = 1
                self.container.frame = CGRect(x: 0, y: 0, width: menuWidth, height: window.frame.height)

            }, completion: nil)

        }

    }

    func handleDismiss() {
        if let window = UIApplication.shared.keyWindow {
            UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
                self.blackView.alpha = 0
                self.container.frame = CGRect(x: 0, y: 32, width: 0, height: window.frame.height)

            }, completion: nil)
        }

    }


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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as! MenuOptionCell
        cell.descriptionLabel.text = menuItems[indexPath.row]
//        cell.iconImageView.image  = image
        return cell
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 40
    }
}

和自定义单元格:

import UIKit

class MenuOptionCell: UITableViewCell {

    // Mark: - Properties

    let iconImageView: UIImageView = {
        let iv = UIImageView()
        iv.contentMode = .scaleAspectFit
        iv.clipsToBounds = true
        return iv
    }()

    let descriptionLabel: UILabel = {
       let label = UILabel()
        label.textColor = .black
        label.font = UIFont.systemFont(ofSize: 16)
        label.text = "Sample Text"
        return label
    }()

    // Mark: - Init

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

        backgroundColor = .clear
        selectionStyle = .none

        addSubview(iconImageView)
        addSubview(descriptionLabel)
        iconImageView.anchor(top: topAnchor, left: leftAnchor, right: rightAnchor)
        descriptionLabel.anchor(top: topAnchor, left: iconImageView.rightAnchor, bottom: bottomAnchor, right: rightAnchor)

    }


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

}

当我按下左导航栏按钮时,菜单出现并且我可以看到表格视图,但所有单元格都是空的。

我希望单元格包含

容器的想法是将 som space 从 tableview 放在顶部,我可以在其中放置一些其他信息。

您需要在 SideMenu 视图控制器的 showSettings 方法

中将 self 设置为 delegatedataSource
tableview.register(MenuOptionCell.self, forCellReuseIdentifier: reuseIdentifier)
tableview.delegate = self
tableview.datasource = self