UITapGestureRecognizer 不工作 - Swift 2

UITapGestureRecognizer not working - Swift 2

我是 iOS 的初学者,正在使用 swift 2。 我基于这个视频: https://www.youtube.com/watch?v=itbok1NZvss (基于 SWIFT 3)创建一个可扩展的 UITable 。但是 UITableViewCell 当我点击时没有展开。在这种情况下,UITapGestureRecognizer 不起作用。

我的代码:

struct  Section {
var genre: String!
var movies: [String]!
var expanded: Bool!

init(genre: String , movies: [String] , expanded: Bool)
{
    self.expanded = expanded
    self.genre = genre
    self.movies = movies
}   }

ExpandableHeaderView class :

protocol ExpandableHeaderViewDelegate
{
    func toggleSection(header: ExpandableHeaderView , section: Int)
}

class ExpandableHeaderView: UITableViewHeaderFooterView {

    var delegate: ExpandableHeaderViewDelegate?
    var section: Int!

    override init(reuseIdentifier: String?)
    {
        super.init(reuseIdentifier: reuseIdentifier)

        //self.userInteractionEnabled = true

        self.addGestureRecognizer(UIGestureRecognizer(target: self  ,  action: Selector("selectHeaderAction:")))
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func selectHeaderAction(gestureRecognizer: UITapGestureRecognizer)
    {
        let cell = gestureRecognizer.view as! ExpandableHeaderView
        delegate?.toggleSection(self , section: cell.section)

        print("selectHeaderAction")

    }

    func customeInit(title: String, section: Int , delegate: ExpandableHeaderViewDelegate)
    {
        self.textLabel?.text = title
        self.section = section
        self.delegate = delegate
    }
    override func layoutSubviews()
    {
        super.layoutSubviews()
        self.textLabel?.textColor = UIColor.whiteColor()
        self.contentView.backgroundColor = UIColor.darkGrayColor()
    }
}

ViewController:

class ViewController: UIViewController , UITableViewDelegate , UITableViewDataSource , ExpandableHeaderViewDelegate{

    @IBOutlet weak var tableView: UITableView!


    var sections = [
        Section(genre: "1" , movies: ["dfgdgd" , "dgdgdg" , "jsgdhgdg"] , expanded: true ) ,
        Section(genre: "2" , movies: ["dfgdgd" , "dgdgdg"] , expanded: false ) ,
        Section(genre: "3" , movies: ["dfgdgd" , "dgdgdg"] , expanded: true )
    ]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return sections.count

    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sections[section].movies.count
    }

    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 44
    }
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        if(sections[indexPath.section].expanded == true)
        {
         return 44
        }
        else
        {
        return 0
        }
    }

    func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 2
    }

    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        let header = ExpandableHeaderView()
        header.customeInit(sections[section].genre, section: section, delegate: self)
        return header
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("labelCell")
        cell!.textLabel?.text = sections[indexPath.section].movies[indexPath.row]
        return cell!
    }

    func toggleSection(header: ExpandableHeaderView, section: Int) {
        sections[section].expanded = !sections[section].expanded

        tableView.beginUpdates()

        for i in 0 ..< sections[section].movies.count
        {
            tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: i, inSection: section)], withRowAnimation: UITableViewRowAnimation.Automatic)
        }
        tableView.endUpdates()
    }
}

你需要指定你想要什么样的手势,这里你要添加一个UITapGestureRecognizer:

self.addGestureRecognizer(UITapGestureRecognizer(target: self  ,  action: Selector("selectHeaderAction:")))

尝试 this.its 对我来说效果很好。 你只需将 target 传递给你添加点击手势的地方。

override func viewDidLoad() {
    self.addTapGesture(tapNumber: 1, target: self.view, action: #selector(handlePan))
}

func addTapGesture(tapNumber : Int, target: Any , action : Selector) {
    let tap = UITapGestureRecognizer(target: target, action: action)
    tap.numberOfTapsRequired = tapNumber
    (target as AnyObject).addGestureRecognizer(tap)
}

@objc func handlePan() {
    print("call tap gesture event")
}