选择一次行并执行swift计算

时间:2016-07-11 16:23:08

标签: ios swift if-statement closures

我有一个显示项目列表的表格视图,每次选择一行时,都会添加一个复选标记,并将某个金额添加到var total

如果选择了另一行,它将执行与上面相同的行为,并减去取消选择行的前一个数量。取消选择的行也没有收到复选标记。

问题是如果多次选择一行,它将继续添加与所选行相对应的数量。

我想要的是只添加与行相对应的金额一次,而不管选择行的次数。

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 个答案:

答案 0 :(得分:1)

您已经创建了一个带有bool属性selected的结构体,因此您可以非常轻松地使用它来避免单元格中的数量被添加多次,并且您不需要使用根据{{​​1}},要在每次点击一个单元格时收到通知,您只需使用didDeselectRowAtIndexPath方法,如下所示:

但为了更好地使用你的结构,我建议在类型中引入数量,代码会更加干净,就像这样:

didSelectRowAtIndexPath

我们需要保存每次想要更新新单元时最后一个单元格,所以我们需要创建一个像这样的可选属性:

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?

我希望这对你有所帮助。