[英]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.