2015-10-26 105 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) 
... 
}