简体   繁体   English

删除行时TableView“无法识别的选择器发送到实例”错误

[英]TableView “unrecognized selector sent to instance” error when deleting row

I have a button that deletes the cell it resides within. 我有一个按钮,删除它所在的单元格。 The data model is working fine and tableView.reloadData() causes no errors. 数据模型工作正常, tableView.reloadData()不会导致错误。 When I use tableView.deleteRowsAtIndexPaths(..) however, a crash occurs that tells me the following: 但是,当我使用tableView.deleteRowsAtIndexPaths(..)时,会发生崩溃,告诉我以下内容:

-[__NSCFNumber row]: unrecognized selector sent to instance 0xb000000000000003
2014-10-29 03:37:08.999 SampleApp[4915:82542] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber row]: unrecognized selector sent to instance 0xb000000000000003'

The code that indicates where this occurs is shown below. 指示发生这种情况的代码如下所示。 I have got a feeling it is related to my Notification, but I don't know why. 我觉得这与我的通知有关,但我不知道为什么。 I tried using nil as the argument for object when posting the notification, but that doesn't make a difference. 我在发布通知时尝试使用nil作为对象的参数,但这没有什么区别。 I don't see why tableView.reloadData() would work, while the alternative method wouldn't. 我不明白为什么tableView.reloadData()可以工作,而替代方法则不行。 How can I stop this from occurring? 我怎么能阻止这种情况发生?

GoalTableViewCell class which inherits from UITableViewCell (excerpt) 继承自UITableViewCell的GoalTableViewCell类(摘录)

@IBAction func deleteButtonTapped(sender: AnyObject) { //IBAction is connected to a delete button within the cell.
        let index = self.indexInTable //indexInTable is an Int property that tracks where the cell is located in the tableView. It is updated using cellForRowAtIndexPath(..)
        var userInfo = ["index" : index]
        NSNotificationCenter.defaultCenter().postNotificationName("DeletedGoal", object: self, userInfo: userInfo)
    }

GoalsTableViewController class which inherits from UIViewController (and has a tableView in it) (excerpt) 从UIViewController继承的GoalsTableViewController类(并且在其中有一个tableView)(摘录)

    func deletedGoal(notification : NSNotification) {
        if (notification.name == "DeletedGoal") {
            var userInfo = notification.userInfo!
            var index = userInfo["index"] as Int //this is returning the proper index.

            goalsArray.removeAtIndex(index)
            saveGoals(goalsArray)
            //self.tableView.reloadData() //this works.
            self.tableView.deleteRowsAtIndexPaths([index], withRowAnimation: UITableViewRowAnimation.Fade) //this causes error

        }
    }

Edit: I corrected the code (much appreciated guys) but am met with a new error. 编辑:我更正了代码(非常感谢大家),但遇到了新的错误。 I may just open a new question for this: After deleting a higher number of cells in a row I eventually get a crash telling me the indexPath is too high. 我可能只是为此打开一个新问题:在连续删除更多数量的单元格后,我最终得到一个崩溃告诉我indexPath太高了。 I am guessing cellForRowAtIndexPath(..) is the issue, it does not mark the cell's indexInTable property unless scrolling occurs(?) 我猜是cellForRowAtIndexPath(..)是问题,它不标记单元格的indexInTable属性,除非发生滚动(?)

    //correct code
   func deletedGoal(notification : NSNotification) {
            if (notification.name == "DeletedGoal") {
                var userInfo = notification.userInfo!
                //var cell = userInfo["cell"] as GoalTableViewCell
            var index = userInfo["index"] as Int

            println(index)
            goalsArray.removeAtIndex(index)
            saveGoals(goalsArray)
            let indexPath = NSIndexPath(forRow:index,inSection:0)
            self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
            //self.tableView.reloadData()

        }
    }

If you look at the exception message you will see [__NSCFNumber row] which tells you there was an attempt to invoke the row method on an NSCFNumber (which is an internal class for an NSNumber ) - which doesn't have a row method. 如果查看异常消息,您将看到[__NSCFNumber row] ,它告诉您尝试在NSCFNumber(这是NSNumber的内部类)上调用row方法 - 它没有行方法。

Now look at self.tableView.deleteRowsAtIndexPaths([index], withRowAnimation: UITableViewRowAnimation.Fade) - The method name tells you (or you could read the documentation) that it wants an array of NSIndexPath s not NSNumber s 现在看看self.tableView.deleteRowsAtIndexPaths([index], withRowAnimation: UITableViewRowAnimation.Fade) - 方法名称告诉你(或者你可以阅读文档)它想要一个NSIndexPath数组而不是NSNumber s

So, what you wanted to say was 所以,你想说的是

self.tableView.deleteRowsAtIndexPaths([NSIndexPath(forRow:index inSection:0)], withRowAnimation:UITableViewRowAnimation.Fade)

This is assuming your table only has one section. 这假设您的表只有一个部分。 If it has more than one section then you will need to keep track of the section number in your cell as well as the row. 如果它有多个部分,那么您需要跟踪单元格和行中的部分编号。 You could do this by changing the property you store to be the indexPath rather than IndexPath.row . 您可以通过将存储的属性更改为indexPath而不是IndexPath.row来完成此IndexPath.row

Then you can say 然后你可以说

@IBAction func deleteButtonTapped(sender: AnyObject) { //IBAction is connected to a delete button within the cell.
        let index = self.indexInTable //indexInTable is an NSIndexPath property that tracks where the cell is located in the tableView. It is updated using cellForRowAtIndexPath(..)
        var userInfo = ["index" : index]
        NSNotificationCenter.defaultCenter().postNotificationName("DeletedGoal", object: self, userInfo: userInfo)
    }

func deletedGoal(notification : NSNotification) {
    if (notification.name == "DeletedGoal") {
        var userInfo = notification.userInfo!
        var index = userInfo["index"] as NSIndexPath

        goalsArray.removeAtIndex(index.row)
        saveGoals(goalsArray)
        //self.tableView.reloadData() //this works.
        self.tableView.deleteRowsAtIndexPaths([index], withRowAnimation: UITableViewRowAnimation.Fade) //this causes error

    }
}

The problem is most likely that var index is set to be typeof Int. 问题很可能是var index设置为int类型。

However, the methode deleteRowsAtIndexPaths expects an object of type NSIndexPath , since internally it will call the selector row on the passed path. 但是,methode deleteRowsAtIndexPaths需要一个NSIndexPath类型的对象,因为它在内部将调用传递路径上的选择器行。

In your case the selector is called on an Int, which does not respond to the row selector. 在您的情况下,选择器在Int上调用,它不响应行选择器。 Create an indexpath with index as your row first and you'll be good to go. 创建一个索引路径,首先将索引作为您的行,您将很高兴。 :D :d

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 填充Tableview时将无法识别的选择器发送到实例 - unrecognized selector sent to instance when populating a Tableview 故事板上的2个表格视图的cellForRowAtIndexPath,无法识别的选择器发送到实例错误 - cellForRowAtIndexPath for 2 tableview in a storyboard, unrecognized selector sent to instance error [ComponentTaleView tableView:heightForFooterInSection:]:无法识别的选择器已发送到实例 - [ComponentTaleView tableView:heightForFooterInSection:]: unrecognized selector sent to instance tableView:numberOfRowsInSection:]: 无法识别的选择器发送到实例 - tableView:numberOfRowsInSection:]: unrecognized selector sent to instance TableView在reloadData上崩溃 - '无法识别的选择器发送到实例' - TableView Crashes on reloadData - 'unrecognized selector sent to instance' [UIViewController tableView:numberOfRowsInSection:]: 无法识别的选择器发送到实例 - [UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 无法识别的选择器已发送到实例tableview swift - Unrecognized selector sent to instance tableview swift Xcode TableView-无法识别的选择器发送到实例 - Xcode TableView - unrecognized selector sent to instance -[UIViewController tableView:numberOfRowsInSection:]: 无法识别的选择器发送到实例 - -[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 错误:无法识别的选择器发送到实例 - Error: unrecognized selector sent to instance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM