【发布时间】:2016-10-09 14:29:21
【问题描述】:
我正在使用 Xcode 7 和 swift。
我正在尝试将 Collection View 原型单元格上的标签连接到我的代码。我知道要做到这一点,我必须创建 UICollectionViewCell 的子类并将其放入该类中,而不是我所做的视图控制器中。将插座添加到子类后,我立即运行应用程序,应用程序崩溃。我注意到它在错误消息的顶部显示:Interface Builder File 中的 Unknown Class nameOfSubclass。我查看了其他堆栈溢出问题,他们说在自定义类下设置模块很简单。我这样做了,但应用程序仍然崩溃,现在它说:Interface Builder 文件中的未知类_TtC13(应用程序名称/模块名称)17(nameOfSubclass)。
identity inspector of the prototype cell
identity inspector of the UICollectionView
代码
import UIKit
class SecondViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var collectionView: UICollectionView!
private let reuseIdentifier = "hourlyWeatherCell"
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return 48
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) //as! hourlyWeatherCell
// Configure the cell
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
printDate()
// Do any additional setup after loading the view.
self.collectionView.dataSource = self
self.collectionView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
class hourlyWeatherCell: UICollectionViewCell {
@IBOutlet weak var temperatureHLabel: UILabel!
}
}
【问题讨论】:
-
你在框架中有这个类吗?
-
不,我不这么认为,类是视图控制器,子类在视图控制器中。
-
把你的子类放在
UIViewController听起来很奇怪,你能提供一个代码示例吗? -
添加代码@midori
-
好的。请使用大写字母作为类名,例如
hourlyWeatherCell为HourlyWeatherCell。我只能猜测 - 这个问题可能有许多其他原因。我缺少的是您应该针对UICollectionView(stackoverflow.com/questions/24110811/…) 注册您的自定义类。如果这不起作用,请通过网络上处理集合视图中的自定义单元格的任何教程。
标签: ios swift uicollectionview xcode7 uicollectionviewcell