【问题标题】:How to reload section from collectionview如何从collectionview重新加载部分
【发布时间】:2016-08-01 02:05:04
【问题描述】:

我尝试使用didSelect 方法从collectionview 中删除一个单元格。

删除数据效果很好。

但我得到了这个:

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

这是删除单元格功能:

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))
}

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
         }
}

我只想重新加载部分的数量。我应该添加什么?

【问题讨论】:

    标签: swift xcode collectionview


    【解决方案1】:

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

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

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

    那么你可以这样做:

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

    【讨论】:

      【解决方案2】:

      问题是您只是从section 中删除cell,这绝对行不通。

      删除您的cell 后,您还需要通知您的manager.movie_List array。因此,从数组中删除 selectedIndex 数据,然后尝试一下!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-06-30
        • 2012-07-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多