質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

1029閲覧

iosのTableViewの使い方

sena14

総合スコア109

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2018/07/15 16:11

AndroidでいうListViewのようなものをiosで実装したく調べていたらTableViewにいきつきました。
行の要素にbuttonを配置しそのボタンを押すと何番目かの要素によって処理を書きたいです。

下記のように簡単にcellのテキストを変更する方法は分かったのですが自分で配置したbuttonにアクセスする方法が分かりません。
どのようにボタンにアクセスすればよいのでしょうか?よろしくお願いします。

import UIKit class MemoTableViewController: UITableViewController { var memos = ["blue", "red", "pink"] override func viewDidLoad() { super.viewDidLoad() // Uncomment the following line to preserve selection between presentations // self.clearsSelectionOnViewWillAppear = false // Uncomment the following line to display an Edit button in the navigation bar for this view controller. // self.navigationItem.rightBarButtonItem = self.editButtonItem() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // MARK: - Table view data source override func numberOfSections(in tableView: UITableView) -> Int { // #warning Incomplete implementation, return the number of sections return 1 } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // #warning Incomplete implementation, return the number of rows return self.memos.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "MemoTableViewCell", for: indexPath) // Configure the cell... cell.textLabel?.text = self.memos[indexPath.row] return cell } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

図がないので分かりにくいかもですが、デリゲートを使用すると以下のような感じになると思います。

Storyboardの Cellにボタンを配置
② カスタムセルクラス(MemoTableViewCell)を作成、MemoTableViewCellStoryboardのセルのカスタムクラスに設定
※配置したボタンのIBOutletIBActionを結ぶ
protocolを定義する。
④ セルにDelegateを定義して、ボタン押下時にデリゲートメソッドを呼び出す様にする
MemoTableViewControllerにデリゲートメソッドを記述
cell作成時にindexPath.rowdelegateを設定する

※ 便宜上以下のコードはファイル分けしてません。

swift

1 2import UIKit 3 4class MemoTableViewController: UITableViewController { 5 6 var memos = ["blue", "red", "pink"] 7 8 override func viewDidLoad() { 9 super.viewDidLoad() 10 11 } 12 13 // MARK: - Table view data source 14 override func numberOfSections(in tableView: UITableView) -> Int { 15 return 1 16 } 17 18 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 19 return self.memos.count 20 } 21 22 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 23 if let cell = tableView.dequeueReusableCell(withIdentifier: "MemoTableViewCell", for: indexPath) as? MemoTableViewCell { 24 cell.textLabel?.text = self.memos[indexPath.row] 25 cell.button.tag = indexPath.row 26 cell.delegate = self 27 return cell 28 } 29 30 return UITableViewCell() 31 } 32} 33 34extension MemoTableViewController: MemoTableViewCellDelegate { 35 func tappedButton(index: Int) { 36 print("Tap Button Index =", index) 37 } 38} 39 40 41protocol MemoTableViewCellDelegate: class { 42 func tappedButton(index: Int) 43} 44 45class MemoTableViewCell: UITableViewCell { 46 47 weak var delegate: MemoTableViewCellDelegate? 48 @IBOutlet weak var button: UIButton! 49 50 @IBAction func tappedButton(_ sender: UIButton) { 51 delegate?.tappedButton(index: button.tag) 52 } 53} 54

投稿2018/07/15 23:23

_Kentarou

総合スコア8490

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

_Kentarou

2018/07/16 02:08

セルにラベルが表示された状態のコードにボタンを追加してください。
sena14

2018/07/17 04:42

ありがとうございました。こちらもできました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問