我正在尝试构建标签流布局并且我正在关注这个主题;
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 中选择单元格并像下面这样设置它:
更新:
此外,您必须始终使用 CamelCase 命名您的类. 因此更改类名、.swift 文件名以及任何你想调用该类的地方,将其命名为 TagCell。断线现在应如下所示:
var tagCell = collectionView.dequeueReusableCellWithReuseIdentifier("tagCell", forIndexPath: indexPath) as! TagCell
关于ios - 使用未声明的类型 "tagCell"错误。创建那种 xib 的 UICollectionViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34111402/