简体   繁体   English

将UITapGestureRecognizer添加到XIB

[英]Adding UITapGestureRecognizer to XIB

I've created a custom XIB extending from UITableViewHeaderFooterView and attempting to add a gesture recognizer. 我创建了一个从UITableViewHeaderFooterView扩展的自定义XIB,并尝试添加一个手势识别器。 Only problem is attempting to add the recognizer via interface builder results in the object being added to the top level hierarchy and the following error causing my app to crash: 唯一的问题是尝试通过接口构建器添加识别器导致对象被添加到顶层层次结构,并且以下错误导致我的应用崩溃:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'invalid nib registered for identifier (AccordionHeader) - nib must contain exactly one top level object which must be a UITableViewHeaderFooterView instance' 由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“为标识符(AccordionHeader)注册的笔尖无效-笔尖必须仅包含一个顶级对象,该对象必须是UITableViewHeaderFooterView实例”

To the best of my knowledge there's no "viewDidLoad" equivalent available or I would just be adding the recognizer programmatically. 据我所知,没有可用的“ viewDidLoad”等效项,否则我将以编程方式添加识别器。 Is there another way to do this? 还有另一种方法吗?

You are right, the recognizer must not be added to the XIB on top level. 是的,不能将识别器添加到顶层的XIB中。

My solution as follows: 我的解决方法如下:

class SettingsUserAvatarHeader: UITableViewHeaderFooterView {

    // Set as a variable, as it will be re-created on cell re-use
    var tapGestureRecognizer = UITapGestureRecognizer()

    // This will be called every time the cell moves off screen and returns 
    override func prepareForReuse() {
        super.prepareForReuse()

        // Needs to be done manually.
        tapGestureRecognizer = UITapGestureRecognizer()
        avatarImageView.gestureRecognizers = [ tapGestureRecognizer ]
    }

    // This will be needed for the first display
    override func didMoveToSuperview() {
        super.didMoveToSuperview()

        avatarImageView.gestureRecognizers = [ tapGestureRecognizer ]
    }
}

With this, you can listen to the taps directly. 这样,您可以直接聆听水龙头。 I'm using RxSwift: 我正在使用RxSwift:

First, add a extension for the tap: 首先,为水龙头添加一个扩展名:

extension Reactive where Base: SettingsUserAvatarHeader {

    var avatarTap: ControlEvent<UITapGestureRecognizer> {
        return self.base.tapGestureRecognizer.rx.event.asControlEvent()
    }

}

And in your controller/delegate etc: 并在您的控制器/委托等中:

class Consumer: UITableViewDelegate {

    var avatarTapDisposable: Disposable?

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let cell = tableView.dequeueReusableHeaderFooterView(withIdentifier: "userAvatarView")
        let view = cell as! SettingsUserAvatarHeader

        avatarTapDisposable =
                view.rx
                    .avatarTap
                    .subscribe(onNext: { (tap) in
                        // Here your code for the tap
                    })

        return cell
    }
}

Try This 尝试这个

Step 1:- Register XIB file 步骤1:-注册XIB文件

func regXib(){
 self.tblList.register(UINib(nibName: "YourCellName", bundle: nil), forHeaderFooterViewReuseIdentifier: "YourCellNameIdentifierName")
}

Step:- 2 viewForHeaderInSection Method 步骤:-2 viewForHeaderInSection方法

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let cell = tableView.dequeueReusableHeaderFooterView(withIdentifier: "YourCellNameIdentifierName") as! YourCellName
        let recognizer = UITapGestureRecognizer(target: self, action: #selector(self.expand))
        cell.layer.backgroundColor = UIColor.white.cgColor
        cell.addGestureRecognizer(recognizer)
        cell.tag = section
        return cell
    }

Step 3:- Tap Gesture Action 步骤3:-轻按手势动作

@objc func expand(sender:UITapGestureRecognizer){
   let tag = (sender.view?.tag)!
   // Tap Action Code Here 
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM