简体   繁体   English

如何使用CoreData从选定的集合视图单元格中获取标签文本?

[英]How to Get Label Text from Selected Collection View Cell with CoreData?

I am trying to get the text of a label that is in a collection view cell when it is pressed. 我试图获取按下时在集合视图单元格中的标签文本。 I know that doing it the regular way would involve using [indexPath.row] function however the array that the collection view is made from is with CoreData. 我知道以常规方式执行此操作会涉及使用[indexPath.row]函数,但是使用收集数据创建的数组是使用CoreData的。 When I try and use the [indexPath.row] it says: "'subscript' is unavailable: cannot subscript String with an Int, see the documentation comment for discussion." 当我尝试使用[indexPath.row]时,它说:“'下标'不可用:不能用Int下标String,请参见文档注释以进行讨论。” This is how my didSelect function currently looks like: 这是我的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)
}

I am trying to save the text in the label of the collection view cell selected to the variable 'id'. 我正在尝试将文本保存在选定为变量“ id”的集合视图单元格的标签中。 This is the declaration of the collection view: 这是集合视图的声明:

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
}

Note: Project is a CoreData entity and name is the attribute. 注意:项目是CoreData实体,名称是属性。

Does anyone know how I can save the text when cell is clicked to the 'id' variable? 有没有人知道在单元格单击“ id”变量时如何保存文本?

You should not be dequeuing a new collectionViewCell like this inside didSelectItemAt . 您不应该在didSelectItemAt内部使像这样的新collectionViewCell出队。 The func you are looking for is collectionView.cellForItem(at: indexPath) which returns the cell which was selected. 您要查找的函数是collectionView.cellForItem(at: indexPath) ,它返回选定的单元格。

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    guard let cell = collectionView.cellForItem(at: indexPath) as? CCCollectionViewCell else {
        // couldn't get the cell for some reason
        return 
    }

    id = cell.pLabel.text![Project.name] // ?

    print(id)
}

I am not sure what you are trying to do here. 我不确定您要在这里做什么。 You stated that you want to save the cell's label.text into your id variable. 您说过要将单元格的label.text保存到id变量中。 Why are you trying to subscript the text with [Project.name] ? 为什么要用[Project.name]下标文本?

Ideally, you shouldn't be exposing IBOutlet s in your cell. 理想情况下,您不应在单元格中公开IBOutlet Instead… 代替…

class CCCollectionViewCell: UICollectionViewCell {

    IBOutlet weak var pLabel: UILabel!

    var project: Project? {
        didSet {
            pLabel.text = project?.name       
        }
    }
}

and then in your view controller… 然后在您的视图控制器中...

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellP", for: indexPath) as! CCCollectionViewCell
    cell.project = projectList[indexPath.row]
    return cell
}

and… 和…

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    cell = collectionView.cellForRow(atIndexPath: indexPath) as! CCCollectionViewCell
    let project = cell.project

    print(project)
}

or… 要么…

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let project = projectList[indexPath.row]

    print(project)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从选定的集合视图单元格获取信息 - Get info from selected collection view cell 如何从位于集合视图单元格内的步进器获取选定的IndexPath? - How to get selected IndexPath from a stepper located inside a collection view cell? 如何在集合视图单元格上显示coreData(swift4) - how to display coreData on a collection view cell (swift4) 如何从iPhone的“表格”视图中获取选定单元格的单元格值 - how to get the cell value of a selected cell from Table view in iPhone 如何从DetailViewController中选定的单元格获取cell.textLabel.text? - How to get the cell.textLabel.text from the selected cell in DetailViewController? coreData未显示在集合视图单元格上(swift4) - coreData not displaying on collection view cell (swift4) 将信息从tableview传递给视图控制器而不是基于indexPathForSelectedRow,而是传递给选定单元格的标签文本? - Pass information from a tableview to a view controller based not on indexPathForSelectedRow, but on label text of cell selected? 在swift 4.0中点击收集视图单元格时更改标签的文本 - Changing the text of a label when a collection view cell is tapped in swift 4.0 标签文本增加时,如何以编程方式增加集合视图单元格的高度? - How to programmatically increase collection view cell height when label text increases? 显示从选择器视图到集合视图单元格的特定标签的选定值 - display the selected value from picker view to particularlabel of a cell of collection view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM