【发布时间】:2021-09-02 02:56:13
【问题描述】:
我有一个带有 CollectionView 的视图控制器,其中包含图像。
当我尝试使用 TableView 向其添加SideMenu 时,点击条形按钮'NSInvalidArgumentException', reason: 'must pass a class of kind UITableViewCell' 时出现以下错误
我正在使用本教程来实现this
这个错误有什么原因吗?我应该改变什么?
这是我的 ViewController 类
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
}
}
【问题讨论】:
-
在
viewDidLoad的MenuListController中,您必须注册UITableViewCell而不是UITableView,添加tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellmenu")这就是您收到错误的原因 -
天才!谢谢!
标签: ios swift xcode uitableview side-menu