我有一个协议(protocol),仅用于在 View Controller 之间传递数据。
protocol CreateWorkoutDelegate {
func passedWorkout(workout : cellData)
}
出于某种原因,附加到 workoutArray
可以正常工作,但它没有显示在我的表格 View 中。
非常感谢所有帮助!
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)
}
}
最佳答案
您需要在附加数组后调用 tableView.reloadData()
,以便系统调用您的 UITableViewDataSource
委托(delegate)方法,从而使用新的填充 TableView 数据。
关于ios - 通过委托(delegate)附加的数据未显示在表上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45489733/