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

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

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

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

Q&A

解決済

1回答

578閲覧

TableView reloadData時に打ち消し線も入った状態で描画したい!!

masaboy

総合スコア61

Swift

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

0グッド

0クリップ

投稿2022/04/16 08:50

編集2022/04/16 20:23

reloadData時に打ち消し線を入れたままにしたい。

tableView.reloadData前後に、tableViewのプロパティを使い、tableView再描画時に任意のセルを選択状態にする事は出来たのですが、打ち消し線が入らない状態になってしまいます。
どうすれば打ち消し線が入った状態で画面を再描画する事が出来ますでしょうか?

該当のソースコード

下記コード全文です。

swift.ViewController

1import UIKit 2 3class ViewController: UIViewController { 4 5 var vegetable = ["人参","玉葱","南瓜"] 6 var fruits = ["キウイ","オレンジ","メロン"] 7 let sectionTitle: NSArray = ["野菜", "フルーツ"] 8 9 10 @IBOutlet weak var tableView: UITableView! 11 @IBAction func toSecondViewButton(_ sender: Any) { 12 let secondVC = storyboard?.instantiateViewController(identifier: "secondView") as! SecondViewController 13 navigationController?.pushViewController(secondVC,animated: true) 14 } 15 16 override func viewDidLoad() { 17 super.viewDidLoad() 18 19 tableView.delegate = self 20 tableView.dataSource = self 21 tableView.tableFooterView = UIView(frame: .zero) 22 tableView.isEditing = true 23 tableView.allowsMultipleSelectionDuringEditing = true 24 } 25 override func viewWillAppear(_ animated: Bool) { 26 super.viewWillAppear(animated) 27 let indexPaths = tableView.indexPathsForSelectedRows 28 29 self.tableView.reloadData() 30 31 indexPaths?.forEach { 32 tableView.selectRow(at: $0, animated: false, scrollPosition: .none) 33 } 34 } 35} 36extension ViewController: UITableViewDelegate, UITableViewDataSource { 37 //並び替えの許可 38 func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool { 39 return true 40 } 41 //セルの右側に並び替えマークが付き、指定したindexPathの並び替えが出来る 42 func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { 43 44 let itemToMove = vegetable.remove(at: sourceIndexPath.row) 45 vegetable.insert(itemToMove, at: destinationIndexPath.row) 46 } 47 // セクション数を指定 48 func numberOfSections(in tableView: UITableView) -> Int { 49 return sectionTitle.count 50 } 51 52 // セクションタイトルを指定 53 func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 54 let label : UILabel = UILabel() 55 label.textColor = UIColor.systemGreen 56 if(section == 0){ 57 label.text = (sectionTitle[section] as! String) 58 } else if (section == 1){ 59 label.text = (sectionTitle[section] as! String) 60 } 61 return label 62 } 63 64 // セル数を指定 65 // これはマスト! 66 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 67 if section == 0 { 68 return vegetable.count 69 } 70 else if section == 1 { 71 return fruits.count 72 } 73 else { 74 return 0 75 } 76 } 77 // 実際にCellを作る 78 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 79 let cell = UITableViewCell(style: .default, reuseIdentifier: "cell") 80 81 if indexPath.section == 0 { 82 cell.textLabel?.text = String(describing: vegetable[indexPath.row]) 83 cell.detailTextLabel?.text = "行番号 : \(indexPath.row)" 84 cell.imageView?.image = UIImage(named: "blue") 85 cell.textLabel?.textColor = UIColor.red 86 cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 30) 87 return cell 88 } 89 else if indexPath.section == 1 { 90 cell.textLabel?.text = String(describing: fruits[indexPath.row]) 91 cell.textLabel?.textColor = UIColor.blue 92 cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 40) 93 } 94 return cell 95 } 96 //Cellがタップされた時の処理はここ 97 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 98 print("\(indexPath.row)がtapされたよ") 99 if let cell = tableView.cellForRow(at: indexPath) { 100 let attributeString = NSMutableAttributedString(string: vegetable[indexPath.row]) 101 attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSMakeRange(0, attributeString.length)) 102 cell.textLabel?.attributedText = attributeString 103 104 } 105 } 106 func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { 107 if let cell = tableView.cellForRow(at: indexPath) { 108 let attributeString = NSMutableAttributedString(string: vegetable[indexPath.row]) 109 attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 0, range: NSMakeRange(0, attributeString.length)) 110 cell.textLabel?.attributedText = attributeString 111 } 112 } 113}

swift.SecondViewController

1import UIKit 2 3class SecondViewController: UIViewController { 4 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 8 } 9}

試したこと

swift

1 let indexPaths = tableView.indexPathsForSelectedRows 2 3 self.tableView.reloadData() 4 5 indexPaths?.forEach { 6 tableView.selectRow(at: $0, animated: false, scrollPosition: .none)

でindexPathを保存、reloadData時に再選択しています。
イメージ説明

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

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

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

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

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

guest

回答1

0

自己解決

var checkMark: Bool = false
を作り、
tableVIewメソッドcellForRowAt内に変数checkMarkがtrueの時に打ち消し線を表示するコードを書けば、意図した挙動が実現出来ました!!

投稿2022/04/18 19:06

masaboy

総合スコア61

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問