gpt4 book ai didi

ios - 日志 - DispatchQueue - self.tableView.reloadData() 未被调用

转载 作者:行者123 更新时间:2023-11-29 05:13:57 25 4
gpt4 key购买 nike

我刚刚开始并组装了另一个列表应用程序,因为那里没有足够的...但我遇到了一个问题,即输入新条目后没有调用 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>>

最佳答案

在您的“TableViewController”文件中替换 var items: [Entity] = []

有了这个

var items: [Entity] = []{
didSet{
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}

这将在每个新条目上重新加载您的 tableView。此后,您不需要在 Controller 中的任何位置添加 tableView.reloadData()

关于ios - 日志 - DispatchQueue - self.tableView.reloadData() 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59386944/

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