I'm creating a Side-menu that will come out from the left. This menu has a tableview and each cell will present a new ViewController when pressed.
I want to access this Side-menu from all view controllers so it looks the same no matter in which view controller I am.
My guess is that this has something to do with the delegate
and datasource
but I can't set them to self
. At least I don't know where
My Side-menu:
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
}
}
and the Custom cell:
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")
}
}
when I press the left navigationbarButton the menu comes out and I can see the tableview, but all the cells are empty.
I want the cells to contain
The idea with the container is to give som space to the top from the tableview where I could put som other information maybe.
You need to set self
as delegate
and dataSource
in SideMenu
view controller's showSettings
method
tableview.register(MenuOptionCell.self, forCellReuseIdentifier: reuseIdentifier)
tableview.delegate = self
tableview.datasource = self