xcode 如何从 collectionview 重新加载部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38690065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:05:49  来源:igfitidea点击:

How to reload section from collectionview

swiftxcodecollectionview

提问by H.jenny

I tried to delete a cell from collectionviewon didSelectmethod.

我试图从collectionviewondidSelect方法中删除一个单元格。

Deleting the data is working well.

删除数据运行良好。

But I'm getting this:

但我得到了这个:

reason: 'Invalid update: invalid number of sections. The number of sections contained in the collection view after the update (1) must be equal to the number of sections contained in the collection view before the update (2), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).'

原因:'无效更新:节数无效。更新后集合视图中包含的节数(1)必须等于更新前集合视图中包含的节数(2),加上或减去插入或删除的节数(0插入,0已删除)。”

This is delete cell function:

这是删除单元格功能:

func deleteMovie(cell: MoiveCollectionViewCell) {
var indexPath: NSIndexPath = self.collectionView!.indexPathForCell(cell)!
 // remove and reload data
 self.collectionView.deleteItemsAtIndexPaths([indexPath])
 self.collectionView.reloadSections(NSIndexSet(index: indexPath.section))
}

and numberOfSectionsInCollectionViewfunc :

numberOfSectionsInCollectionView功能:

 func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    //number_per_section = 3 
    if manager.movie_List.count % number_per_section  == 0
    {
        return manager.movie_List.count / number_per_section
     }
    else {  
       return manager.movie_List.count / number_per_section  + 1
         }
}

I just want to reload number of sections. What should I add?

我只想重新加载节数。我应该添加什么?

回答by Edward

You need to update the datasource first before you make the calls to:

您需要先更新数据源,然后再调用:

 collectionView.deleteItemsAtIndexPaths([indexPath])
 collectionView.reloadSections(NSIndexSet(index: indexPath.section))

So first delete the object in your datasource model (array, dictionary etc).

因此,首先删除数据源模型(数组、字典等)中的对象。

Then you can just do this:

然后你可以这样做:

 let indexSet = IndexSet(integer: indexPath.section)
 collectionView.reloadSections(indexSet)          

回答by Sohil R. Memon

The problem is you are just removing a cellfrom the sectionand that will definitely not work.

问题是,你只是删除cellsection,这将肯定是不行的。

Once you have removed your cellyou need to notify your manager.movie_Listarraytoo. So, remove the selectedIndex data from array and then try it out!

一旦你删除了你的,cell你也需要通知你的manager.movie_Listarray。因此,从数组中删除 selectedIndex 数据,然后尝试一下!