0
我面临着CollectionView的一个问题,我想看到一个图像集合,并有一个添加按钮作为最后一个项目,所以首先我创建了一个数组NSData的作为我要去这些图像保存到核心数据 var photoArray = [NSData]()
后来我实现UICollectionViewDataSource以编程方式向CollectionView添加新项目,通过最后一个项目
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.item == photoArray.count + 1 {
present(imagePicker, animated: true, completion: nil)
}
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return photoArray.count + 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "photoItem", for: indexPath) as? PhotoCell else {return UICollectionViewCell()}
cell.btn.tag = indexPath.item
if indexPath.item == photoArray.count + 1 {
cell.thumImage.image = UIImage(named: "add_button")
cell.btn.isHidden = true
print("first")
return cell
} else {
let img = photoArray[indexPath.item]
cell.configureAddingImage(img: img)
print("second")
return cell
}
}
其实我面临着与“cellForItemAt indexPath”喜欢“致命错误的问题:指数超出范围“我试图让阵列看起来像var photoArray: [NSData]!
但它造成了其他问题,请给我任何建议或帮助,谢谢!
这解决了这个问题,谢谢! – Just66