2016-07-11 15 views
0

アイテムのリストを表示するテーブルビューがあり、行が選択されるたびにチェックマークが追加され、一定の金額がvar totalに追加されます。一度行を選択して計算を迅速に実行

別の行が選択されている場合は、上記と同じ動作を実行し、選択解除された行の前の値が減算されます。また、選択解除された行にはチェックマークが表示されません。

行が数回選択されると、選択された行に対応する量が追加され続けるという問題があります。

私がしたいのは、行が選択された回数に関係なく、行に対応する量を1回だけ追加することです。

class EighthViewController: UIViewController, UITableViewDelegate,UITableViewDataSource { 

var total = 0 

struct Item { 
    var name:String // name of the row 
    var selected:Bool // whether is selected or not 
} 

var frequency = [ 

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

@IBOutlet weak var tableView: UITableView! 

override func viewDidLoad() { 
    super.viewDidLoad() 
} 


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) { 
    tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = .Checkmark 

    if indexPath.row == 0 { 
    self.total += 30 
     print(total) 

     } else if indexPath.row == 1 { 
     self.total += 30 
     print(total) 

     } else if indexPath.row == 2 { 
      self.total += 30 
      print(total) 

    } else if indexPath.row == 3 { 
     self.total += 40 
      print(total) 

    } else if indexPath.row == 4 { 
     self.total += 44 
      print(total) 
    } 
} 

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { 
    tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = .None 

    if indexPath.row == 0 { 
     self.total -= 30 
     print(total) 

    } else if indexPath.row == 1 { 
      self.total -= 30 // delete previous amount 
      print(total) 


     } else if indexPath.row == 2 { 
      self.total -= 30 // delete previous amount 
      print(total) 


    } else if indexPath.row == 3 { 
     self.total -= 40 // delete previous amount 
     print(total) 

    } else if indexPath.row == 4 { 
     self.total -= 44 // delete previous amount 
     print(total) 
    } 

    } 
} 

答えて

1

あなたはとてもあなたがセルに金額を避けるために、非常に簡単にそれを使用することができboolプロパティselectedを持つ構造体を作成しましたが、数回追加され、あなたは全くdidDeselectRowAtIndexPathを使用する必要はありませんセルがタップされるたびに通知するには、次のような方法を使用する必要があります

しかし、あなたの構造体のより良い使用のために、タイプ内の量を導入することを推奨し、コードはよりクリーンになりますこのように:

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 indexPathForCellSelected: NSIndexPath? 

をし、コードは次のようにする必要がありますが次のようになります。

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 
     } 

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

     indexPathForCellSelected = indexPath 
     self.total += frequency[indexPath.row].amount 
    } 
} 

これはあなたに役立ちます。

+0

ありがとうございました。あなたが提供したコードにはいくつか問題があります。最初のバージョンでは、行が選択されるたびに、その行に対応する量が 'total'に加算されます。チェックマークは消えません。第2のバージョンでは、いくつかの行が選択されている場合、その合計が「合計」に追加され、それらが選択解除され、減算されるが、前に選択された行はチェックマークを失わず、選択された。 – bibscy

+0

私が探している振る舞いは:1.行を選択 - >金額を追加し、チェックマークを追加します。 1.1行を再度選択 - >何も起こりません。 2.1。別の行を選択し、前の行のチェックマークを「なし」に設定し、前に追加した合計を減算し、新しく選択した行にチェックマークを付け、選択した行に対応する金額を加算します。ありがとうございました – bibscy

+0

@bogdanbarbulescuええと更新された答えを参照してください、あなたの質問は、今はっきりして初めて非常に明確ではなかった。私はこれがあなたに役立つことを願っています –

関連する問題