更改tableView中的值

问题描述 投票:0回答:2

我需要改变right labels的价值。但首先我需要改变first four rows然后改变last three rows。因此,当我选择first four rows时,我为right label添加1200数字four rows,然后我选择last three rows right label中的1500last three rows

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

最后我想看到这个结果。

enter image description here

ios swift uitableview didselectrowatindexpath
2个回答
0
投票

替换TableView的DidSelect委托中的行数据

表视图类

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
    }


}

表格单元格类

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个索引

故事板

enter image description here


0
投票

假设您计算变量devices = AnyObject中的行数

现在使用arraysize创建数组

 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)
© www.soinside.com 2019 - 2024. All rights reserved.