当在位置0的indexPath中添加新单元格时,我无法移动单元格。 当我要移动图片时,我想在索引0处添加带有动画的自定义单元格,我想移动我的图片,它应该从索引1开始,当我单击保存时,自定义单元格应该消失,图片会从索引0开始显示,并带有动画。
NewAlbumEmptyCollectionViewCell-自定义单元格
EditPhotoCollectionViewCell-默认图片单元格
@objc func saveAction(){
if rightBarButton.title == "Edit"{
rightBarButton.title = "Save"
isEditEnabled = true
UI.collectionView.reloadData()
}else{
rightBarButton.title = "Edit"
isEditEnabled = false
UI.collectionView.reloadData()
}
}
extension NewAlbumViewController: UICollectionViewDataSource, UICollectionViewDelegate{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if isEditEnabled == true{
print(photoAssets.count + 1)
return photoAssets.count
}else{
print(photoAssets.count)
return photoAssets.count
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if isEditEnabled == true{
if indexPath.section == 0 && indexPath.row == 0 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "emptyCell", for: indexPath) as! NewAlbumEmptyCollectionViewCell
return cell
}else{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "EditPhotoCollectionViewCell", for: indexPath) as! EditPhotoCollectionViewCell
cell.data = photoAssets[indexPath.row]
return cell
}
}else{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "EditPhotoCollectionViewCell", for: indexPath) as! EditPhotoCollectionViewCell
cell.data = photoAssets[indexPath.row]
return cell
}
}
}
答案 0 :(得分:2)
您错过了返回photoAssets.count + 1
的增量值的操作。进行如下更改。
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if isEditEnabled == true{
print(photoAssets.count + 1)
return photoAssets.count + 1
}else{
print(photoAssets.count)
return photoAssets.count
}
}
为获得流畅的动画效果:
@objc func saveAction(){
if rightBarButton.title == "Edit"{
rightBarButton.title = "Save"
isEditEnabled = true
UI.collectionView.insertItems(at: [IndexPath(item: 0, section: 0)])
}else{
rightBarButton.title = "Edit"
isEditEnabled = false
UI.collectionView.deleteItems(at: [IndexPath(item: 0, section: 0)])
}
}