Swift - Adding long press gesture recognizer in custom cell

百般思念 提交于 2019-12-05 04:58:37

问题


So I have a custom cell class and a xib file for my cells in my tableview, and I want to add a long press recognition to the custom cell.

I tried to drag and drop the long press recognizer to the cell in the xib-file, make the file's owner the gesture's delegate, and then "drag and add" it from the document outline to the custom class swift-file. I also added the UIGestureRecognizerDelegate to the custom cell class.

Now when I run the app I get a Thread 1: signal SIGABRT error, and honestly I have no idea why. This is the first time I use the gesture recognizer so sorry if there's an obvious mistake or something I should've done.

Any suggestions on how to proceed would be appreciated.

EDIT: As Logan suggested I undid everything that was done and followed his steps. The code in my custom class now looks like this:

@IBAction func pressed(sender: AnyObject) {

    func handleLongPress(sender: UILongPressGestureRecognizer) {
        if sender.state == .Began {
            println("Received longPress!")
        }
    }
}

But I still get the error. What I noticed is that if I just add the gesture to the xib-file and run the app, I get the error. I don't know if that is because I haven't connected it to the custom class file yet, but I thought I'd just throw it out there.

EDIT 2: After changing my code to this:

@IBAction func pressed(sender: UILongPressGestureRecognizer) {
    if sender.state == .Began {
        println("Received longPress!")
    }
}

I still get the error.

I don't know if I made this clear, but I can't even load the screen, it just crashes right away. So I'm thinking this is a delegation/reference issue, here's how my references are:

1) I set the class of my xib-file owner to the custom cell class. 2) I dragged the gesture recognizer on the cell. 3) I dragged the gesture object to the file-owner so that the file owner is its delegate. 4) I added the code from above in my custom class. 5) I dragged the gesture object to the file-owner so that the gesture is "connected to the code" in the custom class.

These are all my steps, and as I said, I suspect there's a delegate issue somewhere.


回答1:


I had a similar issue. The problem is that when you add a gesture recognizer to the xib file, there are multiple objects (your tableview cell and the gesture recognizer) and the tableview datasource cannot handle this. The error describes the problem correctly. I moved my tableview cell to the main storyboard instead of keeping it in a separate xib file to resolve this issue.

reason: 'invalid nib registered for identifier (CELL) - nib must contain exactly one top level object which must be a UITableViewCell instance'

Another solution to your problem would be to create the gesture recognizer in code like:

self.tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapFired:)];

in the awakeFromNib for the custom table view cell.

I just think the basic issue is that you cannot add gesture recognizers in a table view cell xib and then have the tableview data source load the cell.



来源:https://stackoverflow.com/questions/27788001/swift-adding-long-press-gesture-recognizer-in-custom-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!