카테고리 없음

Xcdoe) button으로 cell index 컨트롤 하기

나태한개발 2024. 7. 4. 20:59
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "MyCellIdentifier", for: indexPath) as? MyTableViewCell else {
            return UITableViewCell()
        }
        
        // 셀의 버튼에 태그 값으로 인덱스 설정
        cell.myButton.tag = indexPath.row
        cell.myButton.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
        
        return cell
    }
    
    @objc func buttonTapped(_ sender: UIButton) {
        let buttonTag = sender.tag
        
        // 버튼 태그를 사용하여 해당 셀 인덱스 참조
        print("Button tapped in row \(buttonTag)")
        
        // 여기서 필요한 작업 수행
    }