ios - 注释不会删除

标签 ios swift

我的应用程序有一个笔记部分,我可以创建笔记和编辑笔记,但在删除笔记时我遇到了问题。我可以从表格 View 本身和所有注释中删除它们,但是当我重新加载应用程序时,它们又回来了,就像它们粘在字典中一样。您能告诉我一种永久删除它们的方法吗?我有三个 Controller ,只有两个相关。以下是相关内容:

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

提前致谢 山姆

最佳答案

在主视图 Controller 中使用这个修改过的函数:

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 中的保存笔记功能

关于ios - 注释不会删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29831597/

相关文章:

ios - 如何在 uiscrollview 中点击后旋转图像?

ios - OpenGL 透明背景 Xamarin

arrays - Swift,字符串检查

ios - 在 Swift 中使用未解析的标识符 'present'(Apple 教程)

ios - 如何在 Swift 中创建一个空数组?

iOS - 检查蓝牙是否打开而没有向用户弹出系统警报

ios - 在 Xcode 类型 "ViewController"中获取错误不符合协议(protocol)“UITableViewDataSource”

iphone - 如何在图像上写入文字并通过邮件发送?

ios - swift 中 AppDelegate 和 Context 的作用是什么?

swift - 如何在没有固定宽度的情况下将文本包装在 UILabel 中(通过 UIViewRepresentable)?