LoginSignup
1
2

More than 5 years have passed since last update.

CustomCellTableViewCell を追加し、Cellにボタンを配置 Cell内のボタンをタプしたらTableViewに検知実装

Posted at

◆OptionButtonsDelegate作成


protocol OptionButtonsDelegate{
    func closeFriendsTapped(buttonname: String,at index:Int)
}

◆delegate変数をCustomCellTableViewCellに作成


class CustomCellTableViewCell: UITableViewCell {

    var delegate:OptionButtonsDelegate!

    @IBAction func btnButtonClick(_ sender: Any) {
        self.delegate?.closeFriendsTapped(buttonname:"report",at: (sender as! UIButton).tag)
    }

◆ViewControllerでOptionButtonsDelegate を実装

class ViewController: UIViewController,OptionButtonsDelegate{

関数実装
func closeFriendsTapped(buttonname: String,at index: Int) {
    print("cell内のButtonがタプされました")   
}

◆delegate変数にself設定

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell: CustomCellTableViewCell = tableView.dequeueReusableCell(withIdentifier: "mycell", for: indexPath) as! CustomCellTableViewCell
            cell.delegate = self
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2