UITapGestureRecognizer не работает при программной установке цели - PullRequest
0 голосов
/ 10 мая 2018

Я пытаюсь добавить прослушиватель событий крана на UIImageView внутри UITableViewCell, поэтому я добавил UITapGestureRecognizer к нему и использовал этот код

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        ...
        cell.editTap.addTarget(self, action: #selector(ProfileTVC.editTapped(sender:))) 
        //editTap is an UITapGestureRecognizer
        ...
    }
}

func editTapped(sender: UITapGestureRecognizer) {
    print("tapped")
    if sender.state == UIGestureRecognizerState.ended {
        let tapLocation = sender.location(in: self.tableView)
        if let tapIndexPath = self.tableView.indexPathForRow(at: tapLocation) {
            if fields[tapIndexPath.row].field == "languages_spoken" {

            } else if fields[tapIndexPath.row].field == "password" {

            } else {

            }
        }
    }
}

, но когда я нажимаю намой UIImageView editTapped не вызывается.Также UIImageView взаимодействие с пользователем включено

Ответы [ 2 ]

0 голосов
/ 10 мая 2018

Вы не можете добавить UITapGestureRecognizer с помощью "addTarget"

Измените метод addTarget с помощью этого метода в функции CellForRowAt;

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ProfileTVC.editTapped(sender:)))
cell.editTap.addGestureRecognizer(tapGesture) //editTap should be the ImageView inside cell.
0 голосов
/ 10 мая 2018

Вы добавляете цель, а не жест

  let tapPress = UITapGestureRecognizer(target: self, action: #selector(ProfileTVC. editTapped(_:)))
 cell.editTap.addGestureRecognizer(tapPress)

//

@objc func editTapped(_ gestureRecognizer:UIGestureRecognizer)
{ 


}
...