简体   繁体   English

如何修复 Table Viewcontroller 上的时间延迟?

[英]How can I fix a time delay on my Table Viewcontroller?

I created a note app.我创建了一个笔记应用程序。 The NotizenViewControllerArchive save the note and with the NotizenViewControllerAdd can you add new notes. NotizenViewControllerArchive保存注释,使用NotizenViewControllerAdd可以添加新注释。

There is a third Viewcontroller, on this I can visit the other two views, but after I switched (with a button) from the third Viewcontroller to NotizenViewControllerArchive there is a time delay, a short moment who I can't see my notes how can I fix that so the notes shows immediately?.还有第三个 Viewcontroller,在这我可以访问其他两个视图,但是在我(用一个按钮)从第三个 Viewcontroller 切换到NotizenViewControllerArchive之后,有一个时间延迟,一小会我看不到我的笔记怎么能我解决了这个问题,所以笔记立即显示?

I tried for a long time to fix it but I don't know how, I put the whole code in because I don't know where the problem is.我尝试了很长时间来修复它,但我不知道如何,我将整个代码放入,因为我不知道问题出在哪里。 Thanks for your help谢谢你的帮助

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")

    }
  }
}

NotizenViewControllerAdd 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.
  }
}

Try moving the code from viewDidAppear to viewWillAppear or viewDidLoad on your NotizenViewControllerArchive.尝试将代码从 viewDidAppear 移动到 NotizenViewControllerArchive 上的 viewWillAppear 或 viewDidLoad。 The code inside the viewDidAppear will only execute after the view is already on screen for the user (hence the name) viewDidAppear 中的代码只会在视图已经出现在用户屏幕上之后执行(因此得名)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM