Userdefaults Tableview 地标 Swift 3

Userdefaults Tableview placemark Swift 3

我正在使用 uitableview。当我 select 编辑了一个单元格时,我试图保存地标。从下面的代码中,我添加了一个表格视图,当我 select 一个单元格时,将显示地标。但我无法保存它,因为当我转到另一个视图控制器并返回时,它没有显示。我研究过并发现我需要使用 UserDefaults 但我不知道如何使用它。有人可以指出我如何实现这一目标。谢谢

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")

    let contactsCell = app.helper.contacts[indexPath.row]
    cell!.textLabel?.text = contactsCell

    return cell!
}


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let contactToCall = app.helper.contacts[indexPath.row]
    app.helper.contactSelected = contactToCall

    if let cell = tableView.cellForRow(at: indexPath) {
        if cell.isSelected {
            cell.accessoryType = .checkmark


        }
    }

}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {

    if let cell = tableView.cellForRow(at: indexPath) {
        cell.accessoryType = .none
    }
}

你不需要 UserDefaults 除非你想在应用程序关闭并重新启动后保持状态,一旦你开始存储用户默认值,你可能想要存储更多 - 所以创建是有意义的默认数据 class 用于存储 selected 索引和您可能需要的任何其他 'stuff'。

在此示例中,我将在 viewDidLoad 上加载默认值并将默认值存储在每个 select / deselect 上。如果您存储了其他值,则应该进行额外的存储调用。

创建一个 class 来存储您的数据

class MyDefaultsData : NSObject, NSCoding
{
    var selectedIndexPath : IndexPath?
    var otherStuff : String?

    override init()
    {
        // you can set up default values here if you need them
        selectedIndexPath = nil
        otherStuff = nil
    }

    // archiving code
    func encode(with aCoder: NSCoder)
    {
        // you can't store IndexPath directly, so split it into row and section
        if selectedIndexPath == nil
        {
            // set invalid values which we can identify later
            aCoder.encode(-1, forKey: "indexSection")
            aCoder.encode(-1, forKey: "indexRow")
        }
        else
        {
            aCoder.encode(selectedIndexPath!.section, forKey: "indexSection")
            aCoder.encode(selectedIndexPath!.row, forKey: "indexRow")
        }
        aCoder.encode(otherStuff, forKey: "otherStuff")
    }

    required init(coder aDecoder: NSCoder)
    {
        let indexSection = aDecoder.decodeInteger(forKey: "indexSection")
        let indexRow = aDecoder.decodeInteger(forKey: "indexRow")
        if indexSection != -1 && indexRow != -1  // if no row is selected, these values will both be -1
        {
            selectedIndexPath   = IndexPath(row: indexRow, section: indexSection)
        }
        else
        {
            selectedIndexPath = nil
        }

        self.otherStuff     = aDecoder.decodeObject(forKey: "otherStuff") as? String
    }

}

在 ViewController

中定义默认数据变量
var defaultData         = MyDefaultsData()

在您的 viewDidLoad

中加载数据
override func viewDidLoad()
{
    loadDefaults()
}

func loadDefaults()
{
    var dataDefaults : MyDefaultsData?
    if let data = UserDefaults.standard.object(forKey: "NSDefaultsTest") as? Data
    {
        dataDefaults =  NSKeyedUnarchiver.unarchiveObject(with: data) as? MyDefaultsData

        self.defaultData.otherStuff = dataDefaults?.otherStuff ?? "No value found"

        self.defaultData.selectedIndexPath = dataDefaults?.selectedIndexPath
    }
}

然后您在 cellForRowAt

中使用该默认数据
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")

    let contactsCell = app.helper.contacts[indexPath.row]
    cell!.textLabel?.text = contactsCell

    // set or clear the checkmark
    if selectedIndexPath == indexPath
    {
        cell.accessoryType = .checkmark
    }
    else
    {
        cell.accessoryType = .none
    }
    return cell!
}

当您更改 selected 行时,您需要重新加载前一个 selected 行(如果有)和当前 selected 行,以获得复选标记绘制

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
    let previousIndexPath = defaultData.selectedIndexPath
    if previousIndexPath != indexPath
    {
        defaultData.selectedIndexPath = indexPath
    }
    else
    {
        defaultData.selectedIndexPath = nil
    }
    storeDefaults()


    if previousIndexPath != nil
    {
        tableView.reloadRows(at: [previousIndexPath!], with: .automatic)
    }
    if defaultData.selectedIndexPath != nil
    {
        tableView.reloadRows(at: [defaultData.selectedIndexPath!], with: .automatic)
    }
}