ios - 字符串存储.plist不保存/加载并且不填充UITableView

标签 ios swift uitableview plist

在我正在从事的一个项目中,我有一个笔记式功能,充当练习/训练日志。该训练日志由 5 个文件组成:Note.swift、NotesTableViewController.swift、NoteDetailViewController.swift、NoteDetailTableViewCell.swift 和 NoteStore.swift。该表的类是 NotesTableViewController,它是一个带有 UITableViewDelegate 和 UITableViewDataSource 的 UIViewController。此笔记功能可以正常工作,填充表格 View ,但无法将笔记保存到 .plist 文件并在重新打开应用程序时检索它。我不知道这是否实际上是保存/加载失败,或者其他地方是否出了问题。我将不胜感激任何帮助。该项目可通过 https://github.com/JohnnyH1012/HRC 获取。 。文件如下:

Note.swift

import Foundation

class Note : NSObject, NSCoding {
var title = ""
var text = ""
var date = NSDate() // Defaults to current date / time

// Computed property to return date as a string
var shortDate : NSString {
    let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "MM/dd/yy"
        return dateFormatter.stringFromDate(self.date)
}

override init() {
    super.init()
}

// 1: Encode ourselves...
func encodeWithCoder(aCoder: NSCoder) {
    aCoder.encodeObject(title, forKey: "title")
    aCoder.encodeObject(text, forKey: "text")
    aCoder.encodeObject(date, forKey: "date")
}

// 2: Decode ourselves on init
required init(coder aDecoder: NSCoder) {
    self.title = aDecoder.decodeObjectForKey("title") as! String
    self.text  = aDecoder.decodeObjectForKey("text") as! String
    self.date   = aDecoder.decodeObjectForKey("date") as! NSDate
}

}

NotesTableViewController.swift

import UIKit

class NotesTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tableView: UITableView!

@IBOutlet weak var OpenButton: UIBarButtonItem!

override func viewDidLoad() {
    super.viewDidLoad()

    // Leverage the built in TableViewController Edit button
    self.navigationItem.leftBarButtonItem = self.editButtonItem()

    OpenButton.target = self.revealViewController()
    OpenButton.action = Selector("revealToggle:")

    self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())

    self.tableView.reloadData()

}

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    // ensure we are not in edit mode
    editing = false
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Here we pass the note they tapped on between the view controllers
    if segue.identifier == "NoteDetailPush" {
        // Get the controller we are going to
        var noteDetail = segue.destinationViewController as! NoteDetailViewController
        // Lookup the data we want to pass
        var theCell = sender as! NoteDetailTableViewCell
        // Pass the data forward
        noteDetail.theNote = theCell.theNote
    }
}


@IBAction func saveFromNoteDetail(segue:UIStoryboardSegue) {
    // We come here from an exit segue when they hit save on the detail screen

    // Get the controller we are coming from
    var noteDetail = segue.sourceViewController as! NoteDetailViewController

    // If there is a row selected....
    if let indexPath = tableView.indexPathForSelectedRow() {
        // Update note in our store
        NoteStore.sharedNoteStore.updateNote(theNote: noteDetail.theNote)

        // The user was in edit mode
        tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
    } else {
        // Otherwise, add a new record
        NoteStore.sharedNoteStore.createNote(theNote: noteDetail.theNote)

        // Get an index to insert the row at
        var indexPath = NSIndexPath(forRow: NoteStore.sharedNoteStore.count()-1, inSection: 0)

        // Update tableview
        tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
    }
}

// MARK: - Table view data source

  func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // Just return the note count
    return NoteStore.sharedNoteStore.count()
}


 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    // Fetch a reusable cell
    let cell = tableView.dequeueReusableCellWithIdentifier("NoteDetailTableViewCell", forIndexPath: indexPath) as! NoteDetailTableViewCell

    // Fetch the note
    var rowNumber = indexPath.row
    var theNote = NoteStore.sharedNoteStore.getNote(rowNumber)

    // Configure the cell
    cell.setupCell(theNote)

    return cell
}


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

    if editingStyle == .Delete {
        // Delete the row from the data source
        NoteStore.sharedNoteStore.deleteNote(indexPath.row)
        // Delete the note from the tableview
        tableView.deleteRowsAtIndexPaths([indexPath],        withRowAnimation: .Fade)
    }
}


}

NoteDetailViewController

import UIKit

class NoteDetailViewController: UIViewController {

var theNote = Note()

@IBOutlet weak var noteTitleLabel: UITextField!

@IBOutlet weak var noteTextView: UITextView!

override func viewDidLoad() {
    super.viewDidLoad()

    // The view starts here. By now we either have a note to edit
    // or we have a blank note in theNote property we can use

    // Update the screen with the contents of theNote
    self.noteTitleLabel.text = theNote.title
    self.noteTextView.text = theNote.text

    // Set the Cursor in the note text area
    self.noteTextView.becomeFirstResponder()
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Whenever we leave the screen, update our note model
    theNote.title = self.noteTitleLabel.text
    theNote.text = self.noteTextView.text
}

@IBAction func CancelNote(sender: AnyObject) {

    self.dismissViewControllerAnimated(true, completion: nil)

}
}

NoteDetailTableViewCell

import UIKit

class NoteDetailTableViewCell : UITableViewCell {

// The note currently being shown
weak var theNote : Note!

// Interface builder outlets
@IBOutlet weak var noteTitleLabel : UILabel!
@IBOutlet weak var noteDateLabel : UILabel!
@IBOutlet weak var noteTextLabel : UILabel!

// Insert note contents into the cell
func setupCell(theNote:Note) {
    // Save a weak reference to the note
    self.theNote = theNote

    // Update the cell
    noteTitleLabel.text = theNote.title
    noteTextLabel.text = theNote.text
    noteDateLabel.text = theNote.shortDate as String
}

}

最后是 NoteStore

import Foundation

class NoteStore {
// Mark: Singleton Pattern (hacked since we don't have class var's yet)
class var sharedNoteStore : NoteStore {
struct Static {
    static let instance : NoteStore = NoteStore()
    }
    return Static.instance
}

// Private init to force usage of singleton
private init() {
    load()
}

// Array to hold our notes
private var notes : [Note]!

// CRUD - Create, Read, Update, Delete

// Create

func createNote(theNote:Note = Note()) -> Note {
    notes.append(theNote)
    return theNote
}

// Read

func getNote(index:Int) -> Note {
    return notes[index]
}

// Update
func updateNote(#theNote:Note) {
    // Notes passed by reference, no update code needed
}

// Delete
func deleteNote(index:Int) {
    notes.removeAtIndex(index)
}

func deleteNote(withNote:Note) {

    for (i, note) in enumerate(notes) {
        if note === withNote {
            notes.removeAtIndex(i)
            return
        }
    }

}

// Count
func count() -> Int {
    return notes.count
}


// Mark: Persistence

// 1: Find the file & directory we want to save to...
func archiveFilePath() -> String {
    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
    let documentsDirectory = paths.first as! NSString
    let path = documentsDirectory.stringByAppendingPathComponent("NoteStore.plist")

    return path
}

// 2: Do the save to disk.....
func save() {
    NSKeyedArchiver.archiveRootObject(notes, toFile: archiveFilePath())
}


// 3: Do the reload from disk....
func load() {
    let filePath = archiveFilePath()
    let fileManager = NSFileManager.defaultManager()

    if fileManager.fileExistsAtPath(filePath) {
        notes = NSKeyedUnarchiver.unarchiveObjectWithFile(filePath) as! [Note]
    } else {
        notes = [Note]()
    }
}

}

最佳答案

NoteStore.sharedNoteStore.save() 从未被调用

关于ios - 字符串存储.plist不保存/加载并且不填充UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31435261/

相关文章:

iphone - 想要完全释放对象的内存

ios - 如何让 uitableview 填满我所有的 View ?

ios - 设置状态栏颜色 - 延迟?

ios - 在 UICollectionView 选择上选择 UITableView

ios - 在不同的表格 View 单元格布局之间快速切换

swift - 如何快速选择aTableView部分中的单元格

ios - 如何从 iOS 上的另一个应用程序打开 Google Play 音乐应用程序?

ios - 每次检测 WKWebView 何时完成加载

ios - 在 ScrollView 底部后滚动放大图像

swift - 在结构中的数组中搜索数组结构