改变tableView中的值

Change the value in tableView

我需要更改 right labels 的值。但首先我需要更改 first four rows,然后更改 last three rows。所以当我 select first four rows 我为 four rows 添加 right label 数字 1200,然后我 select last three rowsright label 数字 1500last three rows

要更改 right label 中的数据,我在最后使用按钮 Задать цену,所有这些数据必须在 array.

最后想看这个结果

替换 TableView

的 DidSelect Delegate 中的行数据

table 查看 Class

import UIKit

class demoTableVC: UIViewController {

    let leftLabelArray : [String] = ["1","2","3","4","5","6","7"]
    var rightLabelArray : [String] = ["0","0","0","0","0","0","0"]

    @IBOutlet weak var demoTableView: UITableView!

    @IBAction func buttonAction(_ sender: Any)
    {
        //Get the updated Array
        print(rightLabelArray)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        demoTableView.delegate = self
        demoTableView.dataSource = self

    }
}

extension demoTableVC : UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        //Return Number of Rows
        return leftLabelArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        //Define cell 
        let cell = self.demoTableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableCellClass

        //Update Values 
        cell.leftLabel.text = leftLabelArray[indexPath.row]
        cell.rightLabel.text = rightLabelArray[indexPath.row]

        //Return cell
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {
        //Switch case statement
        switch indexPath.row
        {
        //This Handle your first 4 case of Table Cell
        case 0,1,2,3:
            rightLabelArray[indexPath.row] = "1200"
            self.demoTableView.reloadRows(at: [indexPath], with: .none)
            break;

        //This Handle your next 3 case of Table Cell
        case 4,5,6:
            rightLabelArray[indexPath.row] = "1500"
            self.demoTableView.reloadRows(at: [indexPath], with: .none)
        default:
            break;
        }
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80
    }


}

Table 单元格 class

import UIKit

class TableCellClass: UITableViewCell {

    @IBOutlet weak var leftLabel: UILabel!
    @IBOutlet weak var rightLabel: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

注意此代码是为使用 switch case 语句的 7 个静态行编写的,如果行数增加超过 7,则需要修改 switch case 语句

这将在流程中取代前 7 个索引

故事板

假设您有一个变量 devices = AnyObject

中的行数计数

现在用数组大小​​

创建数组
 var myInts = [Int](repeating: 0, count: devices.count)

在方法中,

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if indexPath.row <= 3 {
            let cell = tableView.cellForRow(at: indexPath) as? YourCell
            cell?.rightLabel.text = "1200"
            myInts[indexPath.row] = "1200"
        }
        else if (indexPath.row < devices.count) && (indexPath.row >= devices.count-3) {
            let cell = tableView.cellForRow(at: indexPath) as? YourCell
           cell?.rightLabel.text = "1500"
           myInts[indexPath.row] = "1500"
        }
}

现在点击按钮

print(myInts)