简体   繁体   English

如何知道在UICollectionView中单击了自定义单元格的哪一部分?

[英]How to know which part of a customised cell have been clicked on in UICollectionView?

在此处输入图片说明 I am doing a project via swift . 我正在通过swift进行项目。 I've defined a collection view and customised its cell such that it would look like the image down . 我已经定义了一个集合视图并自定义了它的单元格,使其看起来像图像一样。 Now , once I click on the cell , my function will tell me that I have clicked on which cell but I want to get more detailed info for example I get the picture in cell number 2 . 现在,一旦我单击了单元格,函数就会告诉我已经单击了哪个单元格,但是我想获取更多详细信息,例如,我在2号单元格中得到了图片。 For that purpose I tried to define a tap gesture but it seems that it doesnt work ? 为此,我试图定义一个轻击手势,但似乎不起作用? here is all my code : 这是我的全部代码:

import UIKit import AVFoundation import AVKit 导入UIKit导入AVFoundation导入AVKit

class ViewUSRController: UIViewController , UICollectionViewDelegate , UICollectionViewDataSource  {

    @IBOutlet var collectionView: UICollectionView!
    var cell : MyCollectionViewCell?

    var names = ["Danial" , "dkfloza" , "Kosarifar" , "IOS" , "Learning", "sina" ]
    var likes = ["23" , "7" , "17" , "100K" , "1000K" , "100"]

    var dislikes = ["0","0","0","0","0","0"]





    var playerViewController = AVPlayerViewController()
    var playerView = AVPlayer()


 override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .black
        self.collectionView.delegate = self




        let tap = UITapGestureRecognizer(target: self.cell?.myLabel, action:#selector(handleTap))
        tap.numberOfTapsRequired =  1
        cell?.myLabel.addGestureRecognizer(tap)

    }

    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{

        return self.names.count
    }


    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
         cell = collectionView.dequeueReusableCell(withReuseIdentifier: "id", for: indexPath as IndexPath) as! MyCollectionViewCell

        // Use the outlet in our custom class to get a reference to the UILabel in the cell
        cell?.myLabel?.text = self.names[indexPath.row]
        cell?.myLabel?.textColor = .black
        cell?.likes_num?.text = likes[indexPath.row]
        cell?.dislike_num?.text = dislikes[indexPath.row]
        cell?.likes_num?.textColor = .black
        cell?.dislike_num?.textColor = .black


        return cell!

    }






//    
//    
//    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//        // handle tap events
//        print("You selected cell #\(indexPath.item)!")
//        
//        
//        
//        
//    }


    func handleTap(){
        print("hello")
    }







}






class MyCollectionViewCell : UICollectionViewCell{
    //appClub
    @IBOutlet var myLabel: UILabel!
    //imagePlace
    @IBOutlet var viewTesting: UIView!
    //likes
    @IBOutlet var likes_num: UILabel!
    //dislikes
    @IBOutlet var dislike_num: UILabel!











}

-> Add gesture to table view ->将手势添加到表格视图

override func viewDidLoad() {
        super.viewDidLoad()

        let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap))
        tap.delegate = self
        tap.numberOfTapsRequired = 1
        tap.numberOfTouchesRequired = 1
        tableView.addGestureRecognizer(tap)
    }

-> Detect tap ->检测水龙头

func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
    let tableView = gestureRecognizer.view as! UITableView
    let point: CGPoint = gestureRecognizer.location(in: tableView)
    if (tableView.indexPathForRow(at: point) != nil) {
        return true;
    }
    return false;
}

-> find the location in cell and by giving tag to the subview view(eg. imageView, label) you can get the exact view as well. ->找到单元格中的位置,并通过给子视图视图添加标签(例如imageView,label),您也可以获取确切的视图。

public func handleTap(tap: UITapGestureRecognizer)
{
    if (UIGestureRecognizerState.ended == tap.state) {
        let tableView = tap.view as! UITableView
        let point: CGPoint = tap.location(in: tableView)
        let indexPath = tableView.indexPathForRow(at: point)
        tableView.deselectRow(at: indexPath!, animated: false)
        let cell = tableView.cellForRow(at:indexPath!)
        let pointInCell = tap.location(in: cell)
        if ((cell?.imageView?.frame)!.contains(pointInCell)) {
            // user tapped image
        } else {
            // user tapped cell
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UIPopoverController-如何指向UICollectionView单元的自定义附件图标上的位置? - UIPopoverController - How to point to a position on UICollectionView cell's customised Accessory icon? 如何查看已滚动的UICollectionView单元格 - How to see which UICollectionView Cells have been scrolled through 如何知道在哪个表格单元格中触摸了哪个图片? - How to know which pic in which tableview cell has been touched? 如何区分UITableViewCell的哪个部分已被点击 - how can I distinguish which part of UITableViewCell has been clicked 如何在 UICollectionView 中自定义单元格 - How to have custom cell in UICollectionView 显示之前是否单击过表格单元格? - Show if tableview cell have been clicked before? UITableViewCell知道单击/选择哪个单元格 - UITableViewCell know which cell is clicked / selected Twitter是否知道已经下载了哪些消息以及如何下载? - Does Twitter know which message have already been downloaded and how? 在UICollectionView中删除单元格后,如何更新UICollectionViewCell中的按钮标签? - How to update the button tag which is part of UICollectionViewCell after a cell is deleted in UICollectionView? 如何识别哪个按钮点击UICollectionView iOS哪个部分? - How to identify the which button clicked on which section UICollectionView iOS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM