スポンサーリンク

【Swift】TableViewCellに配置したUIButtonでindexPathを取得する方法。

Swift
スポンサーリンク

今回の記事はSwiftを使ってTableViewCellに配置したUIButtonでindexPathを取得する方法をご紹介する内容になっています。アプリにて使用する機会があったので備忘録程度ですが、困っている方は是非参考にしてみてください。

スポンサーリンク

TableViewCell用のCustomTableViewCellを用意

まずはTableViewCell用のカスタムクラスを作成します。

ファイルの追加から「CocoaTouchClass」を追加します。

import UIKit

class CustomTableViewCell : UITableViewCell {
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
    }
}

CustomTableViewCellにUIButtonを接続

続いてUIButtonをAutlet接続します。

Storyboardから通常のように線を引っ張って接続するだけでいいです。CustomTableViewCell.swiftに接続してください。

import UIKit

class CustomTableViewCell : UITableViewCell {

    @IBOutlet weak var infoButton: UIButton!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
    }
}

CustomTableViewCellコード

CustomeTavleViewCell.swiftを修正します。

import UIKit

class CustomTableViewCell : UITableViewCell {

    @IBOutlet weak var sampleButton: UIButton!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        sampleButton.addTarget(self,action: #selector(printSenderTag),
                                   for: .touchUpInside)
        // Configure the view for the selected state
    }
    
    // Infobutton
    @objc func printSenderTag(sender:UIButton){
        print(sender.tag)
    }
}

上記のコードの解説はTableViewを使用しているクラス内のコードをみてからの方が分かりやすいので先にそちらを説明します。

TableViewを使用しているクラス内のコード

// (省略)

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // Cell
        let cell = tableView.dequeueReusableCell(withIdentifier: "セルに設定した名前", for: indexPath as IndexPath) as! CustomTableViewCell

        // CustomTableViewCellの接続しているUIButtonのタグにindexPathをセットしている
        cell.infoButton.tag = indexPath.row

        return cell
    }

// (省略)

コメントでも記載していますがCellを返すTableViewのDelegateの中で、「tag」を使用するとAutlet接続しているUIButtonのクリックイベントの「sender」に値を渡せます。

これを踏まえて、CustomTableViewCellの中身を見るとtagから情報を取得し、printで出力していることがわかると思います。

では、今回の記事は以上です。他にも多数のSwift関連の記事を記載しているので是非サイト内興味があれば見て行ってください。

本記事を読んでいただき感謝です。サイトを訪れていただいた方はプログラミング勉強中かと思いますのでプログラミング勉強のコツを合わせてご紹介。

スポンサーリンク
スポンサーリンク
スポンサーリンク

ブログに関しては500円程度かかりますが、それ以外は無料です。知識の吸収と並行してアウトプットは非常に効率が良いです。テックアカデミーに関しては講座レベルが高いにも関わらず、無料体験や人気口座も大幅値下げがあるので、重点的に学びたいものを無料体験してみてください。

転職時にも、エンジニアからテックアカデミー・Paizaは認知度が高いので、未経験入社採用を行う際履歴書で目に留まります。特にPaizaのスキルレベルA・SなどはIT業界でも評価されます。

テックアカデミー・Paizaの無料登録ができる期間中にぜひご利用してみてください。私も活用経験ありです。

Swift
スポンサーリンク
スポンサーリンク
ともぶろぐ

コメント

タイトルとURLをコピーしました