我有一个仅用于在视图控制器之间传递数据的协议。
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)
}
}
答案 0 :(得分:0)
您需要在附加数组后调用tableView.reloadData()
,以便系统调用UITableViewDataSource
委托方法,从而使用新数据填充表格视图。