iOS【UICollectionView:reloadData与reloadSections冲突问题】


/*******
TestReload[32490:289811] ------after---reload
TestReload[32490:289811] -----------numberOfSectionsInCollectionView

TestReload[32490:289811] *** Assertion failure in -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKitCore_Sim/UIKit-3698.84.15/UICollectionView.m:6120

TestReload[32490:289811] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (1) must be equal to the number of items contained in that section before the update (2), plus or minus the number of items inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).'
*** First throw call stack:
(
0 CoreFoundation 0x0000000102b8629b __exceptionPreprocess + 331
1 libobjc.A.dylib 0x0000000102122735 objc_exception_throw + 48
2 CoreFoundation 0x0000000102b86022 +[NSException raise:format:arguments:] + 98
3 Foundation 0x0000000101b26b47 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 194
4 UIKitCore 0x00000001054c07cf -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:] + 16010
5 UIKitCore 0x00000001054bc09f -[UICollectionView _updateSections:updateAction:] + 431
6 UIKitCore 0x00000001054bc1b4 -[UICollectionView reloadSections:] + 62
7 TestReload 0x000000010180119c __29-[ViewController viewDidLoad]_block_invoke + 204
8 libdispatch.dylib 0x000000010447663e _dispatch_client_callout + 8
9 libdispatch.dylib 0x0000000104479589 _dispatch_continuation_pop + 565
10 libdispatch.dylib 0x000000010448b89f _dispatch_source_invoke + 2211
11 libdispatch.dylib 0x0000000104483800 _dispatch_main_queue_callback_4CF + 1071
12 CoreFoundation 0x0000000102ae97f9 CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE + 9
13 CoreFoundation 0x0000000102ae3e86 __CFRunLoopRun + 2342
14 CoreFoundation 0x0000000102ae3221 CFRunLoopRunSpecific + 625
15 GraphicsServices 0x000000010b1511dd GSEventRunModal + 62
16 UIKitCore 0x00000001053fd115 UIApplicationMain + 140
17 TestReload 0x00000001018023a0 main + 112
18 libdyld.dylib 0x00000001044ec551 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
*/


代码块

#import "ViewController.h"

@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView   *collectView;
@property (nonatomic, strong) NSMutableArray     *testArray;
@property (nonatomic, strong) NSMutableArray     *testArray2;

@property (nonatomic, strong) NSMutableArray     *testArray3;

@property (nonatomic, assign) BOOL               has3Group;

@end

@implementation ViewController

- (NSMutableArray *)testArray {
    if (!_testArray) {
        _testArray = [[NSMutableArray alloc] init];
    }
    return _testArray;
}

- (NSMutableArray *)testArray2 {
    if (!_testArray2) {
        _testArray2 = [[NSMutableArray alloc] init];
    }
    return _testArray2;
}

- (NSMutableArray *)testArray3 {
    if (!_testArray3) {
        _testArray3 = [[NSMutableArray alloc] init];
    }
    return _testArray3;
}

- (UICollectionView *)collectView {
    if (!_collectView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        _collectView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
        _collectView.delegate = self;
        _collectView.dataSource = self;
        [_collectView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"tewCell"];
    }
    return _collectView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //测试组数 默认2组
//    self.has3Group = YES;
    
    NSLog(@"------section组1:源-------");
    [self.testArray addObjectsFromArray:@[@"00000-0",@"0000001"]];
    NSLog(@"------section组2:源-------");
    [self.testArray2 addObjectsFromArray:@[@"11111-0",@"11111-1"]];
    [self.testArray3 addObjectsFromArray:@[@"22222-0",@"22222-1"]];
    [self.view addSubview:self.self.collectView];
    
    self.collectView.backgroundColor = [UIColor lightGrayColor];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        NSLog(@"------after---reload");
        //===== 测试1 修改第一组数据源,刷新第二组 崩溃 ======//
        [self.testArray removeLastObject];
        [self.collectView reloadSections:[NSIndexSet indexSetWithIndex:1]];
        

        //===== 测试2 修改第二组数据源,刷新第一组 崩溃 ======//
//        //[self.testArray removeLastObject];
//        [self.testArray2 removeLastObject];
//        [self.collectView reloadSections:[NSIndexSet indexSetWithIndex:0]];
//
        //===== 测试3 修改第一组数据源,刷新一、二组 没问题 ======//
//        [self.testArray removeLastObject];
//        [self.collectView reloadSections:[NSIndexSet indexSetWithIndex:0]];
//        [self.collectView reloadSections:[NSIndexSet indexSetWithIndex:1]];
//
//        //===== 测试4 修改第一组数据源,刷新二、一组 崩溃 ======//
//        [self.testArray removeLastObject];
//        [self.collectView reloadSections:[NSIndexSet indexSetWithIndex:1]];
//        [self.collectView reloadSections:[NSIndexSet indexSetWithIndex:0]];
        
        
        //===== 测试5 修改第二组数据源,刷新二、一组 没问题 ======//
//        [self.testArray2 removeLastObject];
//        [self.collectView reloadSections:[NSIndexSet indexSetWithIndex:1]];
//        [self.collectView reloadSections:[NSIndexSet indexSetWithIndex:0]];
        
        //===== 测试6 修改第二组数据源,刷新一、二组 崩溃 ======//
//        [self.testArray2 removeLastObject];
//        [self.collectView reloadSections:[NSIndexSet indexSetWithIndex:0]];
//        [self.collectView reloadSections:[NSIndexSet indexSetWithIndex:1]];
        
        //===== 测试7 修改第一、二组数据源,刷新一组 或 二组 崩溃 ======//
//        [self.testArray removeLastObject];
//        [self.testArray2 removeLastObject];
//        [self.collectView reloadSections:[NSIndexSet indexSetWithIndex:0]];
//        [self.collectView reloadSections:[NSIndexSet indexSetWithIndex:1]];
        
        //===== 测试8 修改第一刷新一、修改二刷新二,都执行 ======//
//        [self.testArray2 removeLastObject];
//        [self.collectView reloadSections:[NSIndexSet indexSetWithIndex:1]];
//         [self.testArray removeLastObject];
//        [self.collectView reloadSections:[NSIndexSet indexSetWithIndex:0]];
        

//        [self testReloadOrSection_0];

//        [self testReloadOrSection_1];
//
//        [self testReloadOrSection_2];
//
//        [self testReloadOrSection_3];
//
//        [self testReloadOrSection_4];

//        [self testReloadOrSection_3Groub5];
        
//        [self testReloadOrSection_3Group6];
    });
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"tewCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor greenColor];
    
    NSArray *tempArr = indexPath.section == 0 ?  self.testArray : (indexPath.section == 1 ? self.testArray2 : self.testArray3);
    NSLog(@"\n-----sectin: indx: %li, %ld  %@",indexPath.section,(long)indexPath.row,tempArr);
    return cell;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    NSLog(@"-----------numberOfSectionsInCollectionView");
    return self.has3Group ? 3 : 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return self.testArray.count;
    } else if (section == 1) {
        return self.testArray2.count;
    }
    return self.testArray3.count;
}


- (void)testReloadOrSection_0 {
    
    //===== 测试9 修改第一刷新一、修改二刷新表, ======//
    [self.testArray removeLastObject];
    //执行有效
    [self.collectView reloadSections:[NSIndexSet indexSetWithIndex:0]];
    [self.testArray2 removeLastObject];
    //不执行
    [self.collectView reloadData];
    NSLog(@"self.testArray2: %@",self.testArray2);
    
    /**
    结果:
    2018-10-27 16:46:35.746080+0800 TestReload[31767:268106] ------after---reload
    2018-10-27 16:46:35.747224+0800 TestReload[31767:268106]
    -----sectin: indx: 0, 0  (
                              "00000-0"
                              )
    2018-10-27 16:46:35.748565+0800 TestReload[31767:268106] self.testArray2: (
                                                                               "11111-0"
                                                                               )
     */
}

- (void)testReloadOrSection_1 {
    
    //===== 测试10 修改第一刷新表,在修改二、(结果:数据一、二都会刷) ======//
    [self.testArray2 removeLastObject];
    [self.collectView reloadData];
    [self.testArray removeLastObject];

    //延迟就不会刷
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        [self.testArray removeLastObject];
//    });
    
    /**
     结果:
     2018-10-27 16:30:23.917433+0800 TestReload[31482:259201] ------after---reload
     2018-10-27 16:30:23.918204+0800 TestReload[31482:259201]
     -----sectin: indx: 0, 0  (
     "00000-0"
     )
     2018-10-27 16:30:23.918396+0800 TestReload[31482:259201]
     -----sectin: indx: 1, 0  (
     "11111-0"
     )
     */
}

- (void)testReloadOrSection_2 {
    
    //===== 测试11 修改第一刷新表,在修改二、(结果:数据一、二都会刷) ======//
    [self.testArray2 removeLastObject];
    [self.collectView reloadData];
    [self.testArray removeLastObject];
    [self.collectView reloadSections:[NSIndexSet indexSetWithIndex:0]];
    
    /**
     结果:【 先刷 0 ,后刷 1 】
     -----sectin: indx: 0, 0  (
     "00000-0"
     )
     2018-10-27 16:31:39.886629+0800 TestReload[31520:260372]
     -----sectin: indx: 1, 0  (
     "11111-0"
     )
     */
}

- (void)testReloadOrSection_3 {
    
    //===== 测试12 修改第一刷新表,在修改二、(结果:数据一、二都会刷) ======//
    [self.testArray2 removeLastObject];
    [self.collectView reloadData];
    [self.testArray removeLastObject];
    //【 先刷 1 ,后刷 0 】
    [self.collectView reloadSections:[NSIndexSet indexSetWithIndex:1]];
    
    /**
     结果: 【 先刷 1 ,后刷 0 】
     2018-10-27 16:33:07.055397+0800 TestReload[31553:261296] ------after---reload
     2018-10-27 16:33:07.056510+0800 TestReload[31553:261296]
     -----sectin: indx: 1, 0  (
     "11111-0"
     )
     2018-10-27 16:33:07.056817+0800 TestReload[31553:261296]
     -----sectin: indx: 0, 0  (
     "00000-0"
     )
     */
}

- (void)testReloadOrSection_4 {
    
    //===== 测试12 修改第一刷新表,在修改二、(结果:数据一、二都会刷 没问题) ======//
    [self.testArray2 removeLastObject];
    [self.collectView reloadData];
    [self.testArray removeLastObject];
    [self.collectView reloadData];
    
    /**
    结果:
    2018-10-27 16:42:33.134940+0800 TestReload[31694:265878] ------after---reload
    2018-10-27 16:42:33.135664+0800 TestReload[31694:265878]
    -----sectin: indx: 0, 0  (
                              "00000-0"
                              )
    2018-10-27 16:42:33.135846+0800 TestReload[31694:265878]
    -----sectin: indx: 1, 0  (
                              "11111-0"
                              )
     */
}


- (void)testReloadOrSection_3Groub5 {
    // 3组
    if (!self.has3Group) {
        return;
    }
    //===== 测试12 修改第一刷新表,在修改二、(结果:数据一、二都会刷) ======//
    [self.testArray2 removeLastObject];
    [self.collectView reloadData];
    [self.testArray removeLastObject];
    //【 先刷 1 ,后刷 0 】
    [self.collectView reloadSections:[NSIndexSet indexSetWithIndex:1]];
    [self.collectView reloadSections:[NSIndexSet indexSetWithIndex:2]];
    
    /***
    结果:
    018-10-27 16:56:46.212766+0800 TestReload[31928:273289] ------after---reload
    2018-10-27 16:56:46.214051+0800 TestReload[31928:273289]
    -----sectin: indx: 1, 0  (
                              "11111-0"
                              )
    2018-10-27 16:56:46.214354+0800 TestReload[31928:273289]
    -----sectin: indx: 0, 0  (
                              "00000-0"
                              )
    2018-10-27 16:56:46.214606+0800 TestReload[31928:273289]
    -----sectin: indx: 2, 0  (
                              "22222-0",
                              "22222-1"
                              )
    2018-10-27 16:56:46.214814+0800 TestReload[31928:273289]
    -----sectin: indx: 2, 1  (
                              "22222-0",
                              "22222-1"
                              )
    2018-10-27 16:56:46.216255+0800 TestReload[31928:273289]
    -----sectin: indx: 2, 0  (
                              "22222-0",
                              "22222-1"
                              )
    2018-10-27 16:56:46.216513+0800 TestReload[31928:273289]
    -----sectin: indx: 2, 1  (
                              "22222-0",
                              "22222-1"
                              )

    */
}


- (void)testReloadOrSection_3Group6 {
    // 3组
    if (!self.has3Group) {
        return;
    }
    //===== 测试12 修改第一刷新表,在修改二、(结果:数据一、二都会刷) ======//
    [self.testArray2 removeLastObject];
    [self.collectView reloadData];
    [self.testArray removeLastObject];
    // 注意结果顺序 可以自己换换试试 结果有点奇怪
    [self.collectView reloadSections:[NSIndexSet indexSetWithIndex:2]];
    [self.collectView reloadSections:[NSIndexSet indexSetWithIndex:0]];
    
    /***
    结果:注意结果顺序
    2018-10-27 17:09:19.441771+0800 TestReload[32182:280787] ------after---reload
    2018-10-27 17:09:19.442226+0800 TestReload[32182:280787] -----------numberOfSectionsInCollectionView
    2018-10-27 17:09:19.442572+0800 TestReload[32182:280787] -----------numberOfSectionsInCollectionView
    2018-10-27 17:09:19.443159+0800 TestReload[32182:280787]
    -----sectin: indx: 2, 0  (
                              "22222-0",
                              "22222-1"
                              )
    2018-10-27 17:09:19.443402+0800 TestReload[32182:280787]
    -----sectin: indx: 2, 1  (
                              "22222-0",
                              "22222-1"
                              )
    2018-10-27 17:09:19.443630+0800 TestReload[32182:280787]
    -----sectin: indx: 0, 0  (
                              "00000-0"
                              )
    2018-10-27 17:09:19.443849+0800 TestReload[32182:280787]
    -----sectin: indx: 1, 0  (
                              "11111-0"
                              )
    2018-10-27 17:09:19.444969+0800 TestReload[32182:280787] -----------numberOfSectionsInCollectionView
    2018-10-27 17:09:19.445387+0800 TestReload[32182:280787]
    -----sectin: indx: 0, 0  (
                              "00000-0"
                              )
     */
    
    
    //[self.collectView reloadSections:[NSIndexSet indexSetWithIndex:2]];
    //[self.collectView reloadSections:[NSIndexSet indexSetWithIndex:1]];
    /**
    结果:
    2018-10-27 17:14:47.485862+0800 TestReload[32264:283424] ------after---reload
    2018-10-27 17:14:47.486340+0800 TestReload[32264:283424] -----------numberOfSectionsInCollectionView
    2018-10-27 17:14:47.486698+0800 TestReload[32264:283424] -----------numberOfSectionsInCollectionView
    2018-10-27 17:14:47.487274+0800 TestReload[32264:283424]
    -----sectin: indx: 2, 0  (
                              "22222-0",
                              "22222-1"
                              )
    2018-10-27 17:14:47.487533+0800 TestReload[32264:283424]
    -----sectin: indx: 2, 1  (
                              "22222-0",
                              "22222-1"
                              )
    2018-10-27 17:14:47.487760+0800 TestReload[32264:283424]
    -----sectin: indx: 0, 0  (
                              "00000-0"
                              )
    2018-10-27 17:14:47.487976+0800 TestReload[32264:283424]
    -----sectin: indx: 1, 0  (
                              "11111-0"
                              )
    2018-10-27 17:14:47.489072+0800 TestReload[32264:283424] -----------numberOfSectionsInCollectionView
    2018-10-27 17:14:47.489533+0800 TestReload[32264:283424]
    -----sectin: indx: 1, 0  (
                              "11111-0"
                              )
     */
}
@end
附加:

*** Assertion failure in -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:]

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        collectionView.performBatchUpdates({ () -> Void in
                collectionView.deleteItemsAtIndexPaths([indexPath])
            }) { (finish) -> Void in
        }
    }

原因:执行完performBatchUpdates操作之后,collection view会自动reloadData,
调用numberOfItemsInSection等方法重新布局,
这时就会出现数据越界等情况,
所以要在performBatchUpdates的block中更新numberOfItemsInSection的个数:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        collectionView.performBatchUpdates({ () -> Void in
                collectionView.deleteItemsAtIndexPaths([indexPath])
                // 该函数的作用是删除对应的模型数据
               self.removeImageWithIndex(indexPath.row)
            }) { (finish) -> Void in
        }
    }

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,772评论 6 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,458评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,610评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,640评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,657评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,590评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,962评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,631评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,870评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,611评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,704评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,386评论 4 319
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,969评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,944评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,179评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,742评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,440评论 2 342

推荐阅读更多精彩内容