2017-06-23 7 views
0

私は、Swift 3でXcodeを使用してNotesアプリケーションを作成しています。メモを追加する方法、保存する方法、削除する方法、 。ここで テーブルビューのセルを最も古いものから最も古いものから最も古いものから最も古いものに変更する方法

https://i.stack.imgur.com/1K59R.png

が私のコードです::私はノートを作るとき、彼らはそのように古いものから列挙されている問題が配置されている

// 
// ListNotesTableViewController.swift 
// NotesApp 
// 
// Created by Jozef Porubcin on 6/22/17. 
// Copyright © 2016. All rights reserved. 
// 

import UIKit 

class ListNotesTableViewController: UITableViewController { 
    var notes = [Note](){ 
     didSet { 
      tableView.reloadData() 
     } 
    } 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     notes = CoreDataHelper.retrieveNotes() 
    } 
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return notes.count 
    } 
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "listNotesTableViewCell", for: indexPath) as! ListNotesTableViewCell 
     let row = indexPath.row 
     let note = notes[row] 
     cell.noteTitleLabel.text = note.title 
     cell.noteModificationTimeLabel.text = note.modificationTime?.convertToString() 
     return cell 
    } 
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
     if editingStyle == .delete { 
      CoreDataHelper.delete(note: notes[indexPath.row]) 
      notes = CoreDataHelper.retrieveNotes() 
     } 
    } 
    @IBAction func unwindToListNotesViewController(_ segue: UIStoryboardSegue){ 
     self.notes = CoreDataHelper.retrieveNotes() 
    } 

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     if let identifier = segue.identifier { 
      if identifier == "displayNote" { 
       print("Table view cell tapped") 
       let indexPath = tableView.indexPathForSelectedRow! 
       let note = notes[indexPath.row] 
       let displayNoteViewController = segue.destination as! DisplayNoteViewController 
       displayNoteViewController.note = note 
      } else if identifier == "addNote" { 
       print("+ button tapped") 
      } 
     } 
    } 
} 

は(私は思うが、私はわかりません)。私は問題がどこにあるのかをより具体的にすることができればいいと思いますが、私はこの問題を自分で解決することができます。これは私の3日目のこの言語であり、私は非常に混乱しています。

とにかく、ノートが表示される順序の順序を最新/最終変更から最古/最初に変更する方法を教えてください。

編集:ここではCoreDataHelperのコードです:

// 
// CoreDataHelper.swift 
// NotesApp 
// 
// Created by Jozef Porubcin on 6/22/17. 
// Copyright © 2017. All rights reserved. 
// 

import Foundation 
import CoreData 
import UIKit 

class CoreDataHelper { 
    static let appDelegate = UIApplication.shared.delegate as! AppDelegate 
    static let persistentContainer = appDelegate.persistentContainer 
    static let managedContext = persistentContainer.viewContext 
    static func newNote() -> Note { 
     let note = NSEntityDescription.insertNewObject(forEntityName: "Note", into: managedContext) as! Note 
     return note 
    } 
    static func saveNote() { 
     do { 
      try managedContext.save() 
     } catch let error as NSError { 
      print("Could not save \(error)") 
     } 
    } 
    static func delete(note: Note) { 
     managedContext.delete(note) 
     saveNote() 
    } 
    static func retrieveNotes() -> [Note] { 
     let fetchRequest = NSFetchRequest<Note>(entityName: "Note") 
     do { 
      let results = try managedContext.fetch(fetchRequest) 
      return results 
     } catch let error as NSError { 
      print("Could not fetch \(error)") 
     } 
     return [] 
    } 
} 
+1

必要に応じて 'notes'配列をソートする必要があります。 – rmaddy

+0

あなたのCoreDataHelper.retrieveNotesメソッドでそれらを並べ替えることをお勧めします。それはどう? –

+0

それはどういう意味ですか? (私の完全な知識が不足して申し訳ありません:/) – Octavius

答えて

0

他の人が言及したような問題がテーブルではありません。あなたは順番にデータを提供しなければなりません。それでかなり簡単です。

static func retrieveNotes() -> [Note] { 
    let fetchRequest = NSFetchRequest<Note>(entityName: "Note") 
    fetchRequest.sortDescriptors = [NSSortDescriptor(key:"date", ascending: true)] //date would be whatever theKeyWhereYouStoreTheDateInYourDatabaseIs 
    do { 
     let results = try managedContext.fetch(fetchRequest) 
     return results 
    } catch let error as NSError { 
     print("Could not fetch \(error)") 
    } 
    return [] 
} 
関連する問題