【问题标题】:Why I have this error when I try to use my own CollectionViewCell class?当我尝试使用自己的 CollectionViewCell 类时,为什么会出现此错误?
【发布时间】:2018-03-05 18:03:19
【问题描述】:

1.我将UICollectionView 放入UIView。

2.我正在使用我自己的UICollectionViewCell 类。

3.错误是“类型'TimeLineViewController'不符合协议UICollectionViewDataSource”

4.如果我把funccollectionView(collectionView: UICollectionView,cellForItemAtIndexPath indexPath: NSIndexPath)的返回类型改成UICollectionViewCell,就不会报错了。

这是我的代码:

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.
}


}

而且我的单元类很简单:

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

【问题讨论】:

    标签: ios swift uicollectionview uicollectionviewcell


    【解决方案1】:

    为了使TimeLineViewController 类符合UICollectionViewDataSource 协议,您应该为cellForItemAtIndexPath 函数返回一个UICollectionViewCell。由于您已将函数签名(返回类型)更改为TimeLineCollectionViewCell,因此您会收到此错误。

    将cellForItemAtIndexPath 函数的返回类型设为UICollectionViewCell,并在函数中返回TimeLineCollectionViewCell 的出队实例。由于UICollectionViewCell 是TimeLineCollectionViewCell 的父类,因此您不会看到错误。

    注意:如有必要,您必须将返回的 UICollectionViewCell 值从 cellForItemAtIndexPath 转换为 TimeLineCollectionViewCell

    【讨论】:

    • 非常感谢,这确实帮助了我。我认为我不能将TimeLineCollectionViewCell 对象返回给返回类型为UICollectionViewCell 的函数。 UICollectionViewCell 是 TimeLineCollectionViewCell 的父类,这是有道理的。谢谢!
    • 还有一个问题:如果我使用的是 UICollectionViewController,我可以这样写:override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> TimeLineCollectionViewCell { ... }
    • 是的,你可以这样做。
    猜你喜欢
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    • 2013-03-07
    • 2021-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多