简体   繁体   English

数据不会加载到我的表格视图中

[英]Data won't load into my Table View

So I have a UITableViewController class for my notes app where everything is working fine, adding, deleting, and editing of the table view. 因此,我为我的notes应用程序提供了一个UITableViewController类,其中一切工作正常,可以添加,删除和编辑表视图。 But now I'm trying to load the data using "Realm" Database. 但是现在我正在尝试使用“ Realm”数据库加载数据。 I'm fairly new to iOS (Swift) and especially Realm, so I'm kind of confused. 我对iOS(Swift)尤其是Realm并不陌生,所以我有点困惑。 I got it to save into the database perfectly, and it shows up when I click the "Save" button in my navigation bar. 我将其完美地保存到数据库中,当我单击导航栏中的“保存”按钮时,它就会显示出来。 But when I restart the app, the table view is completely empty. 但是,当我重新启动应用程序时,表视图完全为空。 I've been stuck on this for a while now, tried everything I know so far, but just can not get it to show up whatsoever. 我已经坚持了一段时间,尝试了到目前为止我所知道的一切,但是却无法显示出来。 Can someone help me out, and maybe tell me what I'm doing wrong, or not doing at all? 有人可以帮我,也许告诉我我做错了什么,还是根本没有做? Thank You very much. 非常感谢你。

Here is my class as well 这也是我的课

import UIKit
import Realm

class NoteTableViewController: UITableViewController {

    // MARK: Properties

    var notes = [Note]() // Initialized with a default value (an empty array of Note objects).

    override func viewDidLoad() {
        super.viewDidLoad()

        // Get Realm Database location
        println(RLMRealm.defaultRealm().path)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // Return the number of sections.
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // Return the number of rows in the section.
        return notes.count
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        // Table view cells are reused and should be dequeued using a cell identifier.
        let cellIdentifier = "NoteTableViewCell"
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! NoteTableViewCell

        // Fetches the appropriate note for the data source layout in the notes array
        let note = notes[indexPath.row]

        // Configure the cell...
        cell.titleLabel.text = note.title
        cell.bodyLabel.text = note.body

        return cell
    }



    // Override to support conditional editing of the table view.
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return NO if you do not want the specified item to be editable.
        return true
    }



    // Override to support editing the table view.
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            // Delete the row from the data source
            notes.removeAtIndex(indexPath.row)

            // Get the default Realm (Will do this part later)
            /*
            let realm = RLMRealm.defaultRealm()
            realm.beginWriteTransaction()

            realm.deleteObject(notes[Int(indexPath.row)])
            realm.commitWriteTransaction()
            */

            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        } else if editingStyle == .Insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }
    }


    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "ShowDetail" { // Clicked on a cell
            let noteDetailViewController = segue.destinationViewController as! NoteViewController
            // Get the cell that generated this segue.
            if let selectedNoteCell = sender as? NoteTableViewCell {
                let indexPath = tableView.indexPathForCell(selectedNoteCell)!
                let selectedNote = notes[indexPath.row]
                noteDetailViewController.note = selectedNote
            }
        }
        else if segue.identifier == "AddItem" { // Clicked add button
            println("Adding new note")
        }
    }


    @IBAction func unwindToNoteList(sender: UIStoryboardSegue) {
        if let sourceViewController = sender.sourceViewController as? NoteViewController, note = sourceViewController.note {
            if let selectedIndexPath = tableView.indexPathForSelectedRow() { // User clicked on a row
                // Update an existing note.
                notes[selectedIndexPath.row] = note

                tableView.reloadRowsAtIndexPaths([selectedIndexPath], withRowAnimation: .None)
            }
            else {
                // Add a new note.
                let newIndexPath = NSIndexPath(forRow: notes.count, inSection: 0)
                notes.append(note)

                // Persist in database
                let realm = RLMRealm.defaultRealm()
                realm.beginWriteTransaction()
                Note.createInRealm(realm, withValue: notes[newIndexPath.row])
                realm.commitWriteTransaction()

                tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Bottom)
            }
        }
    }

}

And this one is the Note Object class 这是Note Object类

import UIKit // Automatically imports Foundation
import Realm

class Note: RLMObject {

    // MARK: Properties

    dynamic var title: String = ""
    dynamic var body: String = ""

    // MARK: Initialization

    init?(title: String, body: String) { // Failable initializer


        // Initialize stored properties.
        self.title = title
        self.body = body

        super.init()

        // Initialization should fail if there is no title
        if title.isEmpty {
            return nil
        }

    }

    // Must have for Realm to work
    override init() {
        super.init()
    }

}

Solved it after a good 2 more days...Here is my updated class 再过两天就解决了...这是我更新的课程

Did as Swinny89 said, and started with a new notes object, of all objects, instead of initializing an "empty" notes array. 就像Swinny89所说的那样,并从所有对象中的一个新的notes对象开始,而不是初始化一个“空” notes数组。

import UIKit
import Realm

class NoteTableViewController: UITableViewController {

  // MARK: Properties

    var notes = Note.allObjects()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Get Realm Database location
        println(RLMRealm.defaultRealm().path)

    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // Return the number of sections.
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // Return the number of rows in the section.
        return Int(notes.count)
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        // Table view cells are reused and should be dequeued using a cell identifier.
        let cellIdentifier = "NoteTableViewCell"
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! NoteTableViewCell

        let object = notes[UInt(indexPath.row)] as! Note

        // Configure the cell...
        cell.titleLabel.text = object.title
        cell.bodyLabel.text = object.body

        return cell
    }

    // Override to support conditional editing of the table view.
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return NO if you do not want the specified item to be editable.
        return true
    }

    // Override to support editing the table view.
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {

            // Delete from database
            let realm = RLMRealm.defaultRealm()
            realm.beginWriteTransaction()
            realm.deleteObject(notes[UInt(indexPath.row)] as! RLMObject)
            realm.commitWriteTransaction()

            // Delete row from table view
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        } else if editingStyle == .Insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }
    }

    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "ShowDetail" { // Clicked on a cell
            let noteDetailViewController = segue.destinationViewController as! NoteViewController
            // Get the cell that generated this segue.
            if let selectedNoteCell = sender as? NoteTableViewCell {
                let indexPath = tableView.indexPathForCell(selectedNoteCell)!

                let selectedNote = notes[UInt(indexPath.row)] as! Note
                noteDetailViewController.note = selectedNote

            }
        }
        else if segue.identifier == "AddItem" { // Clicked add button
            println("Adding new note")
        }
    }

    @IBAction func unwindToNoteList(sender: UIStoryboardSegue) {
        if let sourceViewController = sender.sourceViewController as? NoteViewController, note = sourceViewController.note {

            let uuid = NSUUID().UUIDString // Needed for primary key. see below

            var unwindedNote = Note()

            if let selectedIndexPath = tableView.indexPathForSelectedRow() { // User clicked on a row

                // Updating of the note is done in NoteViewController

                tableView.reloadRowsAtIndexPaths([selectedIndexPath], withRowAnimation: .None)
            }
            else {
                // Add a new note.
                let newIndexPath = NSIndexPath(forRow: Int(notes.count), inSection: 0)

                // Persist in database
                let realm = RLMRealm.defaultRealm()
                realm.beginWriteTransaction()

                unwindedNote.title = note.title
                unwindedNote.body = note.body
                unwindedNote.id = uuid // This is done for the primary key that Realm needs, unique for each object created.

                realm.addObjects([unwindedNote])
                realm.commitWriteTransaction()

                tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Bottom)
            }
        }
    }
}

This is because your Note array is initialised as an empty array and that's what you are using to tell the tableView how many rows there are, which is 0. 这是因为您的Note数组被初始化为一个空数组,这就是您用来告诉tableView的行数为0的原因。

Once your Note array has been set and has some data you can then call tableView.reloadData() to reload the array with the data. 一旦设置了Note数组并包含一些数据,就可以调用tableView.reloadData()来重新加载数据。

Also, looking at the code above, your class is inheriting from UITableViewController rather than implementing UITableViewControllerDelegate and UITableViewControllerDataSource . 另外,查看上面的代码,您的类是从UITableViewController继承的,而不是实现UITableViewControllerDelegateUITableViewControllerDataSource Once your class implements these you need to make sure that you set the viewController as the datasource and delegate for the tableViewController either in the storyboard or through code. 一旦您的类实现了这些,就需要确保在故事板上或通过代码将viewController设置为数据源并为tableViewController委托。

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

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