I have a function right here in my tableViewCell and I am printing the count of the data. But when I'm looking at the log:
Table View Cell TRAINING COUNT is 0
Table View TRAINING COUNT is 3
Table View Cell TRAINING COUNT is 0
Table View TRAINING COUNT is 4
Table View Cell TRAINING COUNT is 0
Table View TRAINING COUNT is 1
the print on tableViewCell:
import UIKit
class HomeTableViewCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
@IBOutlet weak var collectionView: UICollectionView!
var trainings = [Training]()
var trainingCategories = [TrainingCategory]()
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
print("Table View Cell TRAINING COUNT is \(trainings.count)")
}
}
is performed first before the print in each row in the tableViewController:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TrainingTableCell", for: indexPath) as! HomeTableViewCell
var trainingsUnderCategory = [Training]()
for counter in 0 ..< trainings.count{
if trainingCategories[indexPath.section].id! == trainings[counter].category_id!{
trainingsUnderCategory.append(trainings[counter])
}
}
cell.backgroundColor = .white
cell.trainings = trainingsUnderCategory
print("Table View TRAINING COUNT is \(cell.trainings .count)")
return cell
}
I can't see how I can use closures here, or am I wrong? Thanks for anyone who'll answer!
awakeFromNib
is called when you create the cell by dequeueReusableCell
function call. Everything works as it should. If you'd like to configure the HomeTableViewCell
with given trainings
data you can do it by creating a function. Here is an example:
// cellForRowAt
...
cell.setTrainings(trainings)
return cell
// HomeTableViewCell
...
func setTrainings(_ trainings: [Training]) {
self.trainings = trainings
// Do configuration here
}
You don't necessarily need to create a function within the cell you can also do the configuration in cellForRowAt
function but separating the configuration would be better code wise.