gpt4 book ai didi

ios - 选择行一次并快速执行计算

转载 作者:行者123 更新时间:2023-11-29 00:49:13 25 4
gpt4 key购买 nike

我有一个显示项目列表的 TableView ,每次选择一行时,都会添加一个复选标记并将一定数量添加到 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)
}

}
}

最佳答案

您已经创建了一个带有 bool 属性 selected 的结构,因此您可以非常轻松地使用它,以避免单元格中的金额被多次添加,并且您根本不需要使用 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?

然后代码应如下所示:

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

希望这对您有帮助。

关于ios - 选择行一次并快速执行计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38311752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com