お世話になっております。
UITableViewのcellの削除機能を以下のように実装したのですが、cellを削除すると5つ下のcellの左隅にスペースが空いて、おかしな表示になってします。
timeLineViewController.swift
swift
1func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 2 return true 3 } 4 5 func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 6 //削除の場合 7 if editingStyle == .Delete{ 8 let realm = try!Realm() 9 let key = tableViewSections[indexPath.section] as! NSString 10 let note:Note = self.tableViewCells![key]![0][indexPath.row] as! Note 11 12 self.tableViewCells![key]![0].removeObject(note) 13 14 //realmから削除 15 try!realm.write({ () -> Void in 16 realm.delete(note) 17 }) 18 19 20 tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) 21 22 23 } 24 25 } 26 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 27 let cell:timeLineTableViewCell = tableView.dequeueReusableCellWithIdentifier("noteCell")as! timeLineTableViewCell 28 29 let realm = try!Realm() 30 31 let key = self.tableViewSections[indexPath.section] as! NSString 32 let note:Note = self.tableViewCells![key]![0][indexPath.row] as! Note 33 34 if note.photos.isEmpty{ 35 cell.Photo.image = UIImage(named: "Stack of Photos-50 (1)") 36 }else{ 37 //写真のファイルネームを取得 38 let filename = note.photos[0].filename 39 40 //画像ファイルパス 41 let filepath = (path! as NSString).stringByAppendingPathComponent(filename) 42 //ファイルから写真を取り込む 43 let image = UIImage(contentsOfFile: filepath) 44 cell.Photo.image = image 45 } 46 //ノートのコメントの一行目を取得 47 48 49 let title = note.noteText.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet())[0] 50 cell.titleLabel.text = title 51 52 53 //ノートの一行目以降を取得。 54 //削除したタイトルの空白と改行が残っているので、トリミング。 55 56 57 let bodytext = note.noteText.stringByReplacingOccurrencesOfString(title, withString: "") 58 let afterText = bodytext.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) 59 60 cell.bodyTextLabel.text = afterText 61 62 63 //時間を表示する。曜日を配列に入れる 64 let weekDay:Array = ["","日曜日","月曜日","火曜日","水曜日","木曜日","金曜日","土曜日"] 65 let calendar:NSCalendar = NSCalendar(calendarIdentifier:NSCalendarIdentifierGregorian)! 66 let unit:NSCalendarUnit = [NSCalendarUnit.Year, NSCalendarUnit.Month,NSCalendarUnit.Day,NSCalendarUnit.Hour,NSCalendarUnit.Minute,NSCalendarUnit.Weekday] 67 let comps:NSDateComponents = calendar.components(unit, fromDate:note.createDate!) 68 69 70 print("時間:\(comps.minute)") 71 var mimute = comps.minute.description 72 if mimute.characters.count == 1{ 73 74 cell.timeLabel.text = "\(comps.hour)"+":"+"0"+mimute 75 76 }else{ 77 78 cell.timeLabel.text = "\(comps.hour)"+":"+"\(comps.minute)" 79 80 } 81 cell.dayLabel.text = "\(comps.day)" 82 print(comps.weekday) 83 cell.weekDayLabel.text = weekDay[comps.weekday] 84 85 return cell 86 87 } 88
表示がおかしくなるのは、決まって削除した5つ下のcellです。二つ削除すると、それぞれ5つ下がずれてしまいます。
また、スクロールして画面外に出してから元に戻すと、セルの表示は正しく戻ります。
上記のことから、セルの再利用で問題が起きているのではないかと思うのですが、解決策が分かりません。
何か、アドバイスをいただけると幸いです。よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。