Search code examples
iosswiftuitapgesturerecognizer

UITapGestureRecognizer not working - Swift 2


I am a beginner in iOS and work with swift 2. i'm based on this video : https://www.youtube.com/watch?v=itbok1NZvss (Which is based on SWIFT 3) create a expandable UITable . but UITableViewCell not Expand when i clicked. In this means, UITapGestureRecognizer does not work.

my code :

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

Solution

  • You need to specify what kind of gesture you want, here you have to add a UITapGestureRecognizer:

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