2015-10-26 4 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

Вы можете предотвратить аварию от случаться, благополучно разворачивания клетки. newsTypeLbl с оператором if let.

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

Я пытаюсь напечатать значение в метке и попробовал то, что вы предложили сделать это: if let label = cell.newsTypeLbl {print (label.text)} ', но все, что происходит, это, кажется, сломать в этой строке' let cell = navigationCollectionView! .cellForItemAtIndexPath (usableIndexPath) как! newsTypeCell' – Tunds

+0

что такое ошибка? –

+0

Такая же фатальная ошибка: неожиданно обнаружена нуль при развертывании Необязательное значение (lldb) – Tunds

0

Что-то, что поможет вам здесь на самом деле использовать вид сбора, передаваемый вам в этом методе делегата:

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