iOS/UIKit
[Swift/UIKit]CollectionView/TableView Cell안에 버튼넣고 이벤트주는법
최지철
2023. 10. 24. 18:45
반응형
간단합니다.
@objc 메서드 만들고 버튼에서 addTaget 설정만 해주면 됩니다.
1. 컬렉션뷰 셀 부분에 addTaget주기
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CollectionViewCell.identifier, for: indexPath) as! CollectionViewCell
cell.goBtn.addTarget(self, action: #selector(self.goBtnClicked), for: .touchUpInside) //addTarget
cell.goBtn.tag = indexPath.row
return cell
}
2.@objc 메서드 만들기
@objc func goBtnClicked(_ sender: UIButton) {
print("바로가기 클릭")
}
반응형