更改tableView中不同行的值

时间:2018-03-09 16:30:58

标签: ios swift uitableview

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

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

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

答案 0 :(得分:0)

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