2017-03-25 30 views
0
// 
    // 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'即使我添加方法UICollectionViewDataSource它给了我一个错误

我的问题是一个上面相似的。我尝试和谷歌搜索,但无法找到如何解决我的代码中的错误。尽管我已经添加了方法,但它说不符合协议

回答

0

两个问题:

  • 的数据源的方法,这两个签名是错误的斯威夫特3.正确的签名是:

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int 
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    
  • 正如Roope的答案已经提到的数据源方法里面的必须是

+0

谢谢。我是新来的迅速,因此我犯了愚蠢的错误,并试图学习。 – HellofromLearner

1

这些功能在您的班级之外。因此,他们不是所说课程的方法。将它们移到里面。

相关问题