在 .xib 中的 UICollectionViewCell 中使用 IBOutlet 时出现 NSUnknownKeyException
NSUnknownKeyException when using an IBOutlet in UICollectionViewCell in .xib
我一直在尝试使用来自 xib 的 IBOutlet
向自定义 UICollectionViewCell
class 添加标签,该标签将用作 UICollectionView
我在另一个 xib 中有,但是当我添加插座时,我在我创建的标签引用上得到一个 NSUnknownKeyException
,没有插座正确引用单元格负载的内容,但我希望能够操作单元格内的标签。
这是我的 parent xib class:
class Calendar : UIView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
{
let nibName: String = "Calendar"
let calendarDayNibName: String = "CalendarDay"
let calendarReusableCellName: String = "CalendarCell"
let calendarDaysLimit: Int = 35
var contentView: UIView?
@IBOutlet var sundayLabel: UILabel!
@IBOutlet var mondayLabel: UILabel!
@IBOutlet var tuesdayLabel: UILabel!
@IBOutlet var wednesdayLabel: UILabel!
@IBOutlet var thursdayLabel: UILabel!
@IBOutlet var fridayLabel: UILabel!
@IBOutlet var saturdayLabel: UILabel!
@IBOutlet var calendarDays: UICollectionView!
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
guard let view = self.loadViewFromNib() else { return }
view.frame = self.bounds
self.addSubview(view)
contentView = view
contentView?.isUserInteractionEnabled = true
}
override func awakeFromNib()
{
super.awakeFromNib()
calendarDays.register(UINib(nibName: calendarDayNibName, bundle: nil), forCellWithReuseIdentifier:calendarReusableCellName)
calendarDays.dataSource = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return calendarDaysLimit;
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: calendarReusableCellName, for: indexPath)
configureCell(cell: cell)
return cell
}
private func configureCell(cell: UICollectionViewCell)
{
//does nothing right now, placeholder for if configuring cell on
//collection view load
}
private func loadViewFromNib() -> UIView? {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: nibName, bundle: bundle)
self.isUserInteractionEnabled = true
return nib.instantiate(withOwner: self, options: nil).first as? UIView
}
public func loadCalendarDays(month: Int)
{
//todo: write day loading logic here
}
}
这里是 child xib class (UICollectionViewCell
):
class CalendarDay: UICollectionViewCell
{
@IBOutlet weak var dayLabel: UILabel!
}
如果有帮助的话,这是我的整体项目:https://github.com/CharlemagneVI/practice-calendar
您设置的 classes 和 IBOutlet 连接错误...好吧,不太正确...
在CalendarDay.xib
中,File's Owner
的class应该是默认的NSObject
:
它应该没有有任何联系:
单元格对象本身的class应该是CalendarDay
:
和是您建立联系的地方:
应该这样做:)
我一直在尝试使用来自 xib 的 IBOutlet
向自定义 UICollectionViewCell
class 添加标签,该标签将用作 UICollectionView
我在另一个 xib 中有,但是当我添加插座时,我在我创建的标签引用上得到一个 NSUnknownKeyException
,没有插座正确引用单元格负载的内容,但我希望能够操作单元格内的标签。
这是我的 parent xib class:
class Calendar : UIView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
{
let nibName: String = "Calendar"
let calendarDayNibName: String = "CalendarDay"
let calendarReusableCellName: String = "CalendarCell"
let calendarDaysLimit: Int = 35
var contentView: UIView?
@IBOutlet var sundayLabel: UILabel!
@IBOutlet var mondayLabel: UILabel!
@IBOutlet var tuesdayLabel: UILabel!
@IBOutlet var wednesdayLabel: UILabel!
@IBOutlet var thursdayLabel: UILabel!
@IBOutlet var fridayLabel: UILabel!
@IBOutlet var saturdayLabel: UILabel!
@IBOutlet var calendarDays: UICollectionView!
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
guard let view = self.loadViewFromNib() else { return }
view.frame = self.bounds
self.addSubview(view)
contentView = view
contentView?.isUserInteractionEnabled = true
}
override func awakeFromNib()
{
super.awakeFromNib()
calendarDays.register(UINib(nibName: calendarDayNibName, bundle: nil), forCellWithReuseIdentifier:calendarReusableCellName)
calendarDays.dataSource = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return calendarDaysLimit;
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: calendarReusableCellName, for: indexPath)
configureCell(cell: cell)
return cell
}
private func configureCell(cell: UICollectionViewCell)
{
//does nothing right now, placeholder for if configuring cell on
//collection view load
}
private func loadViewFromNib() -> UIView? {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: nibName, bundle: bundle)
self.isUserInteractionEnabled = true
return nib.instantiate(withOwner: self, options: nil).first as? UIView
}
public func loadCalendarDays(month: Int)
{
//todo: write day loading logic here
}
}
这里是 child xib class (UICollectionViewCell
):
class CalendarDay: UICollectionViewCell
{
@IBOutlet weak var dayLabel: UILabel!
}
如果有帮助的话,这是我的整体项目:https://github.com/CharlemagneVI/practice-calendar
您设置的 classes 和 IBOutlet 连接错误...好吧,不太正确...
在CalendarDay.xib
中,File's Owner
的class应该是默认的NSObject
:
它应该没有有任何联系:
单元格对象本身的class应该是CalendarDay
:
和是您建立联系的地方:
应该这样做:)