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

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

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

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

Q&A

解決済

1回答

2610閲覧

tableViewのcellを削除すると、他のcellの表示がおかしくなってしまう

funkyfrea

総合スコア86

Swift

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

0グッド

0クリップ

投稿2016/01/04 06:36

お世話になっております。
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つ下がずれてしまいます。
また、スクロールして画面外に出してから元に戻すと、セルの表示は正しく戻ります。
上記のことから、セルの再利用で問題が起きているのではないかと思うのですが、解決策が分かりません。
何か、アドバイスをいただけると幸いです。よろしくお願いします。

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

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

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

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

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

guest

回答1

0

ベストアンサー

おそらく、削除ボタン(セル右端の赤いDelete表示)を表示するために通常と異なるレイアウトに調整されたことのあるセルを再利用した時に、問題が発生するのだろうと推測します。

部品レイアウトの調整にAutoLayoutを使用しているのか、AutoResizeを使用しているのか、コードで座標指定して調整しているのかわからないので、これ以上コメントできませんが、表示がずれているのは、timeLineTableViewCellというカスタムセル内のPhotoという部品(おそらくUIImageView)だと思います。
その部品のレイアウトをどのように指定して調整しているか再確認してみることをお勧めします。

投稿2016/01/05 03:03

編集2016/01/05 05:11
TakeOne

総合スコア6299

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問