いつもお世話になります。
UITabelView
/SectionHeaderView
の挿入/削除をアニメーション付きで表示しようとしているのですが、SectionHeaderView
(青い部分)の挿入だけ思ったようにアニメーションがかかりません。
どこを見直せばいいでしょうか、また方向性が根本から間違っているのでしょうか?
動作は、ロングプレスしたセルの上にSectionHeader
を挿入し、SectionHeader
にある☓ボタンで該当SectionHeader
を削除しています。
↓Section:2
(青い部分)がパっと表示される、なんとかフェードインとかスライドインみたくならないでしょうか?
swift
1import UIKit 2 3class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 4 5 //MARK: -- properties -- 6 var arrayOfArray = [["AAAA", "BBBB", "CCCC", "DDDD", "EEEE", "FFFF", "GGGG", "HHHH"], 7 ["IIII", "JJJJ", "KKKK", "LLLL", "MMMM", "OOOO", "PPPP", "QQQQ"], 8 ["RRRR", "SSSS", "TTTT", "UUUU", "VVVV", "XXXX", "YYYY", "ZZZZ"]] 9 10 //MARK: -- Outlets -- 11 @IBOutlet weak var testTableView: UITableView! 12 13 // セクションヘッダーを追加する処理 14 @IBAction func addSectionHeaderView(_ sender: UILongPressGestureRecognizer) { 15 if(sender.state == UIGestureRecognizer.State.began) { 16 } else if (sender.state == UIGestureRecognizer.State.ended) { 17 18 let point = sender.location(in: testTableView) 19 guard let indexPath = testTableView.indexPathForRow(at: point) else { return } 20 21 print("CellLongPressed/addSectionHeaderView") 22 23 if !(indexPath.row == 0) { 24 // 配列操作 25 let targetArray = arrayOfArray[indexPath.section] 26 let firsrOfTargetArray = targetArray.prefix(indexPath.row) 27 let secondOfTargetArray = targetArray.dropFirst(indexPath.row) 28 arrayOfArray.remove(at: indexPath.section) 29 arrayOfArray.insert(firsrOfTargetArray.map{$0}, at: indexPath.section) 30 arrayOfArray.insert(secondOfTargetArray.map{$0}, at: indexPath.section + 1) 31 32 // 削除するインデックスパスの配列を作る 33 let deleteIndexes: [IndexPath] = 34 (indexPath.row ..< targetArray.count).map {[indexPath.section, $0]} 35 36 //TODO: -- ここがうまくいかない/アニメーションの組み合わせ? -- 37 // tableView操作 38 testTableView.beginUpdates() 39 40 //testTableView.deleteSections(IndexSet(integer: indexPath.section), with: .none) 41 //testTableView.insertSections(IndexSet(integer: indexPath.section), with: .none) 42 43 testTableView.deleteRows(at: deleteIndexes, with: .fade) 44 testTableView.insertSections(IndexSet(integer: indexPath.section + 1), with: .none) 45 testTableView.endUpdates() 46 // リロードしないとHeaderViewが表示されなくなる/tableView(_: viewForHeaderInSection) -> UIView?が呼ばれない 47 testTableView.reloadData() 48 49 /* tableViewをリロードしない場合の呼び出し回数/LongPress"FFFF" 50 CellLongPressed/addSectionHeaderView 51 numberOfSection/ 4 52 numberOfSection/ 4 53 generateSection 54 */ 55 56 /* tableViewをリロードした場合の呼び出し回数/LongPress"FFFF" 57 CellLongPressed/addSectionHeaderView 58 numberOfSection/ 4 59 numberOfSection/ 4 60 generateSection 61 numberOfSection/ 4 62 generateSection 63 generateSection 64 generateSection 65 */ 66 } 67 } 68 } 69 70 // セクションヘッダーを取り除く処理 71 @IBAction func removeSectionBtnTapped(_ sender: UIButton) { 72 guard let targetSection = sender.superview?.superview?.tag else { return } 73 let preTargetSection = targetSection - 1 74 75 if !(targetSection == 0) { 76 77 print("removeSectionBtnTapped") 78 79 // 配列操作 80 let targetSectionArray = arrayOfArray[targetSection] 81 arrayOfArray.remove(at: targetSection) 82 let preTargetSectionArray = arrayOfArray[preTargetSection] 83 arrayOfArray[preTargetSection] = preTargetSectionArray + targetSectionArray 84 85 // 挿入するインデックスパスの配列を作る 86 let insertIndexes: [IndexPath] = 87 (preTargetSectionArray.count ..< arrayOfArray[preTargetSection].count).map {[preTargetSection, $0]} 88 89 // tableView操作 90 testTableView.beginUpdates() 91 testTableView.deleteSections(IndexSet(integer: targetSection), with: .none) 92 testTableView.insertRows(at: insertIndexes, with: .fade) 93 testTableView.endUpdates() 94 95 // リロードしないとHeaderViewが表示されなくなる/tableView(_: viewForHeaderInSection) -> UIView?が呼ばれない 96 testTableView.reloadData() 97 98 /* tableViewをリロードした場合の呼び出し回数/Tap"FFFF" 99 removeSectionBtnTapped 100 numberOfSection/ 3 101 numberOfSection/ 3 102 numberOfSection/ 3 103 generateSection 104 generateSection 105 */ 106 107 /* tableViewをリロードしない場合の呼び出し回数/Tap"FFFF" 108 removeSectionBtnTapped 109 numberOfSection/ 3 110 numberOfSection/ 3 111 */ 112 } 113 } 114 115 //MARK: - UITableViewDataSource - 116 117 //MARK: -- Section -- 118 119 // numberOfSection 120 func numberOfSections(in tableView: UITableView) -> Int { 121 122 print("numberOfSection/",arrayOfArray.count) 123 124 return arrayOfArray.count 125 } 126 127 // generateSection 128 func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 129 130 print("generateSection") 131 132 let sectionHeaderView = testTableView.dequeueReusableCell(withIdentifier: "Section") as! SectionHeaderCell 133 134 sectionHeaderView.tag = section 135 sectionHeaderView.removeSectionBtn.isEnabled = section == 0 ? false: true 136 137 let labelStr = "Section:" + String(section + 1) 138 sectionHeaderView.setValueToSection(LabelStr: labelStr) 139 140 return sectionHeaderView 141 } 142 143 // heightOfSection 144 func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 145 return 44 146 } 147 148 //MARK: -- Cell -- 149 150 // numOfCell 151 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 152 return arrayOfArray[section].count 153 } 154 155 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 156 let cell = testTableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomCell 157 let cellLabelStr = arrayOfArray[indexPath.section][indexPath.row] 158 159 cell.setValueToCell(cellLabelStr: cellLabelStr) 160 161 return cell 162 } 163 164 // heightOfCell 165 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 166 return 44 167 } 168 169 //MARK: - UITableViewDelegate - 170 171 // selectCell 172 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 173 testTableView.selectRow(at: indexPath, animated: true, scrollPosition: .none) 174 } 175 176 override func viewDidLoad() { 177 super.viewDidLoad() 178 // Do any additional setup after loading the view, typically from a nib. 179 } 180} 181
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。