ios - 更改tableView中的值

标签 ios swift uitableview didselectrowatindexpath

我需要更改右侧标签上的值。但首先我需要更改前四行,然后更改最后三行。因此,当我选择前四行时,我在右标签中添加数字1200作为四行,然后我选择右标签中的最后三行最后三行的数字为1500

要更改右侧标签中的数据,我在最后使用按钮Задать цену,所有这些数据必须位于数组中。

example[1]

最后我想看到这个结果。

enter image description here

最佳答案

替换TableView的DidSelect Delegate中的行数据

表格 View 类

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

Storyboard

enter image description here

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

相关文章:

ios - NSManagedObject 子类的 Xcode 9.3 自动完成功能不起作用?

ios - UIImageView 约束为 1 :1 aspect ratio and 1:1 scaled to original?

ios - 为应用程序添加试用期。我应该在 Swift (iOS) 中使用 Timer 还是 DispatchQueue?

ios - 如何使用导航将字符串从一个 tableViewController 传递到另一个 tableViewController

ios - 以编程方式创建静态 TableView/单元格

ios - 如何使用 Xcode 在导航栏中添加一个共享按钮的 Action

ios - 设置 Navbar hidden = false 不起作用

ios - iOS换行更均匀吗?

ios - 如何使用自动布局将 UILabel 设置为 UICell 宽度的百分比

ios - @导入 "Unexpected ' @' in program"