ios - 我收到此错误,此类与键 collectionView 的键值编码不兼容。 ,

标签 ios swift uicollectionview

<分区>

我有一个 UIViewController,嵌入式我有一个 UICollectionView,我还有一个名为 ChatCell 的子类

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! 

//我的错误是 ^ 我更改了名称并且没有更新 Storyboard 中的 socket !确保你有正确的导出,否则它们将不会被 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)
    }

}

最佳答案

在您的 Storyboard(或 xib)中,您指定了一个连接到名为 collectionView 的 socket ,但在您的代码中,您将其称为 collectionView2

删除错误的连接并重新指向您当前的变量名。

关于ios - 我收到此错误,此类与键 collectionView 的键值编码不兼容。 ,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42702180/

相关文章:

ios - tableview 上的持久数据(存储在 coredata 中)问题

ios - 将 google direction JSON 字符串 url 转换为 URL

ios - 如何使用 Swift 在 iOS 中为不同的通知响应播放单独的音频?

ios - iOS 是否提供了一个 api 来让 UITextField 在收到短信时自动填充

ios - 更改 UICollectionViewCell 中标签的颜色仅在一个单元格中生效?

ios - 用于访问 PHAsset 对象的 URL 信息的同步方法

ios - 在 ionic 3 中使用 FilePicker-Phonegap-iOS-Plugin 进行 ionic 服务时出现类型错误

ios - 使用 NSCoding 归档 UIImageView

ios - 如何在旋转时定义 CollectionView 的大小

swift - UICollectionViewCell Segue 仅在多项选择时触发