[英]How can I fix a time delay on my Table Viewcontroller?
我创建了一个笔记应用程序。 NotizenViewControllerArchive
保存注释,使用NotizenViewControllerAdd
可以添加新注释。
还有第三个 Viewcontroller,在这我可以访问其他两个视图,但是在我(用一个按钮)从第三个 Viewcontroller 切换到NotizenViewControllerArchive
之后,有一个时间延迟,一小会我看不到我的笔记怎么能我解决了这个问题,所以笔记立即显示?
我尝试了很长时间来修复它,但我不知道如何,我将整个代码放入,因为我不知道问题出在哪里。 谢谢你的帮助
NotizenViewControllerArchive
import UIKit
class NotizenViewControllerArchive: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var table: UITableView!
var items:[String] = []
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "Cell")
let backgroundView = UIView()
backgroundView.backgroundColor = UIColor.init(red: 48/255, green: 173/255, blue: 99/255, alpha: 1)
cell.selectedBackgroundView = backgroundView
cell.textLabel?.text = items[indexPath.row]
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewDidAppear(_ animated: Bool) {
let itemObject = UserDefaults.standard.object(forKey: "items")
if let tempItems = itemObject as? [String] {
items = tempItems
}
table.reloadData()
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCell.EditingStyle.delete {
items.remove(at: indexPath.row)
tableView.reloadData()
UserDefaults.standard.set(items, forKey: "items")
}
}
}
NotizenViewController添加
import UIKit
class NotizenViewControllerAdd: UIViewController, UITextFieldDelegate {
@IBOutlet weak var itemTextField: UITextField!
@IBAction func ad(_ sender: UIButton) {
let itemObject = UserDefaults.standard.object(forKey: "items")
var items:[String]
if let tempItems = itemObject as? [String] {
items = tempItems
items.append(itemTextField.text!)
print("items")
} else {
items = [itemTextField.text!]
}
UserDefaults.standard.set(items, forKey: "items")
itemTextField.text = ""
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
return true
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
尝试将代码从 viewDidAppear 移动到 NotizenViewControllerArchive 上的 viewWillAppear 或 viewDidLoad。 viewDidAppear 中的代码只会在视图已经出现在用户屏幕上之后执行(因此得名)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.