martin
martin

Reputation: 1974

Swift - Adding long press gesture recognizer in custom cell

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.

Upvotes: 2

Views: 3440

Answers (2)

Owen Zhao
Owen Zhao

Reputation: 3355

1) I set the class of my xib-file owner to the custom cell class.

You are wrong at the very beginning. A UITableViewCell is a subclass of UIView. So you should set View to the custom cell class, not the file's owner. The file's owner should be NSObject.

You can only set file's owner as your custom class if it is the subclass of UIViewController.

Upvotes: 0

user3435374
user3435374

Reputation: 1296

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.

Upvotes: 3

Related Questions