在表格视图单元格中使用步进器 - 但返回VC的数据未正确更新

时间:2016-10-18 10:15:03

标签: ios arrays uitableview uistepper

我正在尝试创建配置屏幕,以便用户可以设置他或她想要做的锻炼次数。我设法将一个tableview添加到空白视图控制器,但有一些风格限制 - 例如根据单元格数扩展和折叠tableview。

所以我想我会在tableviewcontroller中设置屏幕。现在一切顺利,直到我想要section 0cell 1中的 Stepper 来增加和减少行数section 1。它的工作范围是数据数组(workouts[])使用最新值(在cellForRowAt:indexPath方法中)更新视图控制器,而后者又使用(假设的)最新{{1}更新numberOfRowsInSection }。

然而,事实证明workouts.count在最后由步进器更新数组之前保持计数。

现在,我已经尝试了一些解决方法,但我仍然无法正确更新workouts.count方法 - 在我看来,在视图控制器中没有更新Workouts阵列使用最新值,而不是之前的设定值。

这是我的视图控制器:

numberOfRowsInSection

这是我的ACircuitTableViewCell:

class AViewController: UITableViewController {

//MARK: Properties
@IBOutlet weak var aTableView: UITableView!

var sections: [String] = ["", "Set Up Circuits"]
var workouts: [Exercise] = []

//MARK: Initialisation
override func viewDidLoad() {
    super.viewDidLoad()
    workouts = [Exercise(name: "Circuit 1", timeMinutes: 2)]
    self.tableView.delegate = self
    self.tableView.dataSource = self
}

//MARK: Table Config
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return self.sections[section]
}

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[indexPath.row].getName()
        cell.aDetail.text = "Tap here to configure"
        return cell

    }
}

这是从应用加载时到控制台的输出,到步进器+按钮上的三次点击。

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: [Exercise]?
var anArray: [Exercise] = []
var workouts: [Exercise] = [Exercise(name: "Circuit 1", timeMinutes: 2)]

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")

}

1 个答案:

答案 0 :(得分:0)

您应该在控制器中处理步进器回调并在那里更改viewController的dataSource。

  1. 在视图控制器中处理单元格事件的最常用方法是使用委托,就像我在这里描述的那样: https://stackoverflow.com/a/40111943/1689376

  2. 或者,您可以self方法添加cellForRow目标权限的操作:

  3. 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[stepper.tag]
    }