UITapGestureRecognizer不工作 - Swift 2

问题描述 投票:0回答: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类:

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

视图控制器:

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()
    }
}
ios swift uitapgesturerecognizer
2个回答
2
投票

你需要指定你想要的手势,在这里你必须添加一个UITapGestureRecognizer

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

0
投票

试试这个。对我来说工作正常。你只需通过目标添加点击手势。

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")
}
© www.soinside.com 2019 - 2024. All rights reserved.