Reputation: 86
I'm currently doing an app in swift and doing a menubar that scrolls from the right of the screen and display a list of possibilities.
I created the UITableView on my storyboard as a view that I sticked over the Navigation Controller.
I want now to add cells to this tableview to create a navigation bar, but I don't know how do it since there's no controller managing the item menuView. Here is my ViewControllerClass:
import CoreData
class ViewController: UIViewController {
@IBOutlet weak var menuView: UITableView!
var menuShowing = false
@IBOutlet weak var leadingConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
menuView.layer.shadowOpacity = 1
menuView.layer.shadowRadius = 6
}
@IBAction func openMenu(_ sender: Any) {
if (menuShowing)
{
leadingConstraint.constant = -200
UIView.animate(withDuration: 0.3, animations: {self.view.layoutIfNeeded()})
} else {
leadingConstraint.constant = 0
UIView.animate(withDuration: 0.3, animations: {self.view.layoutIfNeeded()})
}
menuShowing = !menuShowing
}
}
My questions are : Is my menuView object well done or should I reconsider my previous work?
Can I add a cell in the menuView object simply with a simple function like: MenuView.addRow( * something *)?
Upvotes: 1
Views: 552
Reputation: 679
You can set the UITableViewDataSource and if you want to UITableViewDelegate to ViewController to make the tableView be controlled by ViewController.
import CoreData
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var menuView: UITableView!
var menuShowing = false
@IBOutlet weak var leadingConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
menuView.layer.shadowOpacity = 1
menuView.layer.shadowRadius = 6
menuView.dataSource = self
menuView.delegate = self
}
// Handle TableView like you normally do:
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return UITableViewCell()
}
@IBAction func openMenu(_ sender: Any) {
if (menuShowing)
{
leadingConstraint.constant = -200
UIView.animate(withDuration: 0.3, animations: {self.view.layoutIfNeeded()})
} else {
leadingConstraint.constant = 0
UIView.animate(withDuration: 0.3, animations: {self.view.layoutIfNeeded()})
}
menuShowing = !menuShowing
}
}
Upvotes: 1