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

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

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

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

Swift

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

Q&A

解決済

1回答

2072閲覧

XIBを使ったTableViewCell上のボタンで画面遷移する方法

eotw

総合スコア48

iOS

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

Swift

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

0グッド

0クリップ

投稿2021/12/01 03:51

前提・実現したいこと

SwiftでTodoアプリを作成しています。
XIBを使ったTableViewCell上のボタン(StoryBoardでカスタムセルにボタンを配置)で画面遷移する機能を実装したいです。
いくつか方法はあると思いますが、今回はDelegateを使用しています。

発生している問題・エラーメッセージ

ボタンをタップした際に特にエラーは出ず、無反応状態となっています。

該当のソースコード

TableViewCell

Swift

1import UIKit 2 3protocol MainTableViewCellDelegate { 4 func takingButton() 5} 6 7class MainTableViewCell: UITableViewCell { 8 @IBOutlet weak var timeField: UILabel! 9 @IBOutlet weak var medicineField: UILabel! 10 11 var mainTableViewCellDelegate: MainTableViewCellDelegate? 12 13 override func awakeFromNib() { 14 super.awakeFromNib() 15 // Initialization code 16 } 17 18 override func setSelected(_ selected: Bool, animated: Bool) { 19 super.setSelected(selected, animated: animated) 20 21 // Configure the view for the selected state 22 } 23 24 @IBAction func addAction(_ sender: Any) { 25 mainTableViewCellDelegate?.takingButton() 26 } 27 28}

ViewController

Swift

1import UIKit 2import RealmSwift 3 4class RemindViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 5 let realm = try! Realm() 6 var itemList:Results<TodoItem>! 7 var token:NotificationToken! 8 @IBOutlet weak var tableView: UITableView! 9 10 override func viewDidLoad() { 11 super.viewDidLoad() 12 tableView.register(UINib(nibName: "MainTableViewCell", bundle: nil), forCellReuseIdentifier: "customCell") 13 14 itemList = realm.objects(TodoItem.self).sorted(byKeyPath: "date") 15 token = realm.observe{ notification, realm in 16 17 self.tableView.reloadData() 18 } 19 } 20 21 func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 22 return true 23 } 24 25 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 26 return 150 27 } 28 29 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { 30 if editingStyle == .delete { 31 infoHelper().deleteItem(item: itemList[indexPath.row], token: token) 32 tableView.deleteRows(at: [indexPath], with: .automatic) 33 } 34 } 35 36 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 37 return itemList.count 38 } 39 40 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 41 let cell = tableView.dequeueReusableCell(withIdentifier: "customCell") as! MainTableViewCell 42 let item = itemList[indexPath.row] 43 cell.medicineField?.text = item.name 44 cell.timeField?.text = infoHelper().dateToString(date: item.date) 45 cell.layer.cornerRadius = 10 46 47 48 return cell 49 } 50 51 @objc func buttonEvent(_ sender: UIButton) { 52 print("buttonTag", sender.tag) 53 } 54 55} 56 57extension RemindViewController: MainTableViewCellDelegate { 58 func takingButton() { 59 performSegue(withIdentifier: "toJudgementVC", sender: nil) 60 } 61}

試したこと

下記の記事を参考に試しましたが、現状で止まっており、解決できずという状況です。
https://boku-note.org/xib-tableviewcell/

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

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

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

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

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

guest

回答1

0

ベストアンサー

RemindViewController 側で delegate メソッドを実装していますが
移譲が自身にできていないのでコールされないんだと思います。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "customCell") as! MainTableViewCell let item = itemList[indexPath.row] cell.medicineField?.text = item.name cell.timeField?.text = infoHelper().dateToString(date: item.date) cell.layer.cornerRadius = 10 // 追加 // VCでコールできるように移譲先を自身に設定 cell.mainTableViewCellDelegate = self return cell }

投稿2021/12/01 05:42

TakuyaAso

総合スコア1361

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

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

eotw

2021/12/01 05:50

大変助かりました!ありがとうございます!
TakuyaAso

2021/12/01 05:51

よかったです! 私もよく忘れますw
eotw

2021/12/01 06:04

すっかり見落としていました。。! ありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問