【发布时间】:2019-01-06 10:49:10
【问题描述】:
我正在尝试在 tableView 中创建名称列表,并将其从上到下排序,我使用 coreData 保存所有信息,但是当我重新启动我的应用程序时,我的 tableView 被从下到上排序而不是从上到上底部。
我尝试了许多其他方法,但对此感到很困惑。
我的 coreData 实体“Person”,里面有“name”
import UIKit
import CoreData
var newList = [Person]()
var currentList = [Person]()
var myIndex = 0
class TableViewVC_SmartAccounting: UIViewController,UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UITextFieldDelegate {
@IBOutlet weak var txtField: UITextField!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var outAdd: UIButton!
@IBOutlet weak var searchBar: UISearchBar!
override func viewDidLoad() {
super.viewDidLoad()
fetchData()
newList = currentList
searchBar.backgroundImage = UIImage()
txtField.returnKeyType = UIReturnKeyType.done
txtField.delegate = self
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
txtField.resignFirstResponder()
print("RETURN")
return true
}
func fetchData(){
let fetchRequist: NSFetchRequest<Person> = Person.fetchRequest()
do {
let listFetched = try PersistenceService.context.fetch(fetchRequist)
currentList = listFetched
self.tableView.reloadData()
} catch let err as NSError {
print("Field to fetch an item", err)
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return currentList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCellVC_SmartAccounting
cell.lblName.text = currentList[indexPath.row].name
return cell
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let person = currentList.remove(at: indexPath.row)
PersistenceService.context.delete(person)
PersistenceService.saveContext()
self.tableView.deleteRows(at: [indexPath], with: .fade)
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
myIndex = indexPath.row
performSegue(withIdentifier: "segue", sender: self)
tableView.deselectRow(at: indexPath, animated: true)
}
func addPerson(){
if txtField.text != ""{
let person = Person(context: PersistenceService.context)
person.name = txtField.text
PersistenceService.saveContext()
// currentList.append(person)
currentList.insert(person, at: myIndex)
view.endEditing(true)
newList = currentList
let indexPath = IndexPath(row: myIndex, section: 0)
tableView.insertRows(at: [indexPath], with: .left)
// tableView.insertRows(at: [IndexPath(row: list.count - 1, section: 0)], with: .automatic)
}
txtField.text = ""
self.view.endEditing(true)
}
func setUpSearchBar() {
searchBar.delegate = self
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
self.searchBar.endEditing(true)
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
guard !searchText.isEmpty else {currentList = newList
tableView.reloadData()
return }
currentList = newList.filter ({ (Person) -> Bool in
(Person.name?.lowercased().contains(searchText.lowercased()))!
})
tableView.reloadData()
}
func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
}
@IBAction func btnAdd(_ sender: Any) {
addPerson()
}
}
【问题讨论】:
-
在 fetch 请求中添加
NSSortDescriptor或者——更好的是——使用自动处理排序的NSFetchedResultsController -
感谢您的回答。它现在可以工作,但只有当我输入数字时,但当我输入名字时,当我重新启动我的应用程序时它仍然会下降,但数字仍然写顺序。
-
Core Data 总是无序地保存记录。如果您想要自定义订单,则必须添加索引属性并按索引对记录进行排序
-
如果你看到我上面的代码。你的意思是“myIndex”
-
基本上是的。但索引也必须是Core Data实体的一个属性(如
name)
标签: swift uitableview core-data swift3