2017-01-02 65 views
-1

我的代碼在獲取索引路徑中的單元格的行中發生了以下錯誤,我無法對其進行調試。在調用cellForItemAtIndexPath()時發生EXC_BAD_INSTRUCTION錯誤

螺紋:EXC_BAD_INSTRUCTION(代碼= EXC_1386_INVOP,子碼=爲0x0)

var cell = CollectionViewCell() 

for row in 0...self.collectionNetList.count { 
    let indexpath = NSIndexPath.init(forRow: row, inSection: 0) 

    self.cell = collectionView?.cellForItemAtIndexPath(indexpath) as! CollectionViewCell 

    // ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ CRASHES HERE ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ 

    cell.dlImage.image=UIImage(named: "ted") //Modify the custom pictures on the cell 
} 

然而,在該方法相同的指令按預期方式工作。

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
    self.cell = collectionView.cellForItemAtIndexPath(indexPath)! as! CollectionViewCell 
} 
+0

似乎沒有必要在裏面一個問題,此外,該格式似乎是非常差,目前還不清楚你要問什麼。 – Robba

+0

你連接了cell.dlImage的IBOutlet嗎?並請正確地合成你的問題......因爲它不是100%可以理解的 –

+0

cell.dlImage在自定義單元中 – user7357445

回答

0

CollectionViewCell未註冊。你需要調用UICollectionView的registerNib方法在viewDidLoad或viewWillAppear方法中註冊自定義單元格。

+0

倍率FUNC viewWillAppear中(動畫:BOOL){ self.collectionView .registerNib?(UINib(nibName: 「CollectionViewCell」,束:無),forCellWithReuseIdentifier: 「CollectionViewCell」) } 否或摺疊 self.cell =的CollectionView? .cellForItemAtIndexPath(indexpath)as! CollectionViewCell – user7357445

-1
if let cell = collectionView.cellForItemAtIndexPath(indexPath) { 
    let aCell = cell as! CollectionViewCell 
    aCell.dlImage.image=UIImage(named: "ted") 
} 

這種權利

相關問題