【问题标题】:CocoPod SideMenu not working when clicking menu单击菜单时可可豆荚侧菜单不起作用
【发布时间】: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
    }
    
    
}

【问题讨论】:

  • viewDidLoadMenuListController 中,您必须注册UITableViewCell 而不是UITableView,添加tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellmenu") 这就是您收到错误的原因
  • 天才!谢谢!

标签: ios swift xcode uitableview side-menu


【解决方案1】:

您必须注册UITableViewCell 而不是UITableView,这就是您收到错误的原因

MenuListController 中的viewDidLoad()

添加

tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellmenu")

而不是

tableView.register(UITableView.self, forCellReuseIdentifier: "cellmenu")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    相关资源
    最近更新 更多