Swiftを勉強している初心者です。
UITableViewにRealmSwiftから取得したレコードを表示し、不要なレコードをスワイプで削除する際にしていますが、UITableViewDelegateのviewForHeaderInSectionで表示しているUITableViewCellまで一緒に消えてしまいます。
Headerのcellは削除されず、section内のレコードのみ削除されるようにしたいです。
コードの添削、アドバイスなどをしていただけたら幸いです。
どうぞよろしくお願い致します。
該当のソースコード
ViewController
1 2import UIKit 3import RealmSwift 4import SlideMenuControllerSwift 5 6class ViewController: UIViewController{ 7//抜粋 8 9 @IBOutlet weak var tableView: UITableView! 10 var wordEntities:Results<Words>? = nil 11 var category:Category? = nil 12 13 let realm = try! Realm() 14 15 override func viewDidLoad() { 16 super.viewDidLoad() 17 tableView.delegate = self 18 tableView.dataSource = self 19 setNavigationBarItem() 20 setNavigationBarTitle(title: "Words") 21 tableView.rowHeight = UITableView.automaticDimension 22 tableView.register(UINib(nibName: "WordTableViewCell", bundle: nil), forCellReuseIdentifier: "WordItemCell") 23 } 24 25 override func viewWillAppear(_ animated: Bool) { 26 super.viewWillAppear(animated) 27 if category != nil { 28 wordEntities = realm.objects(Words.self).filter("categoryId == %@", category!.categoryId).sorted(byKeyPath: "updateDate", ascending: false) 29 }else{ 30 wordEntities = realm.objects(Words.self).sorted(byKeyPath: "updateDate", ascending: false) 31 } 32 33 if wordEntities != nil{ 34 tableView.reloadData() 35 tableView.reloadSections(NSIndexSet(index: tableView.sectionIndexMinimumDisplayRowCount) as IndexSet, with: .none) 36 } 37 } 38 39//TableView Delegate 40extension ViewController: UITableViewDelegate{ 41 //********************** 42 //ここでdelete時にHeaderのcellも一緒に消えてしまう 43 //********************** 44 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { 45 if editingStyle == .delete{ 46 try! realm.write { 47 if let wordEntities = wordEntities{ 48 realm.delete(wordEntities[indexPath.row]) 49 } 50 } 51 } 52 tableView.reloadData() 53 } 54 55 //display tableview header 56 func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 57 let headerview = UIView() 58 headerview.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 100) 59 headerview.backgroundColor = UIColor.white 60 61 let searchbar = UISearchBar() 62 searchbar.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 50) 63 searchbar.placeholder = "検索" 64 headerview.addSubview(searchbar) 65 66 //********************** 67 //TableViewCellをHeaderに表示 68 //********************** 69 let cell = tableView.dequeueReusableCell(withIdentifier:"WordItemCell") as! WordTableViewCell 70 cell.frame = CGRect(x:0, y:55, width:self.view.frame.size.width, height:44) 71 cell.delegate = self 72 cell.dropdown.dataSource = arrayCategoryList() 73 headerview.addSubview(cell) 74 75 return headerview 76 } 77 78 //tableview header height size 79 func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat{ 80 return 100 81 } 82} 83 84//TableView DataSource 85extension ViewController: UITableViewDataSource{ 86 //display cell count 87 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 88 if let wordEntities = wordEntities{ 89 return wordEntities.count 90 } 91 return 0 92 } 93 94 //display cell details 95 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 96 let cell = tableView.dequeueReusableCell(withIdentifier:"WordItemCell") as! WordTableViewCell 97 cell.delegate = self 98 cell.dropdown.dataSource = arrayCategoryList() 99 100 if let wordEntities = wordEntities{ 101 cell.textfield.text = wordEntities[indexPath.row].word 102 cell.wordId = wordEntities[indexPath.row].wordId 103 cell.beforeWord = wordEntities[indexPath.row].word 104 105 if wordEntities[indexPath.row].categoryName != nil{ 106 cell.categorybutton.setTitle(wordEntities[indexPath.row].categoryName, for: .normal) 107 cell.categoryName = wordEntities[indexPath.row].categoryName 108 cell.beforecategoryName = wordEntities[indexPath.row].categoryName 109 } 110 } 111 return cell 112 } 113} 114 115
試したこと
delete後にreloadData()だけでなくreloadSections()も加えてみたら、削除1回目はCellも残ったままになりましたが、2回目以降削除すると結局消えてしまいます。
ViewController
1 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { 2 if editingStyle == .delete{ 3 try! realm.write { 4 if let wordEntities = wordEntities{ 5 realm.delete(wordEntities[indexPath.row]) 6 } 7 } 8 } 9 //ここを追加 10 tableView.reloadSections(NSIndexSet(index: tableView.sectionIndexMinimumDisplayRowCount) as IndexSet, with: .none) 11.none) 12 tableView.reloadData() 13 }
補足情報(FW/ツールのバージョンなど)
Swift 4.2
Xcode 10.0
RealmSwift 3.11.1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/07 00:21
2018/11/07 00:38
2018/11/07 00:43
2018/11/07 14:31
2018/11/07 16:11
2018/11/08 14:14
2018/11/09 00:23
2018/11/09 03:27
2018/11/09 03:55 編集
2018/11/09 04:43
2018/11/10 06:44