【发布时间】:2017-12-20 07:32:56
【问题描述】:
UICollectionView 数组值未加载到 cellForItemAt indexPath 我正在使用以下代码
var tableData: [String] = ["Evo X", "458", "GTR"]
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.tableData.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collections", for: indexPath) as! CollectionViewCell
let indexPath = self.tableData[indexPath.row]
print("indexpath\(indexPath)")
cell.celllabel.text = self.tableData[indexPath.row]
cell.backgroundColor = UIColor.cyan
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("You selected cell #\(indexPath.item)!")
label.text = self.tableData[indexPath.row]
}
数组值打印在
print("indexpath(indexPath)")
但没有载入
cell.celllabel.text = self.tableData[indexPath.row]
我的输出是(控制台)
print("indexpath(indexPath)")
indexpathEvo X
indexpath458
indexpathGTR
但是这一行得到了错误
cell.celllabel.text = self.tableData[indexPath.row]
我的错误是
Fatal error: Unexpectedly found nil while unwrapping an Optional value
我该如何解决这个问题?
【问题讨论】:
-
is CollectionViewCell 有一个名为 celllabel 的 textField/textView ???
-
CollectionViewCell 有标签
-
感谢您的回复
-
请尝试 indexPath.item 而不是 indexPath.row。
标签: ios uicollectionview swift4