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