【问题标题】:How to order table view cell from newest to oldest instead of from oldest to newest如何从最新到最旧而不是从最旧到最新排序表格视图单元格
【发布时间】:2017-06-23 02:47:24
【问题描述】:

我正在使用 Swift 3 中的 Xcode 制作一个 Notes 应用程序。我有一种添加注释的方法,一种保存、删除和标题注释的方法。当我做笔记时,它们会从最旧到最新列出,如下所示:

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

这是我的代码:

//
//  ListNotesTableViewController.swift
//  NotesApp
//
//  Created by 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")
            }
        }
    }
}

问题位于底部附近(我认为,但我不确定)。我希望我能更具体地说明问题出在哪里,但是我可以自己解决这个问题。这是我使用这种语言的第三天,我很困惑。

无论如何,如何将笔记显示的顺序从最新/最后修改的顺序更改为最旧的/第一次修改?

编辑:这是 CoreDataHelper 的代码:

//
//  CoreDataHelper.swift
//  NotesApp
//
//  Created by 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 []
    }
}

【问题讨论】:

  • 您需要根据需要对notes 数组进行排序。
  • 最好在您的 CoreDataHelper.retrieveNotes 方法中对它们进行排序。那看起来像什么?
  • 这是什么意思? (对不起,我完全缺乏知识......:/)
  • 我会在上面发布我的 CoreDataHelper 文件...在那里。完成。
  • @Octavius Ca 你展示了Note 实体或Note 类的核心数据结构。

标签: ios swift uitableview swift3


【解决方案1】:

其他人提到的问题不是桌子。您必须按顺序提供数据,并且由于您是从 coredata 获取数据,因此相当容易

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 []
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-26
    • 2016-08-26
    • 2021-12-19
    • 2014-10-18
    • 2019-04-10
    • 2021-08-20
    • 2016-10-23
    • 1970-01-01
    相关资源
    最近更新 更多