错误在夫特

问题描述:

浆纱UICollectionViewCell

我试图根据内错误在夫特

func collectionView(collectionView: UICollectionView, layout collectionViewLayout:UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
    var size = CGSize()  
    var cell = collectionView.dequeueReusableCellWithReuseIdentifier("lessonCell", forIndexPath: indexPath) as UICollectionViewCell 
    var label: UILabel = cell.viewWithTag(300) as UILabel 
    var labelSize = label.frame.size 
    size = labelSize 
    return size 
} 

包含当运行代码,应用程序崩溃与错误“的标签的文本的长度,以调整每个集合视图小区的大小流布局中不支持负值或零大小。'但是,当我通过时,发现崩溃发生在初始化单元变量之前,甚至在确定大小之前。为什么初始化我的单元格变量会抛出这种类型的错误?

+0

您有没有找到解决方案?有完全相同的问题。 :( – SeeMeCode 2014-12-11 20:28:54

+0

@SeeMeCode不知道你是否仍然需要它,但我在下面发布我的解决方案。 – barrt051 2014-12-16 00:42:14

我发现我的问题。我实际上使用的是collectionView.dequeueReusableCellWithReuseIdentifier(),它只能与“cellForItemAtIndexPath”委托方法一起使用。以下代码适用于我:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
    var size = CGSize(width: 0, height: 0) 
    var label = UILabel() 
    label.text = category[indexPath.row] 
    label.sizeToFit() 
    var width = label.frame.width 
    size = CGSize(width: (width+20), height: 50) 
    return size 
}