Ios 如何知道在UICollectionView中单击了自定义单元格的哪个部分?
我正在通过swift做一个项目。我定义了一个集合视图,并对其单元格进行了自定义,使其看起来像下图。现在,一旦我点击了单元格,我的函数会告诉我我已经点击了哪个单元格,但我想获得更详细的信息,例如,我在单元格2中得到了图片。为此,我试着定义一个轻拍手势,但它似乎不起作用?以下是我的全部代码: 导入UIKit 进口AVF基金会 进口AVKitIos 如何知道在UICollectionView中单击了自定义单元格的哪个部分?,ios,uicollectionview,uicollectionviewcell,uitapgesturerecognizer,Ios,Uicollectionview,Uicollectionviewcell,Uitapgesturerecognizer,我正在通过swift做一个项目。我定义了一个集合视图,并对其单元格进行了自定义,使其看起来像下图。现在,一旦我点击了单元格,我的函数会告诉我我已经点击了哪个单元格,但我想获得更详细的信息,例如,我在单元格2中得到了图片。为此,我试着定义一个轻拍手势,但它似乎不起作用?以下是我的全部代码: 导入UIKit 进口AVF基金会 进口AVKit class ViewUSRController: UIViewController , UICollectionViewDelegate , UICollect
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!
}
->将手势添加到表视图
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;
}
->在单元格中找到位置,通过给子视图(如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
}
}
}
->将手势添加到表视图
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;
}
->在单元格中找到位置,通过给子视图(如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
}
}
}
您可以将按钮放在标签上,并将其自动布局为中心+等宽+等高,然后创建基于代理或闭包的操作;这方面的最佳实践太多了。但在每种实践中,你都必须处理自己的
UITouch
对象。尝试同时使用触摸
事件和集合视图委托。识别正在触摸的物品(单元格)以及触摸在“收藏”视图中的位置(或您的单元格)。然后,您可以在触摸项目的坐标系中映射该触摸位置以获得视图。还有一件事,如果您尝试使用任何手势来完成您的工作,您可能会更改响应器(UIResponder
objects)层次结构。这会让事情变得更糟。您可以只在标签上放置按钮,并将其自动布局为中心+等宽+等高,然后创建基于代理或闭包的操作;这方面的最佳实践太多了。但在每种实践中,你都必须处理自己的UITouch
对象。尝试同时使用触摸
事件和集合视图委托。识别正在触摸的物品(单元格)以及触摸在“收藏”视图中的位置(或您的单元格)。然后,您可以在触摸项目的坐标系中映射该触摸位置以获得视图。还有一件事,如果您尝试使用任何手势来完成您的工作,您可能会更改响应器(UIResponder
objects)层次结构。这会让事情变得更糟。我正在尝试将您提供的代码从tableView转换为CollectionView。除了行if((cell?.imageView?.frame)!.contains(pointInCell))
似乎在cell?
之后,我无法获取单元格中的任何元素,例如标签或imageView或。。。。顺便说一句,对于点击检测,选择器应该是#选择器(self.handleTap(点击:)
,否则它将给出编译错误,因为使用不明确:)好的,那么现在您无法访问标签单元格?myLabel有什么问题?如果这些属性是在cell类中定义的公共或内部属性,则可以访问这些属性。我正在尝试将您提供的代码从tableView转换为CollectionView。除了行if((cell?.imageView?.frame)!.contains(pointInCell))
似乎在cell?
之后,我无法获取单元格中的任何元素,例如标签或imageView或。。。。顺便说一句,对于点击检测,选择器应该是#选择器(self.handleTap(点击:)
,否则它将给出编译错误,因为使用不明确:)好的,那么现在您无法访问标签单元格?myLabel有什么问题?如果这些属性是在单元类中定义的公共属性或内部属性,则可以访问这些属性。