我刚开始并已经组合了另一个List应用程序,因为那里没有足够的信息...但是我遇到了一个问题,即在输入新条目后没有调用reloadData()为日记。如果我再次使用XCode重新编译应用程序,则新条目将出现在tableView上。
我已包含信号1 SIGTERM错误,该错误在重新编译应用程序时出现。
import Foundation
import UIKit
import CoreData
class TableViewController: UITableViewController {
@IBAction func addNewItem(_ sender: Any) {
return
}
@IBOutlet var entityTableView: UITableView!
var items: [Entity] = [] //Should be same name as new Entity in the dataModel
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
//Calls the getData which will fetch the new entry and then reload the view
//The register will linkt he UITableViewCell class with the tableView
//estimatedRowHeight and rowHeight define the label size and I intentionally want text cut off (journal entries might be quite long) so a preview works
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.estimatedRowHeight = 60
self.tableView.rowHeight = UITableView.automaticDimension
}
//Will reload the table every time that the view is shown to allow it to show the most recent entries
override func viewWillAppear(_ animated: Bool) {
getData()
print ("\(items)")
NSFetchRequest<NSManagedObject>(entityName: "entry")
}
//Func will get the data. Print to check that data has been pulled and then refreshes the
func getData() {
do {
items = try context.fetch(Entity.fetchRequest())
print (items)
DispatchQueue.main.async {
self.tableView.reloadData()
}
} catch {
print("Unable to fetch the data")
}
}
}
extension TableViewController {
//Will have the items presented as most recent on the top
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell
cell.textLabel?.text = items.reversed()[indexPath.row].newEntry
return cell
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let DeleteAction = UIContextualAction(style: .destructive, title: "Delete", handler: { (action, view, success) in
print("Delete")
})
DeleteAction.backgroundColor = .red
let item = self.items [indexPath.row]
self.context.delete(item)
(UIApplication.shared.delegate as! AppDelegate).saveContext()
self.items.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
return UISwipeActionsConfiguration(actions: [DeleteAction])
}
}
addNewEntry的viewController是:
import UIKit
import CoreData
class addNewEntryViewController: UIViewController, UITextViewDelegate {
@IBOutlet weak var journalEntryField: UITextView!
@IBAction func cancelEntry (_ sender: Any) {
dismiss(animated: true, completion: nil)
}
@IBOutlet weak var headerForNewEntry: UILabel!
@IBAction func saveNewEntry(_ sender: Any) {
guard let enteredText = journalEntryField?.text else {
return
}
if enteredText.isEmpty || journalEntryField?.text == "Type anything..."{
let alert = UIAlertController(title: "Please Type Something", message: "Your entry was left blank.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Okay", style: .default) { action in
})
self.present(alert, animated: true, completion: nil)
} else {
guard let entryText = journalEntryField?.text else {
return
}
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let newEntry = Entity(context: context)
newEntry.newEntry = entryText
(UIApplication.shared.delegate as! AppDelegate).saveContext()
dismiss(animated: true, completion: nil)
}
}
//Will have the delegate set to the current view
override func viewDidLoad() {
super.viewDidLoad()
journalEntryField.delegate = self
}
//Func will hide the keyboard after pressing the Return button
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if (text == "\n") {
textView.resignFirstResponder()
return false
}
return true
}
}
[<Journal__Core_Data_.Entity: 0x600003423e80> (entity: Entity; id: 0xcae729498383650b <x-coredata://71565814-E7F1-4D16-852F-7D8CBEA199F2/Entity/p1>; data: <fault>), <Journal__Core_Data_.Entity: 0x600003427de0> (entity: Entity; id: 0xcae72949838f650b <x-coredata://71565814-E7F1-4D16-852F-7D8CBEA199F2/Entity/p2>; data: <fault>)] [<Journal__Core_Data_.Entity: 0x600003423e80> (entity: Entity; id: 0xcae729498383650b <x-coredata://71565814-E7F1-4D16-852F-7D8CBEA199F2/Entity/p1>; data: <fault>), <Journal__Core_Data_.Entity: 0x600003427de0> (entity: Entity; id: 0xcae72949838f650b <x-coredata://71565814-E7F1-4D16-852F-7D8CBEA199F2/Entity/p2>; data: <fault>)] 2019-12-18 14:56:25.896548+0800 Journal (Core Data)[12470:652660] [Warning] Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a table view cell's content view. We're considering the collapse unintentional and using standard height instead. Cell: <Journal__Core_Data_.TableViewCell: 0x7fb367f6d8c0; baseClass = UITableViewCell; frame = (0 28; 414 99); text = 'Poop face'; clipsToBounds = YES; autoresize = W; layer = <CALayer: 0x600001769b00>>
答案 0 :(得分:0)
在“ TableViewController”文件中
替换var items: [Entity] = []
与此
var items: [Entity] = []{
didSet{
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
这将在每个新条目上重新加载tableView。此后,您无需在Controller中的任何位置添加tableView.reloadData()
。