【发布时间】:2017-08-10 18:53:18
【问题描述】:
我试图在按下时获取集合视图单元格中的标签文本。我知道以常规方式执行此操作将涉及使用 [indexPath.row] 函数,但是创建集合视图的数组使用 CoreData。当我尝试使用 [indexPath.row] 时,它说:“'下标'不可用:不能用 Int 为字符串下标,请参阅文档评论进行讨论。”这是我的 didSelect 函数当前的样子:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellP", for: indexPath) as! CCCollectionViewCell
id = cell.pLabel.text![Project.name]
print(id)
}
我正在尝试将所选集合视图单元格标签中的文本保存到变量“id”。这是集合视图的声明:
var projectList : [Project] = []
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellP", for: indexPath) as! CCCollectionViewCell
let project = projectList[indexPath.row]
cell.pLabel?.text = project.name!
//cell.tag = indexPath.row
return cell
}
注意:项目是CoreData实体,名称是属性。
有谁知道当单元格被点击到“id”变量时如何保存文本?
【问题讨论】:
标签: ios swift core-data uicollectionview