【问题标题】:How to Get Label Text from Selected Collection View Cell with CoreData?如何使用 CoreData 从选定的集合视图单元格中获取标签文本?
【发布时间】: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


    【解决方案1】:

    您不应该像这样在didSelectItemAt 中将新的collectionViewCell 出列。您要查找的函数是 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)
    }
    

    我不确定您要在这里做什么。您声明要将单元格的 label.text 保存到您的 id 变量中。你为什么要在文本下标[Project.name]

    【讨论】:

      【解决方案2】:

      理想情况下,您不应该在您的牢房中暴露IBOutlets。而是……

      class CCCollectionViewCell: UICollectionViewCell {
      
          IBOutlet weak var pLabel: UILabel!
      
          var project: Project? {
              didSet {
                  pLabel.text = project?.name       
              }
          }
      }
      

      然后在你的视图控制器中……

      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
      }
      

      还有……

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

      或者……

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多