在Xcode中收到我不理解的错误

时间:2020-05-09 18:52:58

标签: ios swift xcode

我是SWIFT / Xcode的新手,我正在尝试创建一个集合视图,但是在运行代码时始终出现错误。该应用程序似乎可以成功构建,但是当我选择一个按钮后,该应用程序崩溃了,并且提示以下错误消息。我看到了几个带有相同错误消息的SO帖子,但是没有建议的修复程序对我的实现起作用。我在做什么错,我该如何解决?

错误消息:

***由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[UIViewController collectionView:numberOfItemsInSection:]:无法识别的选择器发送到 实例0x7fd6468165e0'

线程1:异常:“-[UIViewController collectionView:numberOfItemsInSection:]:无法识别的选择器发送到 实例0x7fd6468165e0“

我的代码:

import UIKit

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

    let videoThumbnail = [UIImage(named: "img1"), UIImage(named: "img2"), UIImage(named: "img3"), UIImage(named: "img4"), UIImage(named: "img5"), UIImage(named: "img6")]

    let videoDescription = ["Lorem ipsum 1", "Lorem ipsum 2", "Lorem ipsum 3", "Lorem ipsum 4", "Lorem ipsum 5", "Lorem ipsum 6"]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }


    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return videoThumbnail.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let VideoCell = collectionView.dequeueReusableCell(withReuseIdentifier: "VideoCell", for: indexPath) as! VideoCell

        VideoCell.videoThumbnail.image = videoThumbnail[indexPath.row]
        VideoCell.videoDescription.text = videoDescription[indexPath.row]

        return VideoCell      
    }
}

1 个答案:

答案 0 :(得分:0)

使用UICollectionViewCell(VideoCell)创建的变量名称相同。 试试这个;

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let videoCell = collectionView.dequeueReusableCell(withReuseIdentifier: "VideoCell", for: indexPath) as! VideoCell

        videoCell.videoThumbnail.image = videoThumbnail[indexPath.row]
        videoCell.videoDescription.text = videoDescription[indexPath.row]

        return videoCell      
    }