ios - UITapGestureRecognizer 不工作 - Swift 2

标签 ios swift uitapgesturerecognizer

我是 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 类:

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()
    }
}

View Controller :

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:")))

关于ios - UITapGestureRecognizer 不工作 - Swift 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49024490/

相关文章:

iphone - 是否可以从 XCode 4.1 或 4.0.2 构建到 iOS 5 设备?

objective-c - Cocoa Touch 线程问题

ios - Swift 数字泛型?

swift - 响应请求的分页网址的最佳做法是什么

iphone - UIMenuController 未按预期工作,canPerformAction 未被解雇

ios - 在同一 View 上点击并滑动手势

ios - 使用 PayPal iOS SDK 支付多件商品

ios - iPhone - 在 iTunesConnect 上的应用程序有效期结束后,我可以再次使用相同的名称吗?

ios - 在 UINavigationController 推送/弹出转换期间允许用户交互

iOS:如何从 Swift 类触发 Obj-C 中的 UITapGestureRecognizer 和 UIPanGestureRecognizer?