2016-07-19 13 views
0

アイテムのリストを表示するテーブルビューがあります。行が選択されるたびに、チェックマークが追加され、対応する金額が加算されます。var total.テーブルビューのコンテンツをNSUserDefaultsに保存するSwift

別の行が選択されている場合、前の行のチェックマークが削除され、前の行の金額が減算されます。私は保存してまで取得しようとしています

/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

@ Victor Sigler。上記のコードをご覧ください。 – bibscy

+0

問題は何ですか? – tommybananas

+0

@tommybananas選択した行に対応する量を加算する代わりに行を選択すると、その量が減算されます。 – bibscy

答えて

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

} 
関連する問題