I've got a tableViewCell that I need to have an array passed into the tableViewCell but not just passed into a text label or something like that. I'll let my code show.
My TableViewController:
let subjectsDict = ["Spanish": ["Lesson 1", "Lesson 2"], "Math":["Problem set 1", "Problem set 2"], "Science": ["Lab"]]
let subjectArray = ["Spanish", "Math", "Science"]
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "subjectCell", for: indexPath) as? SubjectTableViewCell else {
return UITableViewCell()
}
cell.subjectList = subjectsDict[subjectArray[indexPath.row]]
return cell
}
And my tableViewCell looks like this.
class subjectTableViewCell: UITableViewCell {
var subjectList: [String] = []
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style , reuseIdentifier: reuseIdentifier)
setUpTable()
}
required init?(coder decoder: NSCoder) {
super.init(coder: decoder)
}
override func awakeFromNib() {
super.awakeFromNib()
setUpTable()
}
func setUpTable() {
print(subjectList)
}
//other code for creating the cell
}
But when I print the subjectList from the subjectTableViewCell it prints none
subjectList.subjectListis empty because thesetUpTableis called when the cell is init or loaded, so before thesubjectListhas been set.