将 tableView 内容保存到 NSUserDefaults Swift

Posted

技术标签:

【中文标题】将 tableView 内容保存到 NSUserDefaults Swift【英文标题】:Save tableView content to NSUserDefaults Swift 【发布时间】:2016-07-19 16:03:23 【问题描述】:

我有显示项目列表的表格视图。每选中一行,就加一个勾号,对应的金额加到var total.

如果选择了另一行,则删除前一行的复选标记,减去前一行的金额。

我正在尝试在 NSUserdefaults 中保存和检索以下信息: 选中行的复选标记(如果选择了任何行), var total 的值, indexPathForCellSelected, , 所选特定行的金额。

import UIKit
class EighthViewController: UIViewController, UITableViewDelegate,UITableViewDataSource 



struct Item 
    var name:String // name of the row
    var selected:Bool // whether is selected or not
    var amount: Int // value of the item


var frequency = [

    Item(name:"Every week",selected: false, amount: 30),
    Item(name:"Every 2 weeks",selected: false, amount: 30),
    Item(name:"Every 4 weeks",selected: false, amount: 30),
    Item(name:"Once",selected: false, amount: 40),
    Item(name:"End of tenancy cleaning", selected: false, amount: 44)
]

var total = 0
var indexPathForCellSelected: NSIndexPath?
@IBOutlet weak var tableView: UITableView!
let indexKey = "indexPathForCellSelected"
let totalKey = "total"
let amountKey = "amount"



override func viewDidLoad() 
        

override func viewDidAppear(animated: Bool) 
    super.viewDidAppear(animated)

    // retrieve indexPathForCellSelected from NSUserDefaults
    if let retrievedIndexPath = NSUserDefaults.standardUserDefaults().objectForKey(indexKey) 
        let data1 = NSUserDefaults.standardUserDefaults().objectForKey(indexKey) as? NSData
            indexPathForCellSelected = NSKeyedUnarchiver.unarchiveObjectWithData(data1!) as? NSIndexPath

            // retrieve total from NSUserDefaults
            if let totalRetrieved = NSUserDefaults.standardUserDefaults().objectForKey(totalKey) as? Int 
        total = totalRetrieved
                print(total)
        

        //
        if let itemAmount =  NSUserDefaults.standardUserDefaults().objectForKey(amountKey) as? Int 
            let myIndexpath = indexPathForCellSelected?.row
                frequency[myIndexpath!].amount = itemAmount

             **UPDATED:** tableView.cellForRowAtIndexPath(indexPathForCellSelected!)?.accessoryType = .Checkmark

        
    



func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    return frequency.count


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


// configure the cell
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
    -> UITableViewCell     
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell")
        cell?.textLabel?.text = frequency[indexPath.row].name
    return cell!


func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 

    if !frequency[indexPath.row].selected 

        // this avoid set initial value for the first time
        if let index = indexPathForCellSelected 
            // clear the previous cell
            frequency[index.row].selected = false
            tableView.cellForRowAtIndexPath(index)?.accessoryType = .None
            self.total -= frequency[index.row].amount
            print(total)
        

        // mark the new one
        frequency[indexPath.row].selected = true
        tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = .Checkmark

        indexPathForCellSelected = indexPath
        self.total += frequency[indexPath.row].amount
        print(total)

       if indexPathForCellSelected != nil  // check if there is a selected row in the table

        //save indexPathForCellSelected in NSUserDefaults
     let data = NSKeyedArchiver.archivedDataWithRootObject(indexPathForCellSelected!)
        NSUserDefaults.standardUserDefaults().setObject(data, forKey: indexKey)

            //save total in NSUserDefaults
                NSUserDefaults.standardUserDefaults().setObject(total, forKey: totalKey)

            // save amount in NSUserDefaults
                NSUserDefaults.standardUserDefaults().setObject(frequency[indexPath.row].amount, forKey: amountKey)

         // end of if indexPathForCellSelected
    
 


这是应用程序的屏幕截图:link

【问题讨论】:

@Victor Sigler。你能看看上面的代码吗? 有什么问题? @tommybananas 当我选择一行而不是添加与所选行对应的金额时,它会减去金额。 为什么这么复杂?只保存索引(行)要容易得多。它只是一个整数并且符合属性列表。 我的意思是,不要保存整个索引路径,只保存 indexPath.row 然后你就不需要归档/取消归档的舞蹈。您可以使用 (NSUserDefaults.standardUserDefaults().integerForKey / setInteger: forKey:..) 轻松保存整数 【参考方案1】:

此代码有效。如果有人有更好的实现,请将其发布在答案部分。

class EighthViewController: UIViewController, UITableViewDelegate,UITableViewDataSource 



struct Item 
    var name:String // name of the row
    var selected:Bool // whether is selected or not
    var amount: Int // value of the item


var frequency = [

    Item(name:"Every week",selected: false, amount: 30),
    Item(name:"Every 2 weeks",selected: false, amount: 30),
    Item(name:"Every 4 weeks",selected: false, amount: 30),
    Item(name:"Once",selected: false, amount: 40),
    Item(name:"End of tenancy cleaning", selected: false, amount: 44)
]

var total = 0
var indexPathForCellSelected: NSIndexPath?
@IBOutlet weak var tableView: UITableView!
let indexKey = "indexPathForCellSelected"
let totalKey = "total"
let amountKey = "amount"



override func viewDidLoad() 


override func viewDidAppear(animated: Bool) 
    super.viewDidAppear(animated)

    // retrieve indexPathForCellSelected from NSUserDefaults
if let retrievedIndexPath = NSUserDefaults.standardUserDefaults().dataForKey(indexKey) 
if let data1 = NSKeyedUnarchiver.unarchiveObjectWithData(retrievedIndexPath) as? NSIndexPath
    indexPathForCellSelected = data1





        // retrieve total from NSUserDefaults
        if let totalRetrieved = NSUserDefaults.standardUserDefaults().objectForKey(totalKey) as? Int 
            total = totalRetrieved
            print(total)
        

        //
      if let itemAmount =  NSUserDefaults.standardUserDefaults().objectForKey(amountKey) as? Int 
            let myIndexpath = indexPathForCellSelected?.row
            frequency[myIndexpath!].amount = itemAmount

            tableView.cellForRowAtIndexPath(indexPathForCellSelected!)?.accessoryType = .Checkmark

        
    



func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    return frequency.count


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


// configure the cell
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
    -> UITableViewCell     
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell")
        cell?.textLabel?.text = frequency[indexPath.row].name
        return cell!


func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 

    if !frequency[indexPath.row].selected 

        // this avoid set initial value for the first time
        if let index = indexPathForCellSelected 
            // clear the previous cell
            frequency[index.row].selected = false
                tableView.cellForRowAtIndexPath(index)?.accessoryType = .None
                    self.total -= frequency[index.row].amount
                        print(total)
        

        // mark the new one
        frequency[indexPath.row].selected = true
            tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = .Checkmark
                self.total += frequency[indexPath.row].amount
                    indexPathForCellSelected = indexPath
        print(total)

        if indexPathForCellSelected != nil  // check if there is a selected row in the table

            //save indexPathForCellSelected in NSUserDefaults
            let data = NSKeyedArchiver.archivedDataWithRootObject(indexPathForCellSelected!)
            NSUserDefaults.standardUserDefaults().setObject(data, forKey: indexKey)

            //save total in NSUserDefaults
            NSUserDefaults.standardUserDefaults().setObject(total, forKey: totalKey)

            // save amount in NSUserDefaults
    NSUserDefaults.standardUserDefaults().setObject(frequency[indexPath.row].amount, forKey: amountKey)

         // end of if indexPathForCellSelected
    
  


【讨论】:

以上是关于将 tableView 内容保存到 NSUserDefaults Swift的主要内容,如果未能解决你的问题,请参考以下文章

将复选标记保存到 tableView Swift 时出现问题

如何将 tableview 行保存到文件并重新加载到 tableview

快速将uiimageview保存到tableview?

将 TextField 内容保存在自定义动态 tableView 中

将 indexpath 值传递给 FirstViewController

在 NSUserDefaults 中保存复选标记 - 动态 TableView