简体   繁体   English

为什么在尝试使用自己的CollectionViewCell类时出现此错误?

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

1.I'm putting a UICollectionView into a UIView . 1.我将一个UICollectionView放入一个UIView

2.I'm using my own UICollectionViewCell class. 2.我正在使用自己的UICollectionViewCell类。

3.The error is "Type 'TimeLineViewController' does not conform to protocol UICollectionViewDataSource " 3.错误为“类型'TimeLineViewController'不符合协议UICollectionViewDataSource

4.If I change the return type of func collectionView(collectionView: UICollectionView , cellForItemAtIndexPath indexPath: NSIndexPath) to UICollectionViewCell , there would not be error. 4.如果我将func collectionView(collectionView: UICollectionViewcellForItemAtIndexPath indexPath: NSIndexPath)的返回类型更改为UICollectionViewCell ,则不会出现错误。

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. 为了使TimeLineViewController类符合UICollectionViewDataSource协议,应该为cellForItemAtIndexPath函数返回一个UICollectionViewCell Since you have changed the function signature (return type) to TimeLineCollectionViewCell , you are getting this error. 由于您已将函数签名(返回类型)更改为TimeLineCollectionViewCell ,因此出现了此错误。

Make the return type of your cellForItemAtIndexPath function as UICollectionViewCell and return the dequeued instance of TimeLineCollectionViewCell within your function. cellForItemAtIndexPath函数的返回类型设为UICollectionViewCell并在函数内返回TimeLineCollectionViewCell的出队实例。 Since UICollectionViewCell is the parent class of TimeLineCollectionViewCell , you will not see the error. 由于UICollectionViewCellTimeLineCollectionViewCell的父类,因此您不会看到该错误。

Note: You will have to cast the returned UICollectionViewCell value from cellForItemAtIndexPath to TimeLineCollectionViewCell wherever necessary 注意:您必须在必要时将返回的UICollectionViewCell值从cellForItemAtIndexPathTimeLineCollectionViewCell

暂无
暂无

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

相关问题 当我使用“ if let”语句解开可选的collectionViewCell时,为什么我的应用程序崩溃? - Why does my app crash when I use an “if let” statement to unwrap an optional collectionViewCell? 当我尝试在 React Native 中导入我的组件时出错 - have an error when i try import my component in react native 当我定义了自己的同名类时,请参考Swift内置类 - Refer to Swift built-in class when I have defined my own class of the same name 为什么我的类显然没有NSManagedObject属性时会出现错误? - Why do I get an error that my class doesn't have an NSManagedObject property when it clearly does? 将CollectionViewCell设置为自定义类时为EXC_I386_IBT - EXC_I386_IBT when setting CollectionViewCell to custom class 为什么当我尝试导入它时,我的课程无法找到,当它显然在侧边栏中时? - Why is it saying my class cannot be found when I try to import it, when it's clearly in the sidebar? 当我尝试运行项目并且未使用模拟器时,为什么iPhone模拟器显示“正在使用模拟器”? - Why does the iPhone simulator show “Simulator in use” when I try to run my project and the simulator is not in use? 当我尝试在Swift中对动作进行排序时,为什么仍会收到此错误? - Why do I keep getting this error when I try to sequence my actions in Swift? 为什么我必须使用自我。 引用自己的对象 - Why do I have to use self. to reference own object 我尝试在两个行星之间创建一条路径,我没有编译错误,但我不明白为什么我的模拟器上没有任何东西出现 - I try to create a path between two planets, I have no compilation error but I do not understand why there is nothing that appears on my simulator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM