ios - 使用未声明的类型 "tagCell"错误。创建那种 xib 的 UICollectionViewCell

标签 ios swift uicollectionview xib uicollectionviewcell

我正在尝试构建标签流布局并且我正在关注这个主题;

http://codentrick.com/create-a-tag-flow-layout-with-uicollectionview/

然后这段代码抛出了这个错误:Use of undeclared type 'tagCell'

var tagCell = collectionView.dequeueReusableCellWithReuseIdentifier("tagCell", forIndexPath: indexPath) as! tagCell

我创建了这些文件;一个名为 tagCell.xib 的 xib,一个名为 tagCell.swift 的文件。我把这些文件连接到 xib 的自定义类下。我不知道是什么问题,我没有遗漏地跟随主题。也许在 swift 2 中有一些关于它的变化。

我需要帮助。我寻找准备使用通过 swift 编写的库,但我没有找到。我现在必须自己编写代码。

有类(class)。

//
//  tagCell.swift
//  matchMyTag
//
//  Created by Faruk Turgut on 05/12/15.
//

import UIKit

class tagCell: UICollectionViewCell {

    @IBOutlet weak var tagTitle: UILabel!
    @IBOutlet weak var plusLabel: UILabel!

    override func awakeFromNib() {
        self.backgroundColor = UIColor(red: 0.8, green: 0.8, blue: 0.8, alpha: 1)
        self.tagTitle.textColor = UIColor(red: 0.1, green: 0.1, blue: 0.1, alpha: 1)
        self.plusLabel.textColor = UIColor(red: 0.1, green: 0.1, blue: 0.1, alpha: 1)
        self.layer.cornerRadius = 4

    }

}

//
//  skillsNeedsViewController.swift
//  matchMyTag
//
//  Created by Faruk Turgut on 05/12/15.
//  Copyright © 2015 Faruk Turgut. All rights reserved.
//

import UIKit

class skillsNeedsViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {


    @IBOutlet weak var collectionView: UICollectionView!
    let TAGS = ["Tech", "Design", "Humor", "Travel", "Music", "Writing", "Social Media", "Life", "Education", "Edtech", "Education Reform", "Photography", "Startup", "Poetry", "Women In Tech", "Female Founders", "Business", "Fiction", "Love", "Food", "Sports"]


    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.delegate = self
        collectionView.dataSource = self

        let cellNib = UINib(nibName: "tagCell", bundle: nil)
        self.collectionView.registerNib(cellNib, forCellWithReuseIdentifier: "tagCell")
        self.collectionView.backgroundColor = UIColor.clearColor()


        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return TAGS.count
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        var tagCell = collectionView.dequeueReusableCellWithReuseIdentifier("tagCell", forIndexPath: indexPath) as! tagCell

        return tagCell
    }


}

最佳答案

您需要在您的单元格上设置重用标识符。

只要打开你的 Storyboard,从 Collection View 中选择单元格并像下面这样设置它:

enter image description here

更新:

此外,您必须始终使用 CamelCase 命名您的类. 因此更改类名、.swift 文件名以及任何你想调用该类的地方,将其命名为 TagCell。断线现在应如下所示:

var tagCell = collectionView.dequeueReusableCellWithReuseIdentifier("tagCell", forIndexPath: indexPath) as! TagCell

关于ios - 使用未声明的类型 "tagCell"错误。创建那种 xib 的 UICollectionViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34111402/

相关文章:

android - Google/iOS map 的替代品

ios - 在单独的类中实现 UITextFieldDelegate

ios - 使用 Google 登录时链接器命令失败,退出代码为 1

ios - 在 ViewContainer 中隐藏一个带有按钮的 View 容器

swift - 不想下载所有对象

ios - 您好,我正在使用 Collection View 单元格,我想记录其中有多少被点击 "made true"并返回下一个 View Controller

ios - UICollectionView 单元格之间的活力效果

ios - 如何每行显示 3 个单元格 UICollectionView

ios - iPhone 图像与 Photoshop 中的同一图像 "saved-as"之间的 OCR 结果不同

ios - 自定义后退按钮出现在 UIImagePickerController 上