reloadData時に打ち消し線を入れたままにしたい。
tableView.reloadData前後に、tableViewのプロパティを使い、tableView再描画時に任意のセルを選択状態にする事は出来たのですが、打ち消し線が入らない状態になってしまいます。
どうすれば打ち消し線が入った状態で画面を再描画する事が出来ますでしょうか?
該当のソースコード
下記コード全文です。
swift.ViewController
import UIKit class ViewController: UIViewController { var vegetable = ["人参","玉葱","南瓜"] var fruits = ["キウイ","オレンジ","メロン"] let sectionTitle: NSArray = ["野菜", "フルーツ"] @IBOutlet weak var tableView: UITableView! @IBAction func toSecondViewButton(_ sender: Any) { let secondVC = storyboard?.instantiateViewController(identifier: "secondView") as! SecondViewController navigationController?.pushViewController(secondVC,animated: true) } override func viewDidLoad() { super.viewDidLoad() tableView.delegate = self tableView.dataSource = self tableView.tableFooterView = UIView(frame: .zero) tableView.isEditing = true tableView.allowsMultipleSelectionDuringEditing = true } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) let indexPaths = tableView.indexPathsForSelectedRows self.tableView.reloadData() indexPaths?.forEach { tableView.selectRow(at: $0, animated: false, scrollPosition: .none) } } } extension ViewController: UITableViewDelegate, UITableViewDataSource { //並び替えの許可 func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool { return true } //セルの右側に並び替えマークが付き、指定したindexPathの並び替えが出来る func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { let itemToMove = vegetable.remove(at: sourceIndexPath.row) vegetable.insert(itemToMove, at: destinationIndexPath.row) } // セクション数を指定 func numberOfSections(in tableView: UITableView) -> Int { return sectionTitle.count } // セクションタイトルを指定 func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let label : UILabel = UILabel() label.textColor = UIColor.systemGreen if(section == 0){ label.text = (sectionTitle[section] as! String) } else if (section == 1){ label.text = (sectionTitle[section] as! String) } return label } // セル数を指定 // これはマスト! func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if section == 0 { return vegetable.count } else if section == 1 { return fruits.count } else { return 0 } } // 実際にCellを作る func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = UITableViewCell(style: .default, reuseIdentifier: "cell") if indexPath.section == 0 { cell.textLabel?.text = String(describing: vegetable[indexPath.row]) cell.detailTextLabel?.text = "行番号 : \(indexPath.row)" cell.imageView?.image = UIImage(named: "blue") cell.textLabel?.textColor = UIColor.red cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 30) return cell } else if indexPath.section == 1 { cell.textLabel?.text = String(describing: fruits[indexPath.row]) cell.textLabel?.textColor = UIColor.blue cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 40) } return cell } //Cellがタップされた時の処理はここ func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { print("\(indexPath.row)がtapされたよ") if let cell = tableView.cellForRow(at: indexPath) { let attributeString = NSMutableAttributedString(string: vegetable[indexPath.row]) attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSMakeRange(0, attributeString.length)) cell.textLabel?.attributedText = attributeString } } func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { if let cell = tableView.cellForRow(at: indexPath) { let attributeString = NSMutableAttributedString(string: vegetable[indexPath.row]) attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 0, range: NSMakeRange(0, attributeString.length)) cell.textLabel?.attributedText = attributeString } } }
swift.SecondViewController
import UIKit class SecondViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() } }
試したこと
swift
let indexPaths = tableView.indexPathsForSelectedRows self.tableView.reloadData() indexPaths?.forEach { tableView.selectRow(at: $0, animated: false, scrollPosition: .none)
でindexPathを保存、reloadData時に再選択しています。
まだ回答がついていません
会員登録して回答してみよう