본문 바로가기
iOS/UIKit

[Swift/UIKit]CollectionView/TableView Cell안에 버튼넣고 이벤트주는법

by 최지철 2023. 10. 24.
728x90
반응형

해당 셀에 버튼을 넣었슴다.

간단합니다. 

@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("바로가기 클릭")
    }

ㅉㅏ란~

728x90
반응형