swift - 即使我为 UICollectionViewDataSource 添加了方法,它也会给我一个错误

标签 swift uicollectionview

    //
    //  ViewController.swift
    //  CollectionViewTry 
    //
   //  Created by Shivam Agrawal on 25/03/17.
//  Copyright © 2017 Shivam Agrawal. All rights reserved.
//

import UIKit

class ViewController: UIViewController, UICollectionViewDelegateFlowLayout,UICollectionViewDataSource {
    var collectionView: UICollectionView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
        layout.itemSize = CGSize(width: 90, height: 120)

        collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
        collectionView.backgroundColor = UIColor.white
        self.view.addSubview(collectionView)






        /*   print(BigBox.count)
         for i in 0...80{
         print("\(BigBox[i].identifier) \(BigBox[i].ColID) \(BigBox[i].RowID) \(BigBox[i].SmallBox) \(BigBox[i].sbCount)")
         }*/
        // 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.
    }


}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 14
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath as IndexPath)
    cell.backgroundColor = UIColor.orange
    return cell
}

ios 8.1: Type 'ViewController' does not conform to protocol 'UICollectionViewDataSource'

我的问题和上面的类似。我试过并用谷歌搜索,但找不到如何修复我的代码中的错误。尽管我添加了方法,但它说不符合协议(protocol)

最佳答案

函数在你的类之外。因此,它们不是所述类的方法。将它们移入其中。

关于swift - 即使我为 UICollectionViewDataSource 添加了方法,它也会给我一个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43019572/

相关文章:

ios - Autolayout 提示 UIImageView 的前导/尾随空间

ios - 没有 DispatchQueue.main.async 的意外布局行为

ios - UICollectionView 滚动时出现故障

ios - ios 视频中图像位置的变化

ios - 来自扩展的声明在 Swift 4 中还不能被覆盖

ios - 如何创建仅具有左上角和左下角半径的圆形 UIButton

ios - 如何快速调整单元格大小以适应尺寸类别

ios - 为什么最小项间间距不起作用?

ios - 更改 UICollectionViewCell 高度以适合 UILabel

ios - 调用 "didSelectItemAt"时从单元格内调用函数