Why I have this error when I try to use my own CollectionViewCell class?

Shane Qi

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)
}
}
R P

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 from cellForItemAtIndexPath to TimeLineCollectionViewCell wherever necessary

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why do I get a compilation error when I try to have two methods with the same name and parameter type?

Why don't I have to import a class I just made to use it in my main class? (Java)

When i try to refresh my page i have this error

why i have an error when i use on update cascade?

Why am I getting an "use of undefined type" error in my code, when I have the header for it included?

Why am I getting these error messages when I try to view media files on my Android device?

Why do I get this error in my logcat when I try to post JSON data?

Why I have this error message when I try to add a new constructor to a class that extend another class?

why does mysql say I have an error in my SQL syntax, when I use the command help and status?

Why I obtain this "Unable to access jarfile" error when I try to execute my jar?

Why do I keep getting this error when I try to sequence my actions in Swift?

I'm getting an error when I try to use extern to have variables across multiple source files

Refer to Swift built-in class when I have defined my own class of the same name

When I should use my own namespaces?

Why is this error showing when I try to use javaws?

Why am I getting a NameError when I try to access an attribute in my class?

Why is there an error when I try to create a constructor for a class I have made?

I have an Internal Server Error when I try to access in my Django site

Why do I get a NoClassDefFound error when I try to save my test plan?

When I try to use firebase in my flutter app it produces an error

Why do I get an error when I try to see my Text using useState?

Why do I have this error when I try to submit a comment without adding Name and/or body?

Why does it give me this error when I try to predict my own result?

Why am I getting an error when I try to use ngrok to create a http tunnel for my local host 5000 for Twilio?

Why am i getting this error when i try to answer my own dns request?

I can change the values even when I use const word with my own array class template

why do i get an error when i try to use my models?

I have Compilation error when i use CenterAlignedTopAppBar in my code

Google Apps Script gives error "Something Went Wrong" when I try to authorize my own code