【问题标题】:Tap Gesture on tableViewCell Swift在 tableViewCell Swift 上点击手势
【发布时间】:2017-04-12 15:50:08
【问题描述】:

我想在表格视图单元格中的图像被录制时完成一个动作。我通过情节提要设置了一个点击手势,但每次触摸单元格时它都会读取点击。

当只有表格视图单元格中的图像被点击时,如何完成点击手势?

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

【问题讨论】:

    标签: ios swift uitableview swift3


    【解决方案1】:

    你应该这样使用。创建一个自定义单元格,并且必须为图像视图使用 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")
    } }
    

    【讨论】:

    • 我知道有更好的方法来做到这一点。你是最好的@Halil İbrahim YILMAZ
    【解决方案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)
    

    【讨论】:

    • 不,我的班级设置方式对我不起作用,还有其他想法吗?
    • 啊,我看到您正在使用 viewWithTag 检索网点,但这很好。您仍然可以用按钮替换图像视图,并以编程方式将按钮连接到您的IBAction
    • 我有一个 firebase 数据库正在加载我的图像,我不想更改我的图像的加载方式,我只需要在我的 tableviewcell 中的 ImageView 被点击时读取。
    • 您无需更改下载图像的方式。检查我更新的答案。
    • 你能举个例子让我更好地理解。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多