【问题标题】:Can not populate tableview cells in side menu无法在侧边菜单中填充表格视图单元格
【发布时间】:2019-05-15 16:07:22
【问题描述】:

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

}

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

我希望单元格包含

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

容器的想法是在 tableview 的顶部留出一些空间,我可以在其中放置一些其他信息。

【问题讨论】:

    标签: swift uitableview menubar


    【解决方案1】:

    您需要在SideMenu视图控制器的showSettings方法中将self设置为delegatedataSource

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

    【讨论】:

    • 我这样做了,可以在“调试视图层次结构”中看到单元格在那里。但是文本标签仍然没有显示。可能是锚有问题?
    • @maniponken 看来您有一个名为anchorUIView extension 方法。显示该方法
    • @maniponken 并在tableview numberofrows、cellforrow方法中使用print语句并确认这些方法被调用
    • @maniponken 检查 iconImageView, descriptionLabel frame in view hierarchy
    • anchor 函数太长。在这里发帖,但是是这样的:func anchor(top: NSLayoutYAxisAnchor? = nil, left: NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, right: NSLayoutXAxisAnchor? = nil, paddingTop: CGFloat? = 0, paddingLeft: CGFloat ? = 0, paddingBottom: CGFloat? = 0, paddingRight: CGFloat? = 0, width: CGFloat? = nil, height: CGFloat? = nil) { translatesAutoresizingMaskIntoConstraints = false if let top = top { topAnchor.constraint(equalTo: top,常量:paddingTop!).isActive = true } 等等...
    猜你喜欢
    • 2020-09-12
    • 1970-01-01
    • 1970-01-01
    • 2020-05-19
    • 1970-01-01
    • 2014-01-30
    • 1970-01-01
    • 2012-11-24
    • 1970-01-01
    相关资源
    最近更新 更多