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

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

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

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

Swift

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

Q&A

解決済

1回答

2370閲覧

tableViewCellの中に配置したUIButtonをラジオボタンのようにしたい

ohoh5454

総合スコア92

Xcode

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

Swift

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

0グッド

1クリップ

投稿2020/02/14 18:36

tableViewCellのなかにUIButtonを配置しています。
そのボタンをラジオボタンのような機能にしたいです。
1、押されたらボタンの色が変化する(実装済み)
2、一つしか選択できない。(すでに他のボタンが押されていたら押されていた方のボタンの色が元に戻る)
この2番ができなくて詰まっております。
押されたボタンの色が変化するまでのコードは下記の通りです。

swift

1import UIKit 2 3 4class SettingViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate{ 5 6 7 8 @IBOutlet weak var tableView: UITableView! 9 @IBOutlet weak var plusButton: UIButton! 10 @IBOutlet weak var saveCheck: UILabel! 11 @IBOutlet weak var doneButton: UIButton! 12 @IBOutlet weak var rouletteTitle: UITextField! 13 @IBOutlet weak var checkBox: UIButton! 14 15 var cell = TableViewCell() 16 17 var cellCount = 0 18 var checkCount = 0 19 20 let colorArray: [UIColor] = 21 [UIColor(red: 255/255, green: 255/255, blue: 0/255, alpha: 1.0),UIColor(red: 255/255, green: 190/255, blue: 0/255, alpha: 1.0),UIColor(red: 255/255, green: 35/255, blue: 0/255, alpha: 1.0),UIColor(red: 255/255, green: 0/255, blue: 255/255, alpha: 1.0), UIColor(red: 200/255, green: 70/255, blue: 255/255, alpha: 1.0),UIColor(red: 30/255, green: 120/255, blue: 255/255, alpha: 1.0),UIColor(red: 80/255, green: 220/255, blue: 255/255, alpha: 1.0),UIColor(red: 0/255, green: 255/255, blue: 0/255, alpha: 1.0),UIColor(red: 40/255, green: 40/255, blue: 190/255, alpha: 1.0),UIColor(red: 200/255, green: 200/255, blue: 200/255, alpha: 1.0)] 22 23 override func viewDidLoad() { 24 super.viewDidLoad() 25 26 tableView.delegate = self 27 tableView.dataSource = self 28 rouletteTitle.delegate = self 29 30 tableView.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: "cellSample") 31 tableView.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height - (plusButton.frame.size.height + saveCheck.frame.size.height + doneButton.frame.size.height)) 32 tableView.separatorStyle = .singleLine 33 tableView.allowsSelection = false 34 35 36 37 } 38 39 40 41 override func viewWillAppear(_ animated: Bool) { 42 super.viewWillAppear(animated) 43 44 navigationController?.isNavigationBarHidden = false 45 46 } 47 48 49 //セルの数 50 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 51 52 return cellCount 53 54 } 55 56 //セクションの数 57 func numberOfSections(in tableView: UITableView) -> Int { 58 return 1 59 } 60 61 62 //セルの構築 63 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 64 cell = tableView.dequeueReusableCell(withIdentifier: "cellSample") as! TableViewCell 65 //セル上のボタン(色) 66 67 cell.colorButton.tag = indexPath.row 68 cell.colorButton.addTarget(self, action: #selector(pushButton(_:)), for: .touchUpInside) 69 if indexPath.row % 10 == 0{ 70 cell.colorButton.backgroundColor = colorArray[0] 71 }else if indexPath.row % 10 == 1{ 72 cell.colorButton.backgroundColor = colorArray[1] 73 }else if indexPath.row % 10 == 2{ 74 cell.colorButton.backgroundColor = colorArray[2] 75 }else if indexPath.row % 10 == 3{ 76 cell.colorButton.backgroundColor = colorArray[3] 77 }else if indexPath.row % 10 == 4{ 78 cell.colorButton.backgroundColor = colorArray[4] 79 }else if indexPath.row % 10 == 5{ 80 cell.colorButton.backgroundColor = colorArray[5] 81 }else if indexPath.row % 10 == 6{ 82 cell.colorButton.backgroundColor = colorArray[6] 83 }else if indexPath.row % 10 == 7{ 84 cell.colorButton.backgroundColor = colorArray[7] 85 }else if indexPath.row % 10 == 8{ 86 cell.colorButton.backgroundColor = colorArray[8] 87 }else if indexPath.row % 10 == 9{ 88 cell.colorButton.backgroundColor = colorArray[9] 89 } 90 91 92 93 //セル上のテキストフィールド(タイトル) 94 cell.itemTitle.placeholder = "項目タイトル" 95 cell.itemTitle.textAlignment = .center 96 97 98 //セル上のテキストフィールド(比率) 99 cell.raitoInit.text = "1" 100 cell.raitoInit.textAlignment = .center 101 cell.raitoInit.keyboardType = UIKeyboardType.numberPad 102 cell.raitoLabel.text = "比率" 103 104 105 return cell 106 107 } 108 109 //セルの高さ 110 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 111 112 return view.frame.size.height/10 113 114 } 115 116 //colorButtonを押したとき 117 @objc private func pushButton(_ sender: UIButton) { 118 119 let row = sender.tag 120 121 //押したボタンの色を切り替える 122 if row % 10 == 0{ 123 sender.backgroundColor = colorArray[0].withAlphaComponent(0.6) 124 }else if row % 10 == 1{ 125 sender.backgroundColor = colorArray[1].withAlphaComponent(0.6) 126 }else if row % 10 == 2{ 127 sender.backgroundColor = colorArray[2].withAlphaComponent(0.6) 128 }else if row % 10 == 3{ 129 sender.backgroundColor = colorArray[3].withAlphaComponent(0.6) 130 }else if row % 10 == 4{ 131 sender.backgroundColor = colorArray[4].withAlphaComponent(0.6) 132 }else if row % 10 == 5{ 133 sender.backgroundColor = colorArray[5].withAlphaComponent(0.6) 134 }else if row % 10 == 6{ 135 sender.backgroundColor = colorArray[6].withAlphaComponent(0.6) 136 }else if row % 10 == 7{ 137 sender.backgroundColor = colorArray[7].withAlphaComponent(0.6) 138 }else if row % 10 == 8{ 139 sender.backgroundColor = colorArray[8].withAlphaComponent(0.6) 140 }else if row % 10 == 9{ 141 sender.backgroundColor = colorArray[9].withAlphaComponent(0.6) 142 } 143 144 145 } 146 147 //すでに色が薄くなっているものを戻す 148 func backColor(){ 149 150 151 152 } 153 154 155 //項目を追加ボタン 156 @IBAction func cellPlus(_ sender: Any) { 157 158 cellCount += 1 159 160 tableView.reloadData() 161 162 }

参考サイト
このサイトを見他のですがやはり他のボタンが押されたときに元々選択されていたボタンを元に戻すやり方がいまいち理解できません。このサイトではfor文が使われているのですがこのfor文も理解できてるようなできていないような感じです。
何かアドバイスくださると嬉しいです。
よろしくお願い致します。

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

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

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

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

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

hoshi-takanori

2020/02/14 22:38

参考サイトでは TableView は使われてませんが、TableView の場合はやり方が変わります。 というか、TableView の使い方は理解されてますでしょうか?
ohoh5454

2020/02/15 08:31

確かにそうですね! tableViewの使い方は理解しきれてないと思います。。
guest

回答1

0

ベストアンサー

参考サイトでは、ラジオボタンとして使われてるボタンたちは普通に配置されているので、常に存在しますが、TableView のセルは画面に表示されてない部分は存在しません。(というか、スクロール時に使いまわされます。)
なので、データソース(SettingViewController)の中でセルに表示するために必要な情報を持って、必要に応じて tableView(_:, cellForRowAt:) の中で設定してやる必要がありますが、今回必要な情報はどのボタンが押されてるかだけなので、まずその変数を checkCount の下あたりに用意します。

swift

1 var cellCount = 0 2 var checkCount = 0 3 4 // 押されたボタンの番号、押されてない時は -1 5 var pushedRow = -1

次にセルの構築ですが、押されたボタンがスクロールなどで再表示される場合があるので、押された場合とそうでない場合と両方に対応するように書く必要があります。
ちなみに、そこにあった if 〜 else if 〜 の羅列は実は 1 行で書けます。

swift

1 //セルの構築 2 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 3 cell = tableView.dequeueReusableCell(withIdentifier: "cellSample") as! TableViewCell 4 //セル上のボタン(色) 5 6 cell.colorButton.tag = indexPath.row 7 cell.colorButton.addTarget(self, action: #selector(pushButton(_:)), for: .touchUpInside) 8 // ここにあった if 〜 else if 〜 を次のコードで置き換える 9 let color = colorArray[indexPath.row % 10]; 10 if indexPath.row == pushedRow { 11 cell.colorButton.backgroundColor = color.withAlphaComponent(0.6) 12 } else { 13 cell.colorButton.backgroundColor = color 14 } 15 16 // 略 17 }

最後にボタンを押した時の処理ですが、押されたボタンの番号を変数にセットしたら、あとはテーブル全体を再描画するだけです。

swift

1 //colorButtonを押したとき 2 @objc private func pushButton(_ sender: UIButton) { 3 4 pushedRow = sender.tag 5 6 tableView.reloadData() 7 8 }

試してませんが、たぶんこれでいけるはず。

投稿2020/02/15 09:16

hoshi-takanori

総合スコア7895

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

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

ohoh5454

2020/02/15 10:18

すごく丁寧にありがとうございます! 早速試してみます!
ohoh5454

2020/02/16 06:35

完璧にいけました! ありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問