-1
我正在开发一个应用程序,其中一个视图是UICollectionView。我已经使集合视图完全正常工作。当用户按下其中一个收集视图单元格时,会打开一个带有照片的新视图。但是,当我尝试在集合视图上滚动时,应用程序崩溃。在UICollectionView上滚动时应用程序崩溃
这里是我的代码:
cell.imageView?.image = self.cellMapArray[indexPath.row]
我得到的错误是
fatal error: Index out of range (lldb)." And the line of code is highlighted with the error "Thread 1: EXC_BAD_INSTRUCTION (code=EXC_l386_INVOP, subcode=0x0)
任何帮助将是:
let cellMapArray = [UIImage(named: "pomona.jpg"), UIImage(named: "Pitzer.jpg"), UIImage(named: "Mudd.jpg"), UIImage(named: "Scripps.jpg"), UIImage(named: "CMC1.jpg")]
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return self.schoolNames.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
cell.imageView?.image = self.cellMapArray[indexPath.row]
cell.imageTitle?.text = self.schoolNames[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
self.performSegue(withIdentifier: "showImage", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == "showImage"
{
let indexPaths = self.collectionView!.indexPathsForSelectedItems!
let indexPath = indexPaths[0] as IndexPath
let vc = segue.destination as! NewViewController
vc.image = self.mapArray[indexPath.row]!
vc.title = self.schoolNames[indexPath.row]
}
}
并线时,应用程序崩溃是高亮不胜感激!
在一个元件中使用多个数组作为数据源是容易出错。发生错误如果'schoolNames'和'cellMapArray'中的项数不相等。使用自定义类或结构来避免这种错误。 – vadian