我需要更改右侧标签
上的值。但首先我需要更改前四行
,然后更改最后三行
。因此,当我选择前四行
时,我在右标签
中添加数字1200
作为四行
,然后我选择右标签
中的最后三行
,最后三行
的数字为1500
。
要更改右侧标签
中的数据,我在最后使用按钮Задать цену
,所有这些数据必须位于数组
中。
最后我想看到这个结果。
最佳答案
替换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
关于ios - 更改tableView中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49108650/