Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITapGestureRecognitor不工作-Swift 2_Ios_Swift_Uitapgesturerecognizer - Fatal编程技术网

Ios UITapGestureRecognitor不工作-Swift 2

Ios UITapGestureRecognitor不工作-Swift 2,ios,swift,uitapgesturerecognizer,Ios,Swift,Uitapgesturerecognizer,我是iOS的初学者,使用swift 2。 我基于以下视频: (基于SWIFT 3)创建可扩展的UITable。但单击时,UITableViewCell未展开。这意味着,UITapgestureRecognitor无法工作。 我的代码: struct Section { var genre: String! var movies: [String]! var expanded: Bool! init(genre: String , movies: [String] , expanded: Bo

我是iOS的初学者,使用swift 2。 我基于以下视频: (基于SWIFT 3)创建可扩展的
UITable
。但单击时,
UITableViewCell
未展开。这意味着,
UITapgestureRecognitor
无法工作。

我的代码:

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()
    }
}
视图控制器:

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()
    }
}
类ViewController:UIViewController、UITableViewDelegate、UITableViewDataSource、ExpandableHeadServiceWDelegate{
@IBVAR表格视图:UITableView!
可变截面=[
部分(类型:“1”,电影:[“dfgdgd”、“DGDG”、“jsgdhgdg”],扩展:真),
部分(类型:“2”,电影:[“dfgdgd”,“dgdgdg”],扩展:false),
部分(类型:“3”,电影:[“dfgdgd”,“dgdgdg”],扩展:真)
]
重写func viewDidLoad(){
super.viewDidLoad()
//加载视图后,通常从nib执行任何其他设置。
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
func numberOfSectionsInTableView(tableView:UITableView)->Int{
返回节数
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回节[section].movies.count
}
func tableView(tableView:UITableView,heightForHeaderInSection:Int)->CGFloat{
返回44
}
func tableView(tableView:UITableView,heightForRowAtIndexPath:nsindepath)->CGFloat{
if(节[indexath.section].expanded==true)
{
返回44
}
其他的
{
返回0
}
}
func tableView(tableView:UITableView,heightforfooterInstruction节:Int)->CGFloat{
返回2
}
func tableView(tableView:UITableView,viewForHeaderInSection:Int)->UIView{
let header=ExpandableHeaderView()
header.customeInit(节[section].genre,节:节,委托:self)
回流集管
}
func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
let cell=tableView.dequeueReusableCellWithIdentifier(“labelCell”)
cell!.textlab?.text=节[indexath.section]。电影[indexath.row]
返回单元!
}
func toggleSection(标题:ExpandableHeaderView,小节:Int){
节[section]。已展开=!节[section]。已展开
tableView.beginUpdate()
对于0中的i..
您需要指定您想要的手势类型,这里您必须添加一个
UITapGestureRecognitor

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

您需要指定所需的手势类型,这里您必须添加一个
UITapgestureRecognitor

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

试试这个。它对我很管用。 您只需在添加点击手势的位置传递目标

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

试试这个。它对我很管用。 您只需在添加点击手势的位置传递目标

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

不要点击手势,而是尝试按uibutton进行操作。您确认没有调用
print(“selectHeaderAction”)
?没有
selectHeaderAction
打印在控制台上?@MilanNosáľ正是。。在控制台中选择HeaderAction no printed(不打印)。请尝试使用uibutton(uibutton)按钮执行操作,而不是点击手势。您确认没有调用
print(“selectHeaderAction”)
?没有
selectHeaderAction
打印在控制台上?@MilanNosáľ正是。。选择控制台中打印的HeaderAction no。我在
ExpandableHeaderView
类中的
init
方法中指定这一行:
self.addgestureRecognitizer(UIgestureRecognitizer(目标:self,操作:Selector)(“selectHeaderAction:”)
Hum,当实例化它时,您将它实例化为一个
UIgestureRecognitizer
,不是作为
UITapgestureRecognitor
。。。在
init
方法中,您需要设置
uitagesturerecognizer
,并且
self.userInteractionEnabled=true
我在
ExpandableHeaderView
中的
init
方法中指定这一行:
self.addgestureerecognizer(uigestureegnizer(目标:self,操作:选择器(“selectHeaderAction”))
Hum,当您实例化它时,您将它实例化为一个
UIgestureRecognitor
,而不是一个
UICapgestureRecognitor
。。。在
init
方法中,您需要设置一个
UITapgestureRecognitizer
,以及
self.userInteractionEnabled=true