gpt4 book ai didi

ios - 如何将 TableView 单元格从最新到最旧而不是从最旧到最新排序

转载 作者:行者123 更新时间:2023-11-30 12:21:44 25 4
gpt4 key购买 nike

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

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

最佳答案

正如其他人提到的问题不是表格。您必须按顺序提供数据,因为您是从 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 []
}

关于ios - 如何将 TableView 单元格从最新到最旧而不是从最旧到最新排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44712274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com