【发布时间】:2019-12-11 21:30:34
【问题描述】:
我有一个带有 4 个 UICollectionViewCells 的 UICollectionView。每个单元格都包含一个 UITableView,其中包含不同数量的单元格。
我有一个数组,我需要从中将一个值(取决于 indexPath)传递到单元格中。我的问题是,当我尝试这样做时,它总是给我 nil。
所以例如我有这个数组:
let arrayOfStrings = ["test1", "test2", "test3", "test4"]
我的集合视图的 cellForItemAt 方法中有这段代码:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "testCollectionViewCell", for: indexPath) as! TestCollectionViewCell
cell.myString = arrayOfStrings[indexPath.row]
cell.label.text = "This text is set."
return cell
}
单元格的标签设置正确,但是当我尝试在单元格的表格视图的 cellForRowAt 方法中打印 myString 时,它总是返回 nil。 我需要这个字符串,因为我打算根据传入的字符串来操作表格视图。
那么为什么会发生这种情况以及可以做些什么呢?
【问题讨论】:
-
你检查 indexPath.row 的值了吗?
标签: ios swift uitableview uicollectionview