简体   繁体   English

UICollectionView刷新了一组新数据

[英]UICollectionView refreshing with a new set of data

I am trying to delete the entire contents of the collectionView using the following function after reading the apple documentation. 阅读Apple文档后,我尝试使用以下功能删除collectionView的全部内容。

https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/CreatingCellsandViews/CreatingCellsandViews.html#//apple_ref/doc/uid/TP40012334-CH7-SW7 https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/CreatingCellsandViews/CreatingCellsandViews.html#//apple_ref/doc/uid/TP40012334-CH7-SW7

It ends up with the following error. 最终出现以下错误。

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections. 由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“无效的更新:无效的节数。 The number of sections contained in the collection view after the update (0) must be equal to the number of sections contained in the collection view before the update (5), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).' 更新后(0)包含在集合视图中的节数必须等于更新前(5)包含在集合视图中的节数,加上或减去插入或删除的节数(插入的0,插入的0删除)。”

What does this mean? 这是什么意思? Or can anyone show me a reference link with an example of how to refresh the contents of collectionView with a new set of data on clicking a button. 或者任何人都可以向我显示参考链接,并提供有关如何在单击按钮时使用新数据集刷新collectionView内容的示例。

-(void) clearCollectionViewAndDataArray
{

[self.galleryCollectionView performBatchUpdates:^{
    [displayDataArray removeAllObjects];
    NSLog(@"Is it empty---?%@",displayDataArray);
  [self.galleryCollectionView deleteItemsAtIndexPaths:[self createIndexPathArrayEntireCollectionView]];
}
completion:nil ];
}




-(NSMutableArray*) createIndexPathArrayEntireCollectionView
{

NSMutableArray* indexPathArray = [[NSMutableArray alloc] init];

for(int i=0; i < [displayDataArray count]; i++)
{

    NSMutableArray* sectionArray=[[NSMutableArray alloc] init];
    sectionArray=[displayDataArray objectAtIndex:i];
    for(int j=0; j < [sectionArray count]; j++)
    {
        NSIndexPath* newPath = [NSIndexPath indexPathForRow:j inSection:i];
        [indexPathArray addObject: newPath];
    }

}

return indexPathArray;

}

The NSMutableArray data used is: 使用的NSMutableArray数据为:

(
(
"<SACatalogs: 0x9554750>",
    "<SACatalogs: 0x953d9c0>",
    "<SACatalogs: 0x953e050>"
),
    (
    "<SACatalogs: 0x953e6e0>",
    "<SACatalogs: 0x953ed70>",
    "<SACatalogs: 0x953f420>"
),
    (
    "<SACatalogs: 0x953fab0>",
    "<SACatalogs: 0x9540160>",
    "<SACatalogs: 0x95407f0>"
),
    (
    "<SACatalogs: 0x9540ec0>",
    "<SACatalogs: 0x9541530>",
    "<SACatalogs: 0x9541be0>"
),
    (
    "<SACatalogs: 0x9542290>",
    "<SACatalogs: 0x9542940>",
    "<SACatalogs: 0x9542ff0>"
)
)

The index path used is 使用的索引路径是

"<NSIndexPath 0xa6bb9a0> 2 indexes [0, 0]",
"<NSIndexPath 0xa648bc0> 2 indexes [0, 1]",
"<NSIndexPath 0xa6dcdc0> 2 indexes [0, 2]",
"<NSIndexPath 0xa6c7d00> 2 indexes [1, 0]",
"<NSIndexPath 0xa6c91c0> 2 indexes [1, 1]",
"<NSIndexPath 0xa6f6100> 2 indexes [1, 2]",
"<NSIndexPath 0xa6490d0> 2 indexes [2, 0]",
"<NSIndexPath 0xa6a93b0> 2 indexes [2, 1]",
"<NSIndexPath 0xa6d08b0> 2 indexes [2, 2]",
"<NSIndexPath 0xa64ddb0> 2 indexes [3, 0]",
"<NSIndexPath 0xa6f7380> 2 indexes [3, 1]",
"<NSIndexPath 0xa6c1c90> 2 indexes [3, 2]",
"<NSIndexPath 0xa6e45f0> 2 indexes [4, 0]",
"<NSIndexPath 0xa6bf840> 2 indexes [4, 1]",
"<NSIndexPath 0xa6a03f0> 2 indexes [4, 2]"

} }

Thanks in Advance 提前致谢

It seems that you remove all objects from displayDataArray first, and then compute the indexPathArray from displayDataArray . 看来你删除所有对象displayDataArray ,然后再计算indexPathArraydisplayDataArray Therefore indexPathArray will be empty and 因此indexPathArray将为空,并且

[self.galleryCollectionView deleteItemsAtIndexPaths:[self createIndexPathArrayEntireCollectionView]];

does nothing . 什么没做 Changing the order to 将订单更改为

[self.galleryCollectionView deleteItemsAtIndexPaths:[self createIndexPathArrayEntireCollectionView]];
[displayDataArray removeAllObjects];

could solve the problem. 可以解决问题。 But as mentioned in a comment, you can just call reloadData if all collection view items have been removed. 但是,正如注释中提到的,如果所有集合视图项都已删除,则可以仅调用reloadData

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM