我的tableViewCell中有一个函数,正在打印数据计数。但是当我查看日志时:
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
在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)")
}
}
首先在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
}
我在这里看不到如何使用闭包,还是我错了?感谢任何会回答的人!
答案 0 :(得分:0)
awakeFromNib
函数调用创建单元格时,会调用 dequeueReusableCell
。一切正常。如果您想使用给定的HomeTableViewCell
数据配置trainings
,可以通过创建一个函数来完成。这是一个示例:
// cellForRowAt
...
cell.setTrainings(trainings)
return cell
// HomeTableViewCell
...
func setTrainings(_ trainings: [Training]) {
self.trainings = trainings
// Do configuration here
}
您不一定需要在单元格中创建一个函数,也可以在cellForRowAt
函数中进行配置,但是将配置分开会更好。