gpt4 book ai didi

ios - 更改tableView中不同行的值

转载 作者:行者123 更新时间:2023-11-30 11:42:15 26 4
gpt4 key购买 nike

如何更改不同选定行的值?我想选择行并更改值,而对于其他未选择的行,我不想更改值。

这是所选行的示例。我或我的用户可以选择其他字符串进行更改,我需要一个通用代码。请帮忙。

我的tableView看起来像这样:

enter image description here

我的代码不起作用,但看起来像这样:

@IBOutlet weak var tableView: UITableView!

var weekdayTime: [String] = ["00:00", "01:00", "02:00", "03:00", "04:00", "05:00", "06:00"]

var weekdayPrice: [Int] = [1000, 2000, 3000, 4000, 5000, 6000, 7000]

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return weekdayTime.count

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "weekdayCell", for: indexPath) as! WeekdayPriceCell

cell.timeLabel.text = weekdayTime[indexPath.row]

cell.priceLabel.text = "\(weekdayPrice[indexPath.row])"

return cell

}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

tableView.deselectRow(at: indexPath, animated: true)

let cell = tableView.cellForRow(at: indexPath)

if (arrayOfSelectedIndexPath.contains(indexPath)) {

cell?.accessoryType = .none

} else {

cell?.accessoryType = .checkmark

}
}

@IBAction func changePrice(_ sender: Any) {

let alertController = UIAlertController(title: "Set price on selected time, message: nil, preferredStyle: .alert)

alertController.addTextField { (textField: UITextField) in

textField.keyboardType = .numberPad

textField.placeholder = "Set price"

}

alertController.addAction(UIAlertAction(title: "Cancal", style: .cancel, handler: nil))

alertController.addAction(UIAlertAction(title: "Change", style: .default, handler: { (action) in

if let price = alertController.textFields?.first?.text {
// it's not work code, i think on this need to create new code
self.weekdayPrice = [Int(price)!]

print(self.weekdayPrice)
self.tableView.reloadData()

}
}))

self.present(alertController, animated: true, completion: nil)

}

@IBAction func saveButtonPressed(_ sender: Any) {

}

我认为我的问题在这里 self.weekdayPrice = [Int(price)!] 及以下,但我不知道在哪里可以解决我的问题。

最佳答案

  1. weekdayPrice 之后添加以下行
    var weekdaySelectedFlags = Array(repeating: false, count: 7)
  2. tableView.deselectRow(at: indexPath, animated: true) 之后添加以下行在 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)方法

    weekdaySelectedFlags[indexPath.row] = !weekdaySelectedFlags[indexPath.row]
  3. 替换以下行 self.weekdayPrice = [Int(price)!]

            if let priceValue = Int(price) {
    self.weekdaySelectedFlags.enumerated().forEach() {
    if $1 == true {
    self.weekdayPrice[$0] = priceValue
    }
    }
    } else {
    print("Failed to convert the input text to Int value")
    }

            if let priceValue = Int(price) {
    for (key ,flag) in self.weekdaySelectedFlags.enumerated() {
    if flag == true {
    self.weekdayPrice[key] = priceValue
    }
    }
    } else {
    print("Failed to convert the input text to Int value")
    }

    那么你的“更改”操作将如下所示

        alertController.addAction(UIAlertAction(title: "Change", style: .default, handler: { (action) in

    if let price = alertController.textFields?.first?.text {
    // it's not work code, i think on this need to create new code
    if let priceValue = Int(price) {
    self.weekdaySelectedFlags.enumerated().forEach() {
    if $1 == true {
    self.weekdayPrice[$0] = priceValue
    }
    }
    } else {
    print("Failed to convert the input text to Int value")
    }


    print(self.weekdayPrice)
    self.tableView.reloadData()

    }
    }))

关于ios - 更改tableView中不同行的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49198178/

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