在表格视图中重新加载并显示数据失败。

huangapple go评论66阅读模式
英文:

Failed to reload and display data in tableview

问题

以下是您提供的代码的中文翻译:

首先,我是Swift和iOS开发的初学者。这是我的第一个应用程序,我在学习Swift和iOS的基础知识后开始开发的。

在使用Realm添加新数据后,重新加载tableview中的数据正常工作。不幸的是,在更新或删除后,重新加载tableview中的数据不起作用。在更新或删除后显示以前的数据。请帮忙。

代码或应用程序的工作原理:

  1. 主页HomeViewController包含一个空的表视图中的数据列表和一个名为ADD的按钮。
  2. 按下ADD -> 跳转到添加页面,其中包含标题和注释字段 -> 按下DONE -> 返回主页,重新加载并显示数据中的标题。(这部分正常工作)
  3. 使用didSelectRowAt跳转到DisplayDataViewController,仅查看数据中的标题和注释。此页面包含EDIT和DELETE按钮。
  4. 按下EDIT -> 跳转到编辑页面即AddNotesViewController(使用相同的页面进行添加和编辑,这就是我使用flagAdd/flagEdit变量的原因) -> 更新后返回主页,并且应该重新加载和显示(重新加载在此处失败,tableview中没有更改)。
  5. 按下DELETE -> 返回主页,不幸的是,此处也无法重新加载。

而且,CRUD与数据库正常工作。

数据

import Foundation
import RealmSwift

@objcMembers class Data: Object {
    dynamic var title = ""
    dynamic var notes = ""
}

HomeViewController:

import UIKit
import RealmSwift

class HomeViewController: UIViewController, RefreshDelegate {

    @IBOutlet var tableViewField: UITableView!
    
    let realm = try! Realm()
    
    var taskView: Results<Data>?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableViewField.delegate = self
        tableViewField.dataSource = self
        loadData()
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let addVc = segue.destination as! AddNotesViewController
        addVc.flagAdd = 0
        addVc.delegate = self
    }

    func loadData() {
        taskView = realm.objects(Data.self)
        tableViewField.reloadData()
    }
}

extension HomeViewController: UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return taskView?.count ?? 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        cell.textLabel?.text = taskView?[indexPath.row].title ?? "Empty List"
        return cell
    }
}

extension HomeViewController: UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        let displayVc = storyboard?.instantiateViewController(identifier: "displayData") as! DisplayDataViewController
        displayVc.index1 = indexPath.row
        displayVc.viewDetails = taskView![indexPath.row]
        
        navigationController?.pushViewController(displayVc, animated: true)
    }
}

AddNotesViewController:

import UIKit
import RealmSwift

protocol RefreshDelegate {
    func loadData()
}

class AddNotesViewController: UIViewController {
    
    @IBOutlet var titleTextField: UITextField!
    @IBOutlet var notesTextView: UITextView!
    
    let realm = try! Realm()
    
    var delegate: RefreshDelegate?
    
    var EditView: Data?
    var index2: Int?
    
    var flagAdd: Int?
    var flagEdit: Int?
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        if flagEdit == 1 {
            navigationItem.title = "Edit Page"
            if let myEdit = self.EditView {
                titleTextField.text = myEdit.title
                notesTextView.text = myEdit.notes
            }
        }
    }
    
    @IBAction func doneButtonPressed(_ sender: UIBarButtonItem) {
        let note = notesTextView.text
        
        if titleTextField.text == ""
        {
            titleTextField.placeholder = "Write Something.."
        }
        else if note == "" || note == "Write notes here...." || note == "Sorry! Write Something here."
        {
            notesTextView.text = "Sorry! Write Something here."
        }
        else if flagAdd == 0
        {
            let titleText = titleTextField.text
            let notesField = notesTextView.text
            
            let dataRealm = Data()
            dataRealm.title = titleText!
            dataRealm.notes = notesField!
            
            try! realm.write{
                realm.add(dataRealm)
            }
            delegate?.loadData()
            titleTextField.endEditing(true)
            notesTextView.endEditing(true)
            
            navigationController?.popViewController(animated: true)
        }
        else
        {
            let newData = realm.objects(Data.self)[index2!]
            
            try! realm.write {
                newData.title = titleTextField.text!
                newData.notes = notesTextView.text
            }
            
            delegate?.loadData()
            
            titleTextField.endEditing(true)
            notesTextView.endEditing(true)
            
            navigationController?.popToRootViewController(animated: true)
        }
    }
}

相同的添加页面用作编辑页面。这就是我使用flagAdd/flagEdit变量的原因。

DisplayDataViewController:

import UIKit
import RealmSwift

class DisplayDataViewController: UIViewController {

    @IBOutlet var DisplayTitleField: UITextField!
    @IBOutlet var DisplayNotesField: UITextView!
    
    let realm = try! Realm()

    var index1: Int?
    var viewDetails: Data?
    
    var delegate: RefreshDelegate?
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        if let myView = self.viewDetails {
            DisplayTitleField.text = myView.title
            DisplayNotesField.text = myView.notes
        }
    }
    
    @IBAction func EditButtonPressed(_ sender: UIButton) {
        let editVc = storyboard?.instantiateViewController(identifier: "goToAdd") as! AddNotesViewController
        editVc.index2 = index1
        editVc.EditView = viewDetails
        editVc.flagEdit = 1
        
        navigationController?.pushViewController(editVc, animated: true)
    }
    
    @IBAction func deleteButtonPressed(_ sender: UIBarButtonItem) {
        try! realm.write {
            realm.delete(viewDetails!)
        }
        
        delegate?.loadData()
        navigationController?.popViewController(animated: true)
    }
}

希望这有助于您理解代码的中文翻译。如果您有任何进一步的问题或

英文:

Firstly, I'm a beginner in Swift and iOS development. This is my first app, which I am working on after learning the basics of Swift and iOS.

Reloading the data in the tableview after adding new data is working properly using Realm. Unfortunately, after an update or deletion, reloading the data in the tableview does not work. Showing the previous data in table view after an update or deletion. Please help.

How the code works or app works:

  1. Home page HomeViewController contains an empty list of data in table view and a button called ADD.
  2. ADD pressed --> jump to add page, having field for title and notes --> DONE pressed --> return to home, reload and display the title in the Data.(This is working)
  3. Using didSelectRowAt it jumps to the DisplayDataViewController, just to view the title and notes in the Data. This page contains EDIT and DELETE button.
  4. EDIT pressed --> jump to edit page ie AddNotesViewController (same page is used for add and edit, that's why I used flagAdd/flagEdit variables) --> after update it goes to home page, and it should be reloaded and display (reloading fails here, no changes in tableview).
  5. DELETE pressed --> goes home, unfortunately reloading fails here also.

And the thing is, CRUD is working properly with the database.

Data

import Foundation
import RealmSwift
@objcMembers class Data: Object {
dynamic var title = &quot;&quot;
dynamic var notes = &quot;&quot;
}

HomeViewController:

import UIKit
import RealmSwift
class HomeViewController: UIViewController, RefreshDelegate {
@IBOutlet var tableViewField: UITableView!
let realm = try! Realm()
var taskView: Results&lt;Data&gt;?
override func viewDidLoad() {
super.viewDidLoad()
tableViewField.delegate = self
tableViewField.dataSource = self
loadData()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let addVc = segue.destination as! AddNotesViewController
addVc.flagAdd = 0
addVc.delegate = self
}
func loadData() {
taskView = realm.objects(Data.self)
tableViewField.reloadData()
}
}
extension HomeViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
return taskView?.count ?? 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: &quot;Cell&quot;, for: indexPath)
cell.textLabel?.text = taskView?[indexPath.row].title ?? &quot;Empty List&quot;
return cell
}
}
extension HomeViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let displayVc = storyboard?.instantiateViewController(identifier: &quot;displayData&quot;) as! DisplayDataViewController
displayVc.index1 = indexPath.row
displayVc.viewDetails = taskView![indexPath.row]
navigationController?.pushViewController(displayVc, animated: true)
}
}

AddNotesViewController:

import UIKit
import RealmSwift
protocol RefreshDelegate {
func loadData()
}
class AddNotesViewController: UIViewController {
@IBOutlet var titleTextField: UITextField!
@IBOutlet var notesTextView: UITextView!
let realm = try! Realm()
var delegate: RefreshDelegate?
var EditView : Data?
var index2: Int?
var flagAdd: Int?
var flagEdit: Int?
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
if flagEdit == 1 {
navigationItem.title = &quot;Edit Page&quot;
if let myEdit = self.EditView {
titleTextField.text = myEdit.title
notesTextView.text = myEdit.notes
}
}
}
@IBAction func doneButtonPressed(_ sender: UIBarButtonItem) {
let note = notesTextView.text
if titleTextField.text == &quot;&quot;
{
titleTextField.placeholder = &quot;Write Something..&quot;
}
else if note == &quot;&quot; || note == &quot;Write notes here....&quot; || note == &quot;Sorry! Write Something here.&quot;
{
notesTextView.text = &quot;Sorry! Write Something here.&quot;
}
else if flagAdd == 0
{
let titleText = titleTextField.text
let notesField = notesTextView.text
let dataRealm = Data()
dataRealm.title = titleText!
dataRealm.notes = notesField!
try! realm.write{
realm.add(dataRealm)
}
delegate?.loadData()
titleTextField.endEditing(true)
notesTextView.endEditing(true)
navigationController?.popViewController(animated: true)
}
else
{
let newData = realm.objects(Data.self)[index2!]
try! realm.write {
newData.title = titleTextField.text!
newData.notes = notesTextView.text
}
delegate?.loadData()
titleTextField.endEditing(true)
notesTextView.endEditing(true)
navigationController?.popToRootViewController(animated: true)
}
}
}

The same add page is used as an edit page. That's why I used the flagAdd/flagEdit variable.

DisplayDataViewController:

import UIKit
import RealmSwift
class DisplayDataViewController: UIViewController {
@IBOutlet var DisplayTitleField: UITextField!
@IBOutlet var DisplayNotesField: UITextView!
let realm = try! Realm()
var index1 : Int?
var viewDetails : Data?
var delegate : RefreshDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
if let myView = self.viewDetails {
DisplayTitleField.text = myView.title
DisplayNotesField.text = myView.notes
}
}
@IBAction func EditButtonPressed(_ sender: UIButton) {
let editVc = storyboard?.instantiateViewController(identifier: &quot;goToAdd&quot;) as! AddNotesViewController
editVc.index2 = index1
editVc.EditView = viewDetails
editVc.flagEdit = 1
navigationController?.pushViewController(editVc, animated: true)
}
@IBAction func deleteButtonPressed(_ sender: UIBarButtonItem) {
try! realm.write {
realm.delete(viewDetails!)
}
delegate?.loadData()
navigationController?.popViewController(animated: true)
}
}

答案1

得分: 1

Here is the translated content:

HomeViewController
我所做的只是在 didSelectRowAt 中添加了一行代码。以下是更新后的 didSelectRowAt 代码:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)

    let dv = (storyboard?.instantiateViewController(identifier: "displayData"))! as! DisplayDataViewController

    dv.index1 = indexPath.row
    dv.viewDetails = taskView![indexPath.row]

    dv.delegate = self // 新添加的代码。它解决了一个删除问题。

    navigationController?.pushViewController(dv, animated: true)
}

DisplayDataViewController,在这里我也添加了 ev.delegate = delegate。以下是更新的 EditButtonPressed 代码:

@IBAction func EditButtonPressed(_ sender: UIButton) {
    let ev = storyboard?.instantiateViewController(identifier: "goToAdd") as! AddNotesViewController
    ev.EditView = viewDetails
    ev.index2 = index1!
    ev.flagEdit = 1

    ev.delegate = delegate // 新添加的代码。它解决了一个更新问题。

    navigationController?.pushViewController(ev, animated: true)
}

最后,它已经可以工作了。

英文:

HomeViewController,
What I did is just add one line of code in didSelectRowAt. Here is the updated didSelectRowAt :

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        let dv = (storyboard?.instantiateViewController(identifier: &quot;displayData&quot;))! as! DisplayDataViewController
        
        dv.index1 = indexPath.row
        dv.viewDetails = taskView![indexPath.row]

        dv.delegate = self // Newly added code.It solves a problem for deletion.

        navigationController?.pushViewController(dv, animated: true)
}

DisplayDataViewController , Here also I added ev.delegate = delegate. Here is the updated code for EditButtonPressed

@IBAction func EditButtonPressed(_ sender: UIButton) {
        let ev = storyboard?.instantiateViewController(identifier: &quot;goToAdd&quot;) as! AddNotesViewController
        ev.EditView = viewDetails
        ev.index2 = index1!
        ev.flagEdit = 1

        ev.delegate = delegate // Newly added code.It solves a problem for updating.
        
        navigationController?.pushViewController(ev, animated: true)    
    }

Finally it's working.

huangapple
  • 本文由 发表于 2023年5月11日 01:01:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220948.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定