Ios 我收到此错误,该类不符合key collectionView的键值编码,
我有一个UIViewController,我有一个UICollectionView,我还有一个叫做ChatCell的子类Ios 我收到此错误,该类不符合key collectionView的键值编码,,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我有一个UIViewController,我有一个UICollectionView,我还有一个叫做ChatCell的子类 ChatCell Subclass: class ChatCell: UICollectionViewCell { @IBOutlet weak var chatLabel: UILabel! @IBOutlet weak var chatImage: UIImageView! } class ChatRoom: UIViewController, UI
ChatCell Subclass:
class ChatCell: UICollectionViewCell {
@IBOutlet weak var chatLabel: UILabel!
@IBOutlet weak var chatImage: UIImageView!
}
class ChatRoom: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet weak var collectionView2: UICollectionView!
//我的错误是^我更改了名称,并且没有更新故事板中的插座!确保您有正确的插座,否则xcode将无法识别它们
var secondClass : ChatCell?
struct Object {
var image: UIImage!
var title: String!
}
// Properties
var object: Object?
var objects: [Object] = []
var picker: UIImagePickerController!
// Do any additional setup after loading the view, typically from a nib.
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidLoad() {
super.viewDidLoad()
picker = UIImagePickerController()
picker?.allowsEditing = false
picker?.delegate = self
picker?.sourceType = .photoLibrary
// NavigationBar Characteristics
}
override func viewDidAppear(_ animated: Bool) {
self.navigationController?.isNavigationBarHidden = false
self.navigationController?.navigationBar.barTintColor = UIColor.black
self.navigationController?.navigationBar.isTranslucent = false
// self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(handleLogout))
self.navigationItem.leftBarButtonItem?.tintColor = UIColor.green
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "+", style: .plain, target: self, action: #selector(didSelectCreateButton))
self.navigationItem.rightBarButtonItem?.tintColor = UIColor.green
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as UICollectionViewCell
let object = objects[indexPath.row]
secondClass?.chatLabel.text = object.title ?? ""
secondClass?.chatImage.image = object.image ?? UIImage()
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return objects.count
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
switch info[UIImagePickerControllerOriginalImage] as? UIImage {
case let .some(image):
object?.image = image
default:
break
}
picker.dismiss(animated: true) {
self.showCellTitleAlert()
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
object = nil
dismiss(animated: true) {
self.collectionView2!.reloadData()
}
}
// Alerts
func showCellTitleAlert() {
let alert = UIAlertController(title: "Cell Title", message: nil, preferredStyle: .alert)
alert.addTextField { $0.placeholder = "Title" }
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel) { _ in
self.object = nil
})
alert.addAction(UIAlertAction(title: "Save", style: .default) { _ in
self.object?.title = (alert.textFields?.first.flatMap { $0.text })!
self.object.flatMap { self.objects.append($0) }
self.collectionView2?.reloadData()
})
present(alert, animated: true, completion: nil)
}
// Create new Cell
////////?//////////////
@IBAction func didSelectCreateButton() {
object = Object()
present(picker, animated: true, completion: nil)
}
}
在故事板(或xib)中,您指定了一个到名为collectionView
的插座的连接,但在代码中,您将其称为collectionView 2
删除错误的连接并重新进行操作以指向当前变量名。在情节提要(或xib)中,您已指定了一个到名为collectionView
的插座的连接,但在代码中,您将其称为collectionView 2
删除错误的连接,然后重新连接以指向当前变量名。确保所有
@IBOutlet
都正确连接到故事板/xib。然后清除派生数据并清除项目。这应该可以解决您的问题。请确保所有@IBOutlet
都正确连接到故事板/xib。然后清除派生数据并清除项目。这应该可以解决你的问题。谢谢你的帮助,我只是想知道为什么这是一个坏的连接?它起作用了!谢谢你我现在明白我做错了什么。谢谢你的帮助,我只是想知道为什么这是一个坏的连接?它工作了!谢谢你,我现在明白我做错了什么。