我有表格视图,显示项目列表。每次选择一行时,都会添加一个复选标记,相应的金额会添加到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
答案 0 :(得分:0)
此代码有效。如果有人有更好的实施,请在答案部分发布。
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
}
}
}