私はswiftでプロジェクトをやっています。私は、コレクションビューを定義し、そのセルをカスタマイズして、イメージのように見えるようにしました。今、私がセルをクリックすると、私の関数は私がどのセルをクリックしたかを教えてくれるが、もっと詳細な情報を得たいと思う。例えば、セル番号2の画像を得る。その目的のために、私はタップジェスチャーを定義しようとしましたが、それは動作しないようですね?ここにすべての私のコードです:カスタマイズされたセルのどの部分がUICollectionViewでクリックされたかを知るには?
輸入の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!
}
ラベルにボタンを配置し、それらを中央+等幅+等高線として自動レイアウトし、デリゲートまたはクロージャベースのアクションを作成することができます。 – SeanLintern88
これにはベストプラクティスが多すぎます。しかし、それぞれの練習では、あなた自身の 'UITouch'オブジェクトを扱わなければなりません。 'touches'イベントとコレクションビューデリゲートを一緒に使ってみてください。接触しているあなたのアイテム(セル)とコレクションビューのタッチの位置(またはあなたの上のセル)を特定します。その後、そのタッチ位置をタッチしたアイテムの座標系にマッピングして、ビューを取得することができます。 –
あなたの仕事を達成するためにジェスチャーを使用しようとすると、レスポンダ( 'UIResponder'オブジェクト)の階層を変更する可能性があります。これは事態を悪化させます。 –