2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

カスタムCellに乗せたButtonのタップアクション

Last updated at Posted at 2021-04-24

はじめに

躓きやすいポイントだと思うので、記事に残しておきます。
いくつか方法があると思いますが、Delegateを使うパターンでいきます。

準備

CustomCellの作成

  1. xibを作成して、TableViewCellの上にButtonを設置
  2. Identifierを"Cell"に設定
    スクリーンショット 2021-04-24 10.12.49.png

クラスの作成

class CustomCell: UITableViewCell {
    
    override class func awakeFromNib() {
        super.awakeFromNib()
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        
    }
    
}

セルのカスタムクラスを設定

スクリーンショット 2021-04-24 10.16.55.png

ButtonをAction接続

class CustomCell: UITableViewCell {
    
    override class func awakeFromNib() {
        super.awakeFromNib()
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
    }
    
    @IBAction func onButtonClick(_ sender: Any) {
        
    }
    
}

Delegateを作成

//追加
protocol CustomCellDelegate {
    func customCellDelegateDidTapButton(cell: UITableViewCell)
}

class CustomCell: UITableViewCell {
    
    var delegate: CustomCellDelegate? //追加
    
    override class func awakeFromNib() {
        super.awakeFromNib()
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
    }
    
    @IBAction func onButtonClick(_ sender: Any) {
        delegate?.customCellDelegateDidTapButton(cell: self) //追加
    }
}

ViewController側からの呼び出し

class ViewController: UIViewController {
    
    @IBOutlet weak var tableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.register(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "Cell")
        tableView.dataSource = self
    }
}

extension ViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomCell
        cell.delegate = self
        return cell
    }
}

extension ViewController: CustomCellDelegate {
    func customCellDelegateDidTapButton(cell: UITableViewCell) {
        //タップされたときの処理
        
    }
}

以上です。

2
4
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
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?