2015-04-23 16 views
0

我有我的应用程序的注释部分,我可以创建注释和编辑注释,但是当涉及到删除注释时,我有一个问题。我可以从表格视图本身和所有注释中删除它们,但是当我重新加载应用程序时,它们会返回,就好像它们粘在字典中一样。你能否请求我永久删除它们?我有三个控制器,只有两个是相关的。以下是相关的:注意事项不会删除

MasterViewController.swift

import UIKit 

class MasterViewController: UITableViewController { 

@IBOutlet weak var menuButton: UIBarButtonItem! 

override func awakeFromNib() { 
    super.awakeFromNib() 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 
    Note.loadnotes() 
    noteTable = self.tableView 
    // Side Menu 
    if self.revealViewController() != nil { 
     menuButton.target = self.revealViewController() 
     menuButton.action = "revealToggle:" 
     self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer()) 
    } 
    // Do any additional setup after loading the view, typically from a nib. 

func insertNewObject(sender: AnyObject) { 
    allNotes.insert(Note(), atIndex: 0) 
    let indexPath = NSIndexPath(forRow: 0, inSection: 0) 
    self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) 
    self.performSegueWithIdentifier("showDetail", sender: self) 
} 

// MARK: - Segues 

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if segue.identifier == "showDetail" { 
     if let indexPath = self.tableView.indexPathForSelectedRow() { 
      let object: Note = allNotes[indexPath.row] as Note 
      currentNoteIndex = indexPath.row 
     } 
     else { 
      currentNoteIndex = 0 
     } 
    } 
} 

// MARK: - Table View 

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return allNotes.count 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell 

    let object: Note = allNotes[indexPath.row] 
     as Note; cell.textLabel!.text = object.note 
    return cell 
} 


override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
    if editingStyle == .Delete { 
     allNotes.removeAtIndex(indexPath.row) 
     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. 
    } 
} 


} 

note.swift提前

import UIKit 

var allNotes:[Note] = [] 
var currentNoteIndex:NSInteger = -1 
var noteTable:UITableView? 

let KAllNotes:String = "notes" 


class Note: NSObject { 
var date:String 
var note:String 

override init() { 
    date = NSDate().description 
    note = "" 
} 

func dictionary() -> NSDictionary { 
    return ["note":note, "date":date] 
} 

class func saveNotes() { 
    var aDictionaries:[NSDictionary] = [] 
    for (var i:NSInteger = 0; i < allNotes.count; i++) { 
     aDictionaries.append(allNotes[i].dictionary()) 
    } 
    NSUserDefaults.standardUserDefaults().setObject(aDictionaries, forKey: KAllNotes) 
    //  aDictionaries.writeToFile(filePath(), atomically: true) 
} 

class func loadnotes() { 
    var defaults:NSUserDefaults = NSUserDefaults.standardUserDefaults() 
    var savedData:[NSDictionary]? = defaults.objectForKey(KAllNotes) as? [NSDictionary] 
    //  var savedData:NSArray? = NSArray(contentsOfFile: filePath()) 
    if let data:[NSDictionary] = savedData { 
     for (var i:NSInteger = 0; i < data.count; i++) { 
      var n:Note = Note() 
      n.setValuesForKeysWithDictionary(data[i] as [NSObject : AnyObject]) 
      allNotes.append(n) 
     } 
    } 
} 

class func filePath() -> String { 
    var d:[String]? = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true) as? [String] 
    if let directories:[String] = d { 
     var docsDirectory:String = directories[0] 
     var path:String = docsDirectory.stringByAppendingPathComponent("\(KAllNotes).notes") 
     return path; 
    } 
    return "" 
} 
} 

感谢 山姆

+2

1)请将所有代码缩小到与删除问题相关的部分。 2)删除记事后,你会在哪里试图保存数据? – rmaddy

+0

在MasterViewController中,最后一个覆盖功能是删除部分 – ShizukaNaHaji

+0

您是否将保存的文档保存在文档目录中? KAllNotes.notes文件包含的内容 –

回答

1

在主视图控制器使用此功能改变:

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
    if editingStyle == .Delete { 
     allNotes.removeAtIndex(indexPath.row) 
     tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 
     Note.saveNotes() 
    } 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. 
    } 
} 

这将强制重新保存字典。使用notes.swift中的保存笔记功能