2015-10-26 70 views
0

我试图访问集合视图中的单元格,然后将字体重量更改为粗体,当用户点击单元格,但我似乎有一些问题。当试图访问我的集合视图网点时,我收到此错误。无法访问uicollectionview单元

fatal error: unexpectedly found nil while unwrapping an Optional value 
(lldb) 

但我似乎并不明白这是因为该单元格的标识符是正确的,而且类名也是正确的。

与小区选择

protocol InfiniteCollectionViewDelegate 
{ 
    func didSelectCellAtIndexPath(collectionView: UICollectionView, unmodifiedIndexPath: NSIndexPath, usableIndexPath: NSIndexPath) 
} 

处理使用协议

extension InfiniteCollectionView: UICollectionViewDelegate 
{ 
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) 
    { 
     infiniteDelegate?.didSelectCellAtIndexPath(self, unmodifiedIndexPath: indexPath, usableIndexPath: NSIndexPath(forRow: getCorrectedIndex(indexPath.row - indexOffset), inSection: 0)) 
    } 
} 

的方式我设立集合视图

func cellForItemAtIndexPath(collectionView: UICollectionView, dequeueIndexPath: NSIndexPath, usableIndexPath: NSIndexPath) -> UICollectionViewCell 
{ 
    let cell = navigationCollectionView.dequeueReusableCellWithReuseIdentifier("newsCell", forIndexPath: dequeueIndexPath) as! newsTypeCell 
    cell.newsTypeLbl.text = cellItems[usableIndexPath.row] 
    return cell 

} 
协议

取得集合观察室出口

func didSelectCellAtIndexPath(collectionView: UICollectionView, unmodifiedIndexPath: NSIndexPath, usableIndexPath: NSIndexPath) 
{ 

    navigationCollectionView.scrollToItemAtIndexPath(unmodifiedIndexPath, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: true) 

    let cell = navigationCollectionView!.cellForItemAtIndexPath(usableIndexPath) as! newsTypeCell 

    print(cell.newsTypeLbl.text) 

} 

回答

0

可以防止死机从安全展开细胞发生的动作。带有if let语句的newsTypeLbl。

if let label = cell.newsTypeLbl{ 
    label.text = cellItems[usableIndexPath.row] 
} 
+0

我想在标签打印的价值,并尝试什么你建议通过这样做'if let label = cell.newsTypeLbl {print(label.text)}'但是发生的所有事情似乎是在这一行中断开let cell = navigationCollectionView!.cellForItemAtIndexPath(usableIndexPath)as! newsTypeCell' – Tunds

+0

错误在说什么? –

+0

同样的致命错误:意外地发现零,同时展开一个可选值 (lldb) – Tunds

0

东西,这将帮助你在这里是实际使用被传递给你在委托方法收集的观点:

func didSelectCellAtIndexPath(collectionView: UICollectionView, unmodifiedIndexPath: NSIndexPath, usableIndexPath: NSIndexPath) 
{ 
collectionView.scrollToItemAtIndexPath(unmodifiedIndexPath, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: true) 
... 
}