更改tableView中的值

时间:2018-03-05 10:46:17

标签: ios swift uitableview didselectrowatindexpath

我需要更改right labels的值。但首先我需要更改first four rows,然后更改last three rows。因此,当我选择first four rows时,我为right label添加了1200个号码four rows,然后我在last three rowsright label中选择了1500 } last three rows

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

example[1]

最后我想看到这个结果。

enter image description here

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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)