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

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

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

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

Swift

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

Q&A

0回答

709閲覧

switchなどを使って、TableViewのテキストラベル色を変更したい。

kazuki_user

総合スコア147

iOS

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

Swift

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

0グッド

0クリップ

投稿2020/09/12 22:52

編集2020/09/20 05:01

## やりたいこと

switchなどで、TableViewのdetailTextLabel.textColorを変更したいです。

## 試したこと

  • 変数cellDetailColorを使ったが、detailTextLabelの色は変わらず。
  • 変数に対して、
  • cell.backgroundColor = .redなどを試しに記述してみると、"Swift"以外のCellも赤色になったりして、うまくいかない。

swift

1var cellDetailColor = tableView.cellForRow(at: indexPath)?.detailTextLabel?.textColor 2 3 switch cell.detailTextLabel?.text { 4 case "Swift": 5 cellDetailColor = .red 6 case "JavaScript": 7 cellDetailColor = .yellow 8 case "PHP": 9 cellDetailColor = #colorLiteral(red: 0.1764705926, green: 0.01176470611, blue: 0.5607843399, alpha: 1) 10 case "HTML": 11 cellDetailColor = .green 12 case "Python": 13 cellDetailColor = .blue 14 case "Go": 15 cellDetailColor = #colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1) 16 default: 17 cellDetailColor = .black 18 }

## エラー

「cellForRowAtIndexPath:を呼び出そうとしましたが、これは許可されていません。」
とエラーが出ます。

Attempted to call -cellForRowAtIndexPath: on the table view while it was in the process of updating its visible cells, which is not allowed. Make a symbolic breakpoint at UITableViewAlertForCellForRowAtIndexPathAccessDuringUpdate to catch this in the debugger and see what caused this to occur. Perhaps you are trying to ask the table view for a cell from inside a table view callback about a specific row? Table view: <UITableView: 0x7f9af4032c00; frame = (0 0; 375 812); clipsToBounds = YES; autoresize = W+H; tintColor = UIExtendedSRGBColorSpace 0.716093 0.18764 0.201993 0.854051; gestureRecognizers = <NSArray: 0x60000313e7c0>; layer = <CALayer: 0x600003f56f20>; contentOffset: {0, 216}; contentSize: {375, 1422}; adjustedContentInset: {88, 0, 34, 0}; dataSource: <CodeCheck_Test_Yumemi.SearchRootVC: 0x7f9af2405300>>

## 警告

変数cellDetailColorに対して警告が出たが、変数を使わずとも上記のエラーで、
TableVIewのテキスト色は変更出来ない。

swift

1var cellDetailColor = tableView.cellForRow(at: indexPath)?.detailTextLabel?.textColor 2// Variable 'cellDetailColor' was written to, but never read 3// 変数 'c​​ellDetailColor'は書き込まれましたが、読み取られませんでした

質問は以上です。
お時間あるときに、ご返信頂けましたら幸いです????

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問