点击tableViewCell Swift上的手势

时间:2017-04-12 00:19:46

标签: ios swift uitableview swift3

我想在表视图单元格中的图像被录音时完成一个动作。我在故事板中设置了一个轻击手势,但每次触摸单元格时都会读取水龙头。

如果仅在我的表格视图单元格中的图像被录音时,如何完成点按手势?

enter image description here

  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return generalRoomDataArr.count // your number of cell here
    }




    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


        let cell = tableView.dequeueReusableCell(withIdentifier: "cell")

        //Transform Data From ^ to load at the bottom
        tableView.transform = CGAffineTransform (scaleX: 1,y: -1);
        cell?.contentView.transform = CGAffineTransform (scaleX: 1,y: -1);
        cell?.accessoryView?.transform = CGAffineTransform (scaleX: 1,y: -1);

        //Set username label to display username
        let usernameLabel = cell?.viewWithTag(1) as! UILabel
        usernameLabel.text = generalRoomDataArr[indexPath.row].username


        //Set message label to display message
        let messageLabel = cell?.viewWithTag(2) as! UILabel
        messageLabel.text = generalRoomDataArr[indexPath.row].message
        messageLabel.numberOfLines = 0

        //initialize UI Profile Image
        let imageView = cell?.viewWithTag(3) as! UIImageView

        //Make Porfile Image Cirlce
        imageView.layer.cornerRadius = imageView.frame.size.width/2
        imageView.clipsToBounds = true

        //Set timeStampLabel to current time AGO
        let timeStampLabel = cell?.viewWithTag(4) as! UILabel
        timeStampLabel.text = generalRoomDataArr[indexPath.row].timeStamp
        timeStampLabel.numberOfLines = 0


        //Loading and change of Usesrs profile image on chat cell
        let userProfileChatImage = generalRoomDataArr[indexPath.row].photoURL

        //Load profile image(on cell) with URL & Alamofire Library



            let downloadURL = NSURL(string: userProfileChatImage!)
            imageView.af_setImage(withURL: downloadURL as! URL)






        // your cell coding
        return cell!
    }






    @IBAction func userPhotoTaped(_ sender: Any) {

        print("tapped")
    }








}//END CLASS

我也试过这个

let tapped:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector(("TappedOnImage:")))
        tapped.numberOfTapsRequired = 1
        cell?.imageView?.addGestureRecognizer(tapped)

func TappedOnImage(sender: UITapGestureRecognizer){
        print("Elltappy")
    }

2 个答案:

答案 0 :(得分:3)

你应该这样使用。创建一个自定义单元格,并且必须使用isUserInteractionEnabled进行图像查看,您可以处理图像点击或表格视图选择操作。

class TestCell: UITableViewCell {

@IBOutlet weak var imageV: UIImageView!

override func awakeFromNib() {
    super.awakeFromNib()
    // You must to use interaction enabled
    imageV.isUserInteractionEnabled = true
    imageV.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(imageTapped(_:))))
}

func imageTapped(_ sender: UITapGestureRecognizer) {
    print("image tapped")
} }

答案 1 :(得分:2)

您是否考虑使用UIButton而不是图片视图?您可以删除默认文本并将其替换为情节提要中的配置文件图像,也可以通过编程方式button.setImage(image, for: .normal)替换。这样,您就可以将自定义单元格类中的按钮IBAction连接到按钮的touchUpInside事件。

<强> 更新

//initialize UI Profile Image
let profileImageButton = cell?.viewWithTag(3) as! UIButton

//Make Porfile Image Button Cirlce
profileImageButton.layer.cornerRadius = profileImageButton.frame.size.width/2
profileImageButton.clipsToBounds = true

profileImageButton.addTarget(self, selector: #selector(userPhotoTaped), action: .touchUpInside)

// ...

profileImageButton.imageView?.af_setImage(withURL: downloadURL as! URL)