ios - 在表格 View 单元格中使用步进器 - 但返回 VC 的数据未正确更新
<p><p>我正在尝试创建一个配置屏幕,以便用户可以设置他或她想要进行的锻炼次数。我设法将一个表格 View 添加到一个空白 ViewController ,但存在一些风格限制 - 例如根据单元格的数量展开和折叠表格 View 。 </p>
<p>所以我想我会在 <code>tableviewcontroller</code> 中设置屏幕。现在一切顺利,直到我希望 <code>section 0</code>、<code>cell 1</code> 中的 <em>Stepper</em> 增加和减少<code>section 1</code> 中的行数。它的工作原理是数据数组(<code>workouts[]</code>)用最新的值(在 <code>cellForRowAt:indexPath</code> 方法中)更新 ViewController ,这反过来又更新了 numberOfRowsInSection使用(据说)最新的 <code>workouts.count</code>。 </p>
<p>然而,事实证明 <code>workouts.count</code> 在步进器最后更新数组之前保存了计数。 </p>
<p>现在,我尝试了多种解决方法,但仍然无法正确更新 <code>numberOfRowsInSection</code> 方法 - 在我看来,View Controller 中的 Workouts 数组没有更新为最新的值,而不是之前的设定值。</p>
<p>这是我的 ViewController :</p>
<pre><code>class AViewController: UITableViewController {
//MARK: Properties
@IBOutlet weak var aTableView: UITableView!
var sections: = ["", "Set Up Circuits"]
var workouts: = []
//MARK: Initialisation
override func viewDidLoad() {
super.viewDidLoad()
workouts =
self.tableView.delegate = self
self.tableView.dataSource = self
}
//MARK: Table Config
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.sections
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
switch section {
case 0:
return 0.1
default:
return 32.0
}
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
switch indexPath.section {
case 0:
return 60
default:
return 44
}
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var numberOfRowsInSection: Int = 0
switch section {
case 0:
numberOfRowsInSection = 1
default:
numberOfRowsInSection = workouts.count
//numberOfRowsInSection = 1
}
print(numberOfRowsInSection, "number of rows in section", section)
return numberOfRowsInSection
}
//MARK: Table Protocols
override func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.section {
//Setting layout for circuit number config
case 0:
let cell = aTableView.dequeueReusableCell(withIdentifier: "ACircuitTableViewCell") as! ACircuitTableViewCell
cell.tableView = self.aTableView
self.workouts = cell.workouts
print(workouts.count, " VC workouts array count")
return cell
//Setting layout for circuit cells
default:
let cell = aTableView.dequeueReusableCell(withIdentifier: "ATableViewCell") as! ATableViewCell
cell.aLabel.text = workouts.getName()
cell.aDetail.text = "Tap here to configure"
return cell
}
}
</code></pre>
<p>这是我的 ACircuitTableViewCell:</p>
<pre><code>class ACircuitTableViewCell: UITableViewCell {
//MARK: Properties
@IBOutlet weak var circuitNumber: UILabel!
@IBOutlet weak var circuitLabel: UILabel!
@IBOutlet weak var circStepper: UIStepper!
var tableView: UITableView!
// var updateCallback : ((_ updateList: Bool)-> Void)?
//var exercises: ?
var anArray: = []
var workouts: =
var workoutsCount: Int = 1
var indexPath: NSIndexPath = NSIndexPath(row: 0, section: 1)
//MARK: Original Functions
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
circuitNumber.text = String(workouts.count)
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
@IBAction func valueChanged(_ sender: AnyObject) {
print("Method .valueChanged activated")
//Update circuitNumber label
circuitNumber.text = String(Int(circStepper.value))
//View pre-array-update status of variables
print(workouts.count, "anArray before appending")
print(circStepper.value, "stepper value before appending")
print(circuitNumber.text, "circuitNumber")
//Update workouts array
if workouts.count < Int(circStepper.value) {
workouts.append(Exercise(name: "Circuit \(Int(circStepper.value))", timeMinutes: 2))
print(workouts.count, "after appending")
tableView.reloadData()
print(workouts.count, "new workout.count")
}
print(workouts.count, "workouts.count")
print("Method .valueChanged completed")
}
</code></pre>
<p>这是从应用加载到点击步进器 + 按钮三下到控制台的输出。</p>
<pre><code>1 number of rows in section 1
1 number of rows in section 0
1 number of rows in section 1
1 number of rows in section 0
1 number of rows in section 1
1 number of rows in section 0
1VC workouts array count
Method .valueChanged activated
1 anArray before appending
2.0 stepper value before appending
Optional("2") circuitNumber
2 after appending
1 number of rows in section 1
1 number of rows in section 0
2 new workout.count
2 workouts.count
Method .valueChanged completed
2VC workouts array count
Method .valueChanged activated
2 anArray before appending
3.0 stepper value before appending
Optional("3") circuitNumber
3 after appending
2 number of rows in section 1
1 number of rows in section 0
3 new workout.count
3 workouts.count
Method .valueChanged completed
3VC workouts array count
Method .valueChanged activated
3 anArray before appending
4.0 stepper value before appending
Optional("4") circuitNumber
4 after appending
3 number of rows in section 1
1 number of rows in section 0
4 new workout.count
4 workouts.count
Method .valueChanged completed
4VC workouts array count
Method .valueChanged activated
4 anArray before appending
5.0 stepper value before appending
Optional("5") circuitNumber
5 after appending
4 number of rows in section 1
1 number of rows in section 0
5 new workout.count
5 workouts.count
Method .valueChanged completed
5VC workouts array count
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您应该在 Controller 中处理步进回调并在那里更改 viewController 的数据源。</p>
<ol>
<li><p>在 ViewController 中处理单元格事件的最常见方法是使用委托(delegate),就像我在这里描述的那样:
<a href="https://stackoverflow.com/a/40111943/1689376" rel="noreferrer noopener nofollow">https://stackoverflow.com/a/40111943/1689376</a> </p></li>
<li><p>或者,您可以在 <code>cellForRow</code> 方法中添加带有 <code>self</code> 目标的操作:</p></li>
</ol>
<p></p>
<pre><code>func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! TableViewCell
cell.stepper?.tag = indexPath.row
cell.stepper?.addTarget(self, action: #selector(stepperValueChanged(_:)), for: .valueChanged)
return cell
}
func stepperValueChanged(_ stepper: UIStepper) {
//use stepper.tag as index to fetch an object from your dataSource
let workout = dataSource
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 在表格 View 单元格中使用步进器 - 但返回 VC 的数据未正确更新,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/40105491/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/40105491/
</a>
</p>
页:
[1]