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

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

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

TableView(UITableView)とは、リスト形式で表示するコントロールで、ほとんどのアプリに使用されています。画面を「行」に分けて管理し、一般的には各行をタップした際に詳細画面に移動します。

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

解決済

2回答

767閲覧

Swift ダブルタップしたセルの色を変えたい。

Larry

総合スコア28

TableView

TableView(UITableView)とは、リスト形式で表示するコントロールで、ほとんどのアプリに使用されています。画面を「行」に分けて管理し、一般的には各行をタップした際に詳細画面に移動します。

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クリップ

投稿2019/11/04 17:03

前提・実現したいこと

ダブルタップしたセルをお気に入りマークとして色を変えたいです。

tableViewのあるセルをダブルタップすると、そのセルの内容をお気に入りとして登録させたいのですが、
その際、見てわかるようにそのセルも背景色を変えたいです。

イメージ説明

下記にイメージを示します。
(左の数字はindexPath.rowで, ここでは例として青色の背景としています)

0: apple
1: orange
2: peach
3: banana
↓(appleとpeachのセルをダブルタップ)

0: apple(青い背景)
1: orange
2: peach(青い背景)
3: banana

また、同じ単語を再度ダブルタップするとそのセルの色は元に戻るようにしたいです
0: apple(青い背景)
1: orange
2: peach(青い背景)
3: banana
↓ (appleとpeachのセルをダブルタップ)

0: apple
1: orange
2: peach
3: banana

新たに単語を登録した時もそのセルの色は単語に対応させ
0: apple(青い背景)
1: orange
2: peach(青い背景
3: banana
↓ (grapを追加)

0: grape
1: apple(青い背景)
2: orange
3: peach(青い背景)
4: banana

並び替えをした時も同じく
0: banana
1: peach(青い背景)
2: orange
3: grape
4: apple(青い背景)

エラーとか問題点とかではありませんが、
これらを実現するために方針を教えていただきたいです。

(ダブルタップした単語を別のお気に入り用の配列に格納していき、そのお気に入り配列と元の単語リストを比較して、どうにかしていく感じですか、、?この先どうすればいいかわからない。。。)

(セルの背景色の反映はダブルタップ時にtableView.reloadData()で更新??)

該当のソースコード

swift

1import UIKit 2 3class ViewController: UIViewController, UIGestureRecognizerDelegate{ 4 5 @IBOutlet weak var tableView: UITableView! 6 var words = [String]() 7 var star = [String]() 8 9 override func viewDidLoad() { 10 super.viewDidLoad() 11 tableView.rowHeight = UITableView.automaticDimension 12 self.tableView.tableFooterView = UIView() 13 navigationItem.leftBarButtonItem = editButtonItem 14 let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action:#selector(onAddTapped(_:))) 15 navigationItem.rightBarButtonItem = addButton 16 17 18 definesPresentationContext = true //very important 19 20 let doubleTapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.doubleTap(_:))) 21 doubleTapGesture.numberOfTapsRequired = 2 22 tableView.addGestureRecognizer(doubleTapGesture) 23 } 24 25 @objc func onAddTapped(_ sender: Any) { 26 let alert = UIAlertController(title: "単語を登録", message: "Enter word",preferredStyle: .alert) 27 alert.addTextField {(ChineseTH) in ChineseTH.placeholder = "words" } 28 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 29 let okAction = UIAlertAction(title: "Add", style: .default) { (_) in 30 31 guard let newWord = alert.textFields?.first?.text, newWord.count > 0 else { return } 32 if self.words.firstIndex(of: newWord) != nil { 33 self.alert0(title: "すでに登録されています", message: newWord) 34 } else { 35 self.insertCellRow(newWord) 36 } 37 } 38 print("追加後のwords = ", self.words) 39 alert.addAction(cancelAction) 40 alert.addAction(okAction) 41 42 self.present(alert, animated: true, completion: nil) 43 } 44 45 func alert0(title: String, message: String) { 46 47 let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.actionSheet) 48 49 // アラート表示 50 self.present(alert, animated: true, completion: { 51 // アラートを自動で閉じる 52 DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: { 53 alert.dismiss(animated: true, completion: nil) 54 }) 55 }) 56 } 57 //セルを追加 58 func insertCellRow(_ word: String){ 59 60 words.insert(word, at: 0) 61 let indexPath = IndexPath(row: 0, section: 0) 62 tableView.insertRows(at: [indexPath], with: .automatic) 63 } 64 65 @objc func doubleTap(_ gesture: UITapGestureRecognizer) { 66 67 let point: CGPoint = gesture.location(in: tableView) 68 let indexPath = tableView.indexPathForRow(at: point) 69 if let indexPath = indexPath { 70 if gesture.state == UIGestureRecognizer.State.ended { 71 72 print("ダブルタップしたセルは", indexPath.row) 73 74 } 75 } 76 } 77} 78 79extension ViewController: UITableViewDelegate, UITableViewDataSource { 80 81 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 82 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") 83 84 85 let subtitle = words[indexPath.row] 86 cell?.textLabel?.text = words[indexPath.row] 87 if let transform = subtitle.applyingTransform(.mandarinToLatin, reverse: false) { 88 cell?.detailTextLabel?.text = transform 89 } 90 91 return cell! 92 } 93 94 95 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 96 return words.count 97 } 98 99 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 100 tableView.deselectRow(at: indexPath, animated: true) 101 102 } 103 104 override func setEditing(_ editing: Bool, animated: Bool) { 105 super.setEditing(editing, animated: animated) 106 tableView.setEditing(editing, animated: animated) 107 } 108 109 func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { 110 let item = words[sourceIndexPath.row] 111 words.remove(at: sourceIndexPath.row) 112 words.insert(item, at: destinationIndexPath.row) 113 tableView.reloadData() 114 } 115 func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool { 116 return true 117 } 118 119 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { 120 words.remove(at: indexPath.row) 121 tableView.deleteRows(at: [indexPath], with: .fade) 122 print("削除セル = ",indexPath.row) 123 } 124 125}

抽象的な質問で済みませんが、ヒント、方針をご教授いただければと思いますm(__)m

補足情報(FW/ツールのバージョンなど)

Xcode 11.2

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

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

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

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

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

guest

回答2

0

ダブルタップすると、そのセルの内容をお気に入りとして登録させたい

本題とは逸れますが、ダブルタップだとかロングタップのように、説明を受けないと判らない機能を基本的な機能として本当に実装すべきかどうかをよく検討してください。

目に見えない機能は一般ユーザーにとっては裏技となんら変わりありません。
★ボタンを付けるなどといった機能の可視化を検討してください。

※検討した上でダブルタップ案になったのでしたら、スルーしてください。

これらを実現するために方針を教えていただきたいです。

やりたい事はまとまっていると思いますので、あとは実装するだけです。
どこが分からないか課題(例えば「ダブルタップを検出する方法」といった課題)を細分化して表にまとめ、一つずつ実験・検証し、課題をクリアしていってください。

どうしてもわからない事がでたら、このサイトで新たに質問を立てると良いでしょう。

投稿2019/11/05 03:01

takabosoft

総合スコア8356

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

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

Larry

2019/11/08 16:40

実際、ボタンで実装しようか迷っていました。。。 もう少し考えてみます! 回答ありがとうございました!
guest

0

ベストアンサー

方法論?が分からないということだったので、
いろいろな方法(挙げられたお気に入りを別の配列にする等)がありますが、

簡単に実装するにはisFavoriteなどのBool値を持つStructを新たに作成し、
単語を定義し直し、それを配列で持ち、
indexPathに応じて表示すればいいと思います。
(何を言われているかチンプンカンプンかもしれませんが。。。)

まず、tableViewへの単語の描出はどのようにやっているのでしょうか?
その方法によりすぐに先に進めるか、
その前にやらないといけないことがあるのかがはっきりすると思います。

上記のロングタップは
①変数を使ったtableviewへのデータの描出、
②新たなstruct作成、
③ただのタップによる値の変更
までできたら、いよいよ③をロングタップに置き換えて完成
になるのではないかと思います。

投稿2019/11/05 09:34

hameji

総合スコア1380

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

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

Larry

2019/11/08 16:41

bool値を持たせて行う方法で、うまくできました! 回答ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問