无法在侧边菜单中填充tableview单元格

时间:2019-05-15 08:03:47

标签: swift uitableview menubar

我正在创建一个左侧的侧边菜单。这个菜单有一个表格视图,每个单元格在按下时都会显示一个新的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")
    }

}

当我按下左侧的NavigationbarButton时,菜单弹出,可以看到表格视图,但是所有单元格都是空的。

我要包含这些单元格

  • 一个
  • 两个
  • 三个
  • 四个
  • 五个

容器的想法是在表视图的顶部给som空间,以便在其中放置som其他信息。

1 个答案:

答案 0 :(得分:3)

您需要在self视图控制器的delegate方法中将dataSource设置为SideMenushowSettings

tableview.register(MenuOptionCell.self, forCellReuseIdentifier: reuseIdentifier)
tableview.delegate = self
tableview.datasource = self