1.I'm putting a UICollectionView
into a UIView
.
2.I'm using my own UICollectionViewCell
class.
3.The error is "Type 'TimeLineViewController' does not conform to protocol UICollectionViewDataSource
"
4.If I change the return type of func collectionView(collectionView: UICollectionView
, cellForItemAtIndexPath indexPath: NSIndexPath)
to UICollectionViewCell
, there would not be error.
Here are my codes:
import UIKit
class TimeLineViewController: UIViewController, UICollectionViewDataSource,UICollectionViewDelegate {
@IBOutlet weak var TimeLineColleciontView: UICollectionView!
// TODO TODO set cell size permeantly
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width-20,
height: (collectionView.frame.width-20) * 1.2 )
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> TimeLineCollectionViewCell {
let id = "TimeLineCell"
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(id, forIndexPath: indexPath) as! TimeLineCollectionViewCell
return cell
}
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent
}
override func viewDidLoad() {
super.viewDidLoad()
self.TimeLineColleciontView.backgroundColor = UIColor(white: 0, alpha: 0)
TimeLineColleciontView.dataSource = self
TimeLineColleciontView.delegate = self
// 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.
}
}
And my cell class is simple:
import UIKit
class TimeLineCollectionViewCell: UICollectionViewCell {
var cover : UIImageView = UIImageView()
var date : UILabel = UILabel()
override func awakeFromNib() {
cover.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.width)
date.frame = CGRect(x: 0, y: 0, width: 300, height: 300)
self.insertSubview(cover, atIndex: 0)
self.insertSubview(date, atIndex: 2)
}
}
For TimeLineViewController
class to conform to UICollectionViewDataSource
protocol, you are supposed to return a UICollectionViewCell
for the cellForItemAtIndexPath
function. Since you have changed the function signature (return type) to TimeLineCollectionViewCell
, you are getting this error.
Make the return type of your cellForItemAtIndexPath
function as UICollectionViewCell
and return the dequeued instance of TimeLineCollectionViewCell
within your function. Since UICollectionViewCell
is the parent class of TimeLineCollectionViewCell
, you will not see the error.
Note: You will have to cast the returned
UICollectionViewCell
value fromcellForItemAtIndexPath
toTimeLineCollectionViewCell
wherever necessary
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments