gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:23:59 28 4
gpt4 key购买 nike

enter image description here我正在通过 swift 做一个项目。我定义了一个 Collection View 并自定义了它的单元格,使其看起来像向下的图像。现在,一旦我单击该单元格,我的函数将告诉我我单击了哪个单元格,但我想获得更详细的信息,例如我在单元格编号 2 中获取图片。为此,我试图定义一个点击手势,但它似乎不起作用?这是我所有的代码:

导入 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!











}

最佳答案

-> 向表格 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)
}

-> 检测点击

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;
}

-> 在单元格中找到位置,并通过给 subview View (例如 imageView、标签)添加标签,您也可以获得准确的 View 。

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

关于ios - 如何知道在 UICollectionView 中点击了自定义单元格的哪一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43543451/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com