I have a View Controller with a CollectionView with images in it.
When I try adding a SideMenu to it with a TableView I get the following error when tapping the bar button 'NSInvalidArgumentException', reason: 'must pass a class of kind UITableViewCell'
I am using this tutorial to implement this
Any reason for this error? What should I change?
Here is my ViewController Class
import SideMenu
import UIKit
class ViewController: UIViewController {
var menu: SideMenuNavigationController?
@IBOutlet weak var collectionView: UICollectionView!
var imgArr = ["1","2","3"]
override func viewDidLoad() {
super.viewDidLoad()
menu = SideMenuNavigationController(rootViewController: MenuListController())
menu?.leftSide = true
SideMenuManager.default.leftMenuNavigationController = menu
SideMenuManager.default.addPanGestureToPresent(toView: self.view)
}
@IBAction func didTapMenu(){
present(menu!, animated: true)
}
}
extension ViewController : UICollectionViewDataSource, UICollectionViewDelegate{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imgArr.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? DataCollectionViewCell
cell?.img.image = UIImage(named:imgArr[indexPath.row])
return cell!
}
}
class MenuListController:UITableViewController{
var items = ["First", "Second"]
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UITableView.self, forCellReuseIdentifier: "cellmenu")
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellmenu", for: indexPath)
cell.textLabel?.text = items[indexPath.row]
return cell
}
}
You have to register UITableViewCell
not UITableView
, that's why you getting the error
In viewDidLoad()
of MenuListController
,
add
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellmenu")
instead of
tableView.register(UITableView.self, forCellReuseIdentifier: "cellmenu")