通过代表附加的数据未显示在表格上

时间:2017-08-03 16:22:23

标签: ios swift

我有一个仅用于在视图控制器之间传递数据的协议。

protocol CreateWorkoutDelegate {
    func passedWorkout(workout : cellData)
}

由于某些原因,附加到workoutArray的工作正常,但我的桌面视图中没有显示。

非常感谢所有帮助!

import UIKit

struct cellData {
    let workoutName : String!
    let workoutSets : Int
    let workoutReps : Int
}

class MainScreen: UIViewController, UITableViewDelegate, UITableViewDataSource, CreateWorkoutDelegate {

    @IBOutlet weak var tableView: UITableView!

    var workoutArray = [cellData]()

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self

    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return workoutArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = Bundle.main.loadNibNamed("CellData", owner: self, options: nil)?.first as! CellData

        cell.workoutNameLabel.text = workoutArray[indexPath.row].workoutName
        cell.setsNumberLabel.text = String(workoutArray[indexPath.row].workoutSets)
        cell.repsNumberLabel.text = String(workoutArray[indexPath.row].workoutReps)

        return cell

    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "createWorkout" {
            let createWorkoutVC : CreateWorkout = segue.destination as! CreateWorkout
            createWorkoutVC.delegate = self
        }
    }

    func passedWorkout(workout: cellData) {
        print(workoutArray.count)
        print(workout)
        workoutArray.append(workout)
        print(workoutArray.count)
    }


    @IBAction func AddWorkout(_ sender: UIButton) {
        performSegue(withIdentifier: "createWorkout", sender: self)

    }
}

1 个答案:

答案 0 :(得分:0)

您需要在附加数组后调用tableView.reloadData(),以便系统调用UITableViewDataSource委托方法,从而使用新数据填充表格视图。