【问题标题】:Unrecognized selector sent to instance for reloadData of tableView after delay延迟后将无法识别的选择器发送到 tableView 的 reloadData 实例
【发布时间】:2020-03-25 17:30:57
【问题描述】:

collectionViewCell 中的我的 tableView 当我调用 collectionViewCell.tableview.perforSelector() 我的应用程序崩溃并出现以下异常。

由于未捕获的异常“NSInvalidArgumentException”而终止应用,原因:“-[UITableView DataOnUI]:无法识别的选择器已发送到实例 0x7ff267876600”

我的应用程序在此行崩溃:- "self.tblTopics?.perform(Selector("DataOnUI"), on: .main, with: nil, waitUntilDone: true)"

代码:

//****************************************************//
extension SelectedCategoriesCell: UITableViewDataSource, UITableViewDelegate {

    //----------------------------------------------
    func setUpTableView(){
        tblTopics?.rowHeight = UITableView.automaticDimension
        tblTopics?.estimatedRowHeight = 44
        tblTopics?.allowsMultipleSelection = true
        lcHeightOftblTopicsTable?.constant = 0
        setUpTableData()
    }

    //----------------------------------------------
    func setUpTableData(){
        tblTopics?.dataSource = self
        tblTopics?.delegate = self
        self.tblTopics?.reloadData()
        self.tblTopics?.perform(Selector("DataOnUI"), on: .main, with: nil, waitUntilDone: true)

    }

    func DataOnUI() {
        self.lcHeightOftblTopicsTable?.constant = self.tblTopics?.contentSize.height ?? 0
        self.tblTopics?.reloadData()
        self.layoutIfNeeded()
        self.setNeedsDisplay()
    }

    //----------------------------------------------
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arrayAllTopic?.count ?? 0
    }

    //----------------------------------------------
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }

    //----------------------------------------------
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let cell = tableView.dequeueReusableCell(withIdentifier: TopicsCell.className, for: indexPath) as? TopicsCell {
            if let jsonObject = arrayAllTopic?[indexPath.row] {
                var topicModel = Model.ListAllCategories(json: jsonObject)
                cell.lblTopicTitle?.text = topicModel.name
                return cell
            }
        }
        return UITableViewCell()
    }

    //----------------------------------------------
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        var jsonData = arrayAllTopic?[indexPath.row]
            jsonData?[api.kisSelected] = true
        arrayAllTopic?[indexPath.row] = jsonData!
        print("\(arrayAllTopic)")

    }

    //----------------------------------------------
    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
         var jsonData = arrayAllTopic?[indexPath.row]
            jsonData?[api.kisSelected] = false
        arrayAllTopic?[indexPath.row] = jsonData!
        print("\(arrayAllTopic)")
    }


}

谢谢你的建议。。

【问题讨论】:

  • func DataOnUI()SelectedCategoriesCell 上,所以不应该是self.perform(...),这意味着应该执行该方法的self(这是一个SelectedCategoriesCell)实例。跨度>

标签: ios swift ios13 swift5 xcode11


【解决方案1】:

首先,永远不要将选择器的名称写成字符串:Selector("DataOnUI")。你不知道怎么做,即使你知道,编译器也比你知道的多。使用#selector 语法:#selector(DataOnUI)

其次,当你这样做时,编译器会告诉你func DataOnUI() 需要一个@objc 标记来帮助你。

第三,不要再以大写字母开头func 的名称,只是因为。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-25
    • 1970-01-01
    • 1970-01-01
    • 2019-08-28
    • 2012-07-24
    相关资源
    最近更新 更多