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

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

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

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

Q&A

解決済

1回答

1258閲覧

tableViewをリロードせずにCellのインデントをアニメーションしながらtableView.isEditingの適用と同時に変更することはできますか?

退会済みユーザー

退会済みユーザー

総合スコア0

Swift

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

0グッド

0クリップ

投稿2020/03/19 17:23

編集2020/03/19 17:26

以前の質問の続きです

tableView.isEditingを適用する際に選択不可のセルと選択可能のセルを同一のインデントで揃えたいのですが、一緒にアニメーションさせることができません。
tableView.isEditingを変更しても各セルのインデント設定は別途更新処理をかけないと変わってくれないようです。
更新処理をかけるとアニメーションがきれいにおこなれなくて困っています。更新処理をかけずにcellのインデントをアニメーション付きで変更する方法はないでしょうか?

↓インデントをアニメーション付きで同時に設定したい
イメージ説明

↓各セルにreloadをかけているのでアニメーションが途中までで、チカチカする…。
イメージ説明

swift

1import UIKit 2 3class ViewController: UIViewController { 4 5 let array = [String](repeating: "A", count: 5) 6 7 @IBOutlet weak var containerView: UIView! 8 @IBOutlet weak var upperView: UIView! 9 @IBOutlet weak var tTableView: UITableView! 10 11 @IBOutlet weak var upperViewBottom: NSLayoutConstraint! 12 13 override func viewDidLoad() { 14 super.viewDidLoad() 15 16 //tTableView.isEditing = true 17 //tTableView.allowsMultipleSelectionDuringEditing = true 18 } 19 20 @IBAction func editBtnDidTap(_ sender: Any) { 21 22 // 全体リロードはアニメーションを放棄することになるのでかけたくない 23 //self.tTableView.reloadData() 24 25 guard let indexPaths = tTableView.indexPathsForVisibleRows else { return } 26 27 UIView.animate(withDuration: 0.4, animations: { 28 29 self.tTableView.isEditing.toggle() 30 self.tTableView.allowsMultipleSelectionDuringEditing = self.tTableView.isEditing 31 32 self.tTableView.reloadRows(at: indexPaths, with: .automatic) 33 34 35 }, completion: { _ in 36 37 //self.tTableView.reloadData() 38 39 }) 40 41 } 42 43} 44 45extension ViewController: UITableViewDataSource { 46 47 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 48 return array.count 49 } 50 51 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 52 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 53 54 cell.textLabel?.text = array[indexPath.row] 55 56 cell.selectionStyle = indexPath.row.isEvenNumber ? .default : .none 57 58 if tableView.isEditing { 59 60 cell.indentationLevel = indexPath.row.isEvenNumber ? 0 : 4 61 cell.indentationWidth = indexPath.row.isEvenNumber ? 0 : 10 62 63 } else { 64 65 cell.indentationLevel = 0 66 cell.indentationWidth = 0 67 } 68 69 print() 70 71 return cell 72 } 73 74 func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 75 76 return indexPath.row.isEvenNumber 77 78 //return true 79 80 } 81 82} 83 84extension ViewController: UITableViewDelegate { 85 86 // willSelectRow 87 func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { 88 return indexPath.row.isEvenNumber ? indexPath : nil 89 } 90 91 // indicateEditButton 92 func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle { 93 return UITableViewCell.EditingStyle.init(rawValue: 3)! 94 } 95 96 // spaceOfEditIco 97 func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPaht: IndexPath) -> Bool { 98 return true 99 } 100 101 func scrollViewDidScroll(_ scrollView: UIScrollView) { 102 103 if scrollView.contentOffset.y < 0 { 104 105 let v: CGFloat = min(abs(scrollView.contentOffset.y), upperView.frame.height) 106 scrollView.contentInset.top = v 107 upperViewBottom.constant = -v 108 109 } else { 110 scrollView.contentInset.top = 0 111 upperViewBottom.constant = 0 112 } 113 } 114} 115 116extension Int { 117 118 var isEvenNumber: Bool { 119 return self % 2 == 0 120 } 121}

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

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

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

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

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

guest

回答1

0

ベストアンサー

とりあえずこれでできました。カスタムセルにはもうひと手間必要。

イメージ説明

swift

1import UIKit 2 3class ViewController: UIViewController { 4 5 let array = [String](repeating: "A", count: 20) 6 7 @IBOutlet weak var containerView: UIView! 8 @IBOutlet weak var upperView: UIView! 9 @IBOutlet weak var tTableView: UITableView! 10 11 @IBOutlet weak var upperViewBottom: NSLayoutConstraint! 12 13 override func viewDidLoad() { 14 super.viewDidLoad() 15 16 //tTableView.isEditing = true 17 //tTableView.allowsMultipleSelectionDuringEditing = true 18 } 19 20 @IBAction func editBtnDidTap(_ sender: Any) { 21 22 guard let indexPaths = tTableView.indexPathsForVisibleRows else { return } 23 24 UIView.animate(withDuration: 0.4, animations: { 25 26 self.tTableView.isEditing.toggle() 27 self.tTableView.allowsMultipleSelectionDuringEditing = self.tTableView.isEditing 28 29 indexPaths.forEach { 30 31 let cell = self.tTableView.cellForRow(at: $0) 32 33 if self.tTableView.isEditing { 34 cell?.indentationLevel = 4 35 cell?.indentationWidth = 10 36 } else { 37 cell?.indentationLevel = 0 38 cell?.indentationWidth = 0 39 } 40 } 41 42 self.view.layoutIfNeeded() 43 44 }, completion: { _ in 45 46 //self.tTableView.reloadData() 47 48 }) 49 50 } 51 52} 53 54extension ViewController: UITableViewDataSource { 55 56 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 57 return array.count 58 } 59 60 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 61 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 62 63 cell.textLabel?.text = array[indexPath.row] 64 65 cell.selectionStyle = indexPath.row.isEvenNumber ? .default : .none 66 67 if tableView.isEditing { 68 69 cell.indentationLevel = indexPath.row.isEvenNumber ? 0 : 4 70 cell.indentationWidth = indexPath.row.isEvenNumber ? 0 : 10 71 72 } else { 73 74 cell.indentationLevel = 0 75 cell.indentationWidth = 0 76 } 77 78 print() 79 80 return cell 81 } 82 83 func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 84 85 return indexPath.row.isEvenNumber 86 87 //return true 88 89 } 90 91} 92 93extension ViewController: UITableViewDelegate { 94 95 // willSelectRow 96 func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { 97 return indexPath.row.isEvenNumber ? indexPath : nil 98 } 99 100 // indicateEditButton 101 func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle { 102 return UITableViewCell.EditingStyle.init(rawValue: 3)! 103 } 104 105 // spaceOfEditIco 106 func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPaht: IndexPath) -> Bool { 107 return true 108 } 109 110 func scrollViewDidScroll(_ scrollView: UIScrollView) { 111 112 if scrollView.contentOffset.y < 0 { 113 114 let v: CGFloat = min(abs(scrollView.contentOffset.y), upperView.frame.height) 115 scrollView.contentInset.top = v 116 upperViewBottom.constant = -v 117 118 } else { 119 scrollView.contentInset.top = 0 120 upperViewBottom.constant = 0 121 } 122 } 123} 124 125extension Int { 126 127 var isEvenNumber: Bool { 128 return self % 2 == 0 129 } 130} 131

投稿2020/03/19 19:18

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問