简体   繁体   English

Swift 3中的自定义UICollectionView不起作用

[英]Custom UICollectionView in Swift 3 not working

So I am new to swift and am trying to create a custom UiCollectionView that you can horizontally scroll through, and when tapping on a button, you can add an image from your camera roll to the array of images in the collection view. 因此,我不熟悉Swift,并且尝试创建一个可水平滚动浏览的自定义UiCollectionView,并在点击按钮时,可以将相机胶卷中的图像添加到集合视图中的图像阵列中。 This is what I have so far and I have run into some problems. 到目前为止,这就是我遇到的问题。 I have tried watching videos online but I still get errors so i don't know what I am doing wrong. 我曾尝试在线观看视频,但仍然遇到错误,因此我不知道自己在做什么错。 I have some images of apple products that I loaded into my assets folder and I will be using those images in an array for the collectionView. 我将一些苹果产品的图像加载到资产文件夹中,并将在数组中将这些图像用于collectionView。 Each image will be in one colletionViewCell. 每个图像将在一个colletionViewCell中。

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

    @IBOutlet weak var collectionView: UICollectionView!

    let imageArray = [UIImage(named: "appleWatch" ), UIImage(named: "iPhone"), UIImage(named: "iPad" ), UIImage(named: "iPod" ), UIImage(named: "macBook")]


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

        return 1

    }


    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return self.imageArray.count


    }


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

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! UICollectionViewCell


        cell.ourImage?.image = self.imageArray[indexPath.row]

        return cell

    }
}

It gives me an error here cell.ourImage?.image = self.imageArray[indexPath.row] and says that "value type UICollectionViewCell has no member 'ourImage'" Even though I named the outlet ourImage on another UICollectionViewCell swift file. 它在这里给了我一个错误cell.ourImage?.image = self.imageArray[indexPath.row]并说“值类型UICollectionViewCell没有成员'ourImage'”,即使我在另一个UICollectionViewCell swift文件上将插座ourImage命名了。 I have checked the Main.storyboard and I think I have named all of my classes correctly and have assigned them to the collectionViewCell and the identifier. 我检查了Main.storyboard,并认为我已经正确命名了所有类,并将它们分配给collectionViewCell和标识符。 I delete this line and it compiles fine, but whenever the app is run nothing shows up on the screen so there may be something wrong with my images too. 我删除了这一行,它可以正常编译,但是无论何时运行该应用程序,屏幕上都不会显示任何内容,因此我的图像也可能有问题。 Does anybody have any ideas? 有人有什么想法吗? How would you go about creating a custom UiCollection View? 您将如何创建自定义UiCollection视图? Do I have the right idea? 我有正确的主意吗?

Instead of casting the dequeued cell to UICollectionViewCell , you need to treat it as your custom UICollectionViewCell subclass. 您无需将出队的单元格转换为UICollectionViewCell ,而是需要将其视为自定义UICollectionViewCell子类。

if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "YourReuseIdentifier", for: indexPath) as? YourCustomCollectionViewCell {
     // set the cell's custom properties
}

You can also force cast using as! YourCustomCollectionViewCell 您也可以使用as! YourCustomCollectionViewCell强制转换as! YourCustomCollectionViewCell as! YourCustomCollectionViewCell , but I personally prefer not to do this. as! YourCustomCollectionViewCell ,但我个人不希望这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM