Swift - 在自定义单元格中添加长按手势识别器 [英] Swift - Adding long press gesture recognizer in custom cell

查看:41
本文介绍了Swift - 在自定义单元格中添加长按手势识别器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我的表格视图中有一个自定义单元格类和一个用于单元格的 xib 文件,并且我想为自定义单元格添加长按识别.

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.

我尝试将长按识别器拖放到 xib 文件中的单元格,使文件的所有者成为手势的委托,然后将其从文档大纲拖放"到自定义类 swift 文件.我还将 UIGestureRecognizerDelegate 添加到自定义单元格类.

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.

现在,当我运行应用程序时,我收到一个线程 1:信号 SIGABRT 错误,老实说我不知道​​为什么.这是我第一次使用手势识别器,如果有明显的错误或我应该做的事情,请见谅.

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.

正如洛根建议的那样,我撤消了所做的一切并按照他的步骤进行.我的自定义类中的代码现在如下所示:

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!")
        }
    }
}

但我仍然收到错误消息.我注意到的是,如果我只是将手势添加到 xib 文件并运行应用程序,则会出现错误.我不知道是不是因为我还没有将它连接到自定义类文件,但我想我只是把它扔在那里.

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.

编辑 2:将我的代码更改为此后:

EDIT 2: After changing my code to this:

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

我仍然收到错误消息.

我不知道我是否说清楚了,但我什至无法加载屏幕,它立即崩溃了.所以我认为这是一个委托/参考问题,以下是我的参考资料:

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) 我将我的 xib 文件所有者的类设置为自定义单元类.2)我在单元格上拖动手势识别器.3)我将手势对象拖到文件所有者,以便文件所有者是其委托.4)我在自定义类中添加了上面的代码.5) 我将手势对象拖到文件所有者处,以便手势在自定义类中连接到代码".

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.

推荐答案

我遇到了类似的问题.问题是,当您向 xib 文件添加手势识别器时,有多个对象(您的 tableview 单元格和手势识别器)并且 tableview 数据源无法处理此问题.该错误正确描述了问题.我将我的 tableview 单元格移动到主故事板,而不是将其保存在单独的 xib 文件中以解决此问题.

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.

原因:为标识符 (CELL) 注册的笔尖无效 - 笔尖必须恰好包含一个必须是 UITableViewCell 实例的顶级对象"

您的问题的另一种解决方案是在如下代码中创建手势识别器:

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

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

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

在自定义表格视图单元格的awakeFromNib中.

in the awakeFromNib for the custom table view cell.

我只是认为基本问题是您不能在表格视图单元格 xib 中添加手势识别器,然后让表格视图数据源加载单元格.

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.

这篇关于Swift - 在自定义单元格中添加长按手势识别器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆