【发布时间】:2020-01-25 02:45:32
【问题描述】:
我有一个名为SwipingController 的UICollectionViewController,它创建了一个TeamCell 并给它一个“波士顿凯尔特人队”的teamName
class SwipingController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.backgroundColor = .white
collectionView?.register(TeamCell.self, forCellWithReuseIdentifier: "cellId")
collectionView?.isPagingEnabled = true
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 2
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! TeamCell
cell.teamName = "Boston Celtics"
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: view.frame.height)
}
}
但是,当我运行代码时,teamName 仍然打印为空字符串。
class TeamCell: UICollectionViewCell {
var teamName: String = ""
override init(frame: CGRect) {
super.init(frame: frame)
print(teamName) //this prints nothing
}
}
【问题讨论】:
-
你现在黑屏了?
-
@YogeshPatel 不,我的 CollectionViewCells 出现在屏幕上。问题是,TeamCell 中的
teamName是一个空字符串,即使我在SwipingController()中将其设置为“波士顿凯尔特人队” -
请为您的标签分配约束,然后它会显示给您。
标签: ios swift uicollectionview uicollectionviewcell