0

I have two CollectionView in same ViewController when i try to change the data of CollectionView content then the app get crashed with error -

*** Assertion failure in -[UICollectionViewData validateLayoutInRect:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3600.7.47/UICollectionViewData.m:433

 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView received layout attributes for a cell with an index path that does not exist: <NSIndexPath: 0xc000000000600016> {length = 2, path = 0 - 3}'
*** First throw call stack:
(
    0   CoreFoundation                      0x00000001105f0b0b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x000000010fb54141 objc_exception_throw + 48
    2   CoreFoundation                      0x00000001105f4cf2 +[NSException raise:format:arguments:] + 98
    3   Foundation                          0x000000010f6ee3b6 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 193
    4   UIKit                               0x000000010e75223d __45-[UICollectionViewData validateLayoutInRect:]_block_invoke + 1743
    5   UIKit                               0x000000010e7515e9 -[UICollectionViewData validateLayoutInRect:] + 2964
    6   UIKit                               0x000000010e6f93ca -[UICollectionView layoutSubviews] + 233
    7   UIKit                               0x000000010de8020b -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1268
    8   QuartzCore                          0x000000010ca5e904 -[CALayer layoutSublayers] + 146
    9   QuartzCore                          0x000000010ca52526 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 370
    10  QuartzCore                          0x000000010ca523a0 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
    11  QuartzCore                          0x000000010c9e1e92 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
    12  QuartzCore                          0x000000010ca0e130 _ZN2CA11Transaction6commitEv + 468
    13  QuartzCore                          0x000000010ca0eb37 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 115
    14  CoreFoundation                      0x0000000110596717 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    15  CoreFoundation                      0x0000000110596687 __CFRunLoopDoObservers + 391
    16  CoreFoundation                      0x000000011057b720 __CFRunLoopRun + 1200
    17  CoreFoundation                      0x000000011057b016 CFRunLoopRunSpecific + 406
    18  GraphicsServices                    0x0000000112cf1a24 GSEventRunModal + 62
    19  UIKit                               0x000000010ddbd0d4 UIApplicationMain + 159
    20  EuroSchool WebGenie                 0x0000000109284754 main + 132
    21  libdyld.dylib                       0x0000000110d4265d start + 1
)

libc++abi.dylib: terminating with uncaught exception of type NSException

Here is my collectionView's data source & delegate methods

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    int value = 0;

    if (collectionView.tag == 1) {

        value = sizeArray.count;
    }
    else {

        value = colorArray.count;
    }

    return value;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView.tag == 1) {

        ProductSizeCustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SizeCell" forIndexPath:indexPath];

        NSMutableDictionary *dict = [[sizeArray objectAtIndex:indexPath.item] mutableCopy];

        cell.sizeLbl.alpha = 1.0;
        cell.sizeLbl.layer.borderColor = [Nav_Bar_Color toColor].CGColor;
        cell.sizeLbl.text = [NSString stringWithFormat:@"%@", [dict objectForKey:@"value"]];

        if ([[dict objectForKey:@"isSelected"] boolValue]) {

            cell.sizeLbl.alpha = 1.0;
            cell.sizeLbl.backgroundColor = [@"#0288D1" toColor];
            cell.sizeLbl.textColor = [UIColor whiteColor];

            previousSelectedSizeItem = indexPath.item;
        }
        else {

            cell.sizeLbl.alpha = 1.0;
            cell.sizeLbl.backgroundColor = [UIColor whiteColor];
            cell.sizeLbl.textColor = [UIColor blackColor];
        }

        if ([dict objectForKey:@"isEnable"] != nil) {

            if ([[dict objectForKey:@"isEnable"] boolValue]) {

                cell.sizeLbl.alpha = 1.0;
            }
            else {

                cell.sizeLbl.alpha = 0.5;
            }
        }

        return cell;
    }
    else if (collectionView.tag == 2) {

        ProductColorCustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ColorCell" forIndexPath:indexPath];

        NSDictionary *dict = [colorArray objectAtIndex:indexPath.item];

        cell.backgroundLbl.alpha = 1.0;
        cell.sizeLbl.alpha = 1.0;

        cell.sizeLbl.layer.borderColor = [[[colorArray objectAtIndex:indexPath.item] objectForKey:@"value"] toColor].CGColor;
        cell.sizeLbl.backgroundColor = [[[colorArray objectAtIndex:indexPath.item] objectForKey:@"value"] toColor];
        cell.sizeLbl.text = @"";
        cell.backgroundLbl.layer.borderColor = [UIColor clearColor].CGColor;

        if ([[dict objectForKey:@"isSelected"] boolValue] && [dict objectForKey:@"isSelected"] != nil) {

            cell.backgroundLbl.layer.borderColor = [@"#0288D1" toColor].CGColor;
            cell.backgroundLbl.backgroundColor = [@"#0288D1" toColor];

            previousSelectedColorItem = indexPath.item;
        }
        else {

            cell.backgroundLbl.layer.borderColor = [UIColor whiteColor].CGColor;
            cell.backgroundLbl.backgroundColor = [UIColor whiteColor];
        }

        if ([dict objectForKey:@"isEnable"] != nil) {

            if ([[dict objectForKey:@"isEnable"] boolValue]) {

                cell.imgViewObj.hidden = YES;
            }
            else {

                cell.backgroundLbl.layer.borderColor = [@"EDECEC" toColor].CGColor;
                cell.backgroundLbl.backgroundColor = [@"EDECEC" toColor];

                cell.imgViewObj.hidden = NO;
            }
        }
        else {

            cell.imgViewObj.hidden = YES;
        }

        return cell;
    }

    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat width = 50;

    if (collectionView.tag == 1) {

        CGSize stringSize = [[[sizeArray objectAtIndex:indexPath.item] objectForKey:@"value"] sizeWithAttributes:@{NSFontAttributeName:[UIFont fontWithName:FONT_NAME_REGUALR size:FONT_SIZE_LARGE]}];

        width = stringSize.width + 34;

        if (width < 50) {

            width = 50;
        }
    }

    return CGSizeMake(width, 50);
}

Here is my method for setting collectionView layout

-(void)arrangeViews:(NSMutableArray *)skuDescriptionArray atIndex:(int)index {

    BOOL isSizeAvailable = NO, isColorAvailable = NO;

    for (int i = 0; i < allProductAttributesArray.count; i++) {

        NSDictionary *dict = [allProductAttributesArray objectAtIndex:i];

        if ([[dict objectForKey:@"type"] caseInsensitiveCompare:@"size"] == NSOrderedSame) {

            isSizeAvailable = YES;
        }
        else if ([[dict objectForKey:@"type"] caseInsensitiveCompare:@"color"] == NSOrderedSame) {

            isColorAvailable = YES;
        }
    }

    NSString *descriptionString;

    if (skuDescriptionArray.count > 0) {

        descriptionString = [[skuDescriptionArray objectAtIndex:0] objectForKey:@"description"];
    }

    if (descriptionString.length > 0) {

        descriptionLbl.text = descriptionString;
    }
    else {

        descriptionLbl.text = [[productArray objectAtIndex:index] objectForKey:@"description"];
    }

    CGSize boundingSize = CGSizeMake(messageWidth - 20, 10000000);
    CGRect itemTextSize = [descriptionLbl.text boundingRectWithSize:boundingSize
                                         options:NSStringDrawingUsesLineFragmentOrigin
                                      attributes:@{NSFontAttributeName:[UIFont fontWithName:FONT_NAME_REGUALR size:FONT_SIZE]}
                                         context:nil];

    CGFloat height = itemTextSize.size.height + 15;

    if (isSizeAvailable && isColorAvailable) {

        lineLbl.hidden = NO;

        collectionViewObjHeightConstraint.constant = 50;
        sizeLblHeightConstraint.constant = 21;
        sizeSpaceHeightConstraint.constant = 10;

        collectionViewObj2HeightConstraint.constant = 50;
        colorLblHeightConstraint.constant = 21;
        colorSpaceHeightConstraint.constant = 10;

        sizeColorViewHeightConstraint.constant = 250;

        contentViewHeightConstraint.constant = 600 + height;
    }
    else if (!isSizeAvailable && !isColorAvailable) {

        lineLbl.hidden = YES;

        collectionViewObjHeightConstraint.constant = 0;
        sizeLblHeightConstraint.constant = 0;
        sizeSpaceHeightConstraint.constant = 0;

        collectionViewObj2HeightConstraint.constant = 0;
        colorLblHeightConstraint.constant = 0;
        colorSpaceHeightConstraint.constant = 0;

        sizeColorViewHeightConstraint.constant = 100;

        contentViewHeightConstraint.constant = 440 + height;
    }
    else if (isSizeAvailable && !isColorAvailable) {

        lineLbl.hidden = NO;

        collectionViewObjHeightConstraint.constant = 50;
        sizeLblHeightConstraint.constant = 21;
        sizeSpaceHeightConstraint.constant = 10;

        collectionViewObj2HeightConstraint.constant = 0;
        colorLblHeightConstraint.constant = 0;
        colorSpaceHeightConstraint.constant = 0;

        sizeColorViewHeightConstraint.constant = 170;

        contentViewHeightConstraint.constant = 510 + height;
    }
    else if (!isSizeAvailable && isColorAvailable) {

        collectionViewObjHeightConstraint.constant = 0;
        sizeLblHeightConstraint.constant = 0;
        sizeSpaceHeightConstraint.constant = 0;

        lineLbl.hidden = NO;

        collectionViewObj2HeightConstraint.constant = 50;
        colorLblHeightConstraint.constant = 21;
        colorSpaceHeightConstraint.constant = 10;

        sizeColorViewHeightConstraint.constant = 170;

        contentViewHeightConstraint.constant = 510 + height;
    }
}

Does anyone know why i am getting this error ?

trungduc
  • 11,323
  • 4
  • 29
  • 51
Savita Pal
  • 63
  • 9
  • Can you show your code or the line which causes crash? It's more easier to find solution – trungduc Nov 10 '17 at 09:16
  • when i reload the data using [collectionViewObj reloadData] then app gets crashed after numberOfItemsInSection this method – Savita Pal Nov 10 '17 at 09:24
  • Did you take a look at this https://stackoverflow.com/questions/39867325/ios-10-bug-uicollectionview-received-layout-attributes-for-a-cell-with-an-index? – trungduc Nov 10 '17 at 09:30
  • @trungduc using the above link which you have provided still i am getting crash – Savita Pal Nov 10 '17 at 09:37
  • It's easier when you provide code to know what are you doing and why you get crash – trungduc Nov 10 '17 at 09:43
  • if (sizeArray.count > 0 && colorArray.count > 0){ [collectionViewObj reloadData]; [collectionViewObj.collectionViewLayout invalidateLayout]; [collectionViewObj2 reloadData]; [collectionViewObj2.collectionViewLayout invalidateLayout]; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ int value = 0; if (collectionView.tag == 1) { value = sizeArray.count; } else { value = colorArray.count; } return value; } – Savita Pal Nov 10 '17 at 09:48
  • Why do you need `invalidateLayout` after `reloadData`. Did you try to remove it? – trungduc Nov 10 '17 at 09:50
  • Yes i have removed invalidateLayout but error occurred – Savita Pal Nov 10 '17 at 09:50
  • Please give all of your code for collectionView and update it to your question. if not, i think it's hard to give you a right answer. – trungduc Nov 10 '17 at 09:52
  • @trungduc please check the code in question, i have updated the question – Savita Pal Nov 10 '17 at 10:16
  • @SavitaPal have you tried setting the [UICollectionViewDelegateFlowLayout](https://stackoverflow.com/a/36452092/3789527)? – LS_ Nov 10 '17 at 10:28
  • @Signo I have not tried UICollectionViewDelegateFlowLayout – Savita Pal Nov 10 '17 at 10:31
  • @SavitaPal You just inherit it in your class and set the `UICollectionView.delegate` to `self` – LS_ Nov 10 '17 at 10:49
  • i think...When you are reloading the collection view...Same Layout is going for both the collection view...Thats why your collection view getting crashed....Check in collection view flow layout method. – Neeraj Sonaro Nov 10 '17 at 10:51
  • I think in some case, your cell width will have problem because it's calculated base on string size. Try to add `if (width > collectionView.frame.size.width) {width = collectionView.frame.size.width}` – trungduc Nov 10 '17 at 10:56
  • @trungduc i have tried using hardcoded value for collectiob view width but still i am getting error – Savita Pal Nov 10 '17 at 11:04
  • @SavitaPal can you show the code which you use to set up or create collectionView? – trungduc Nov 10 '17 at 11:11
  • @trungduc I have updated the question, please check the setup method for collectionview in question. – Savita Pal Nov 10 '17 at 11:22
  • @SavitaPal did it get crash at the first time you call `reloadData`? – trungduc Nov 10 '17 at 11:29
  • @trungduc I have used segmentControl in that it has 4 segments when i am changing the segment at that time app gets crashed. – Savita Pal Nov 10 '17 at 11:32
  • @SavitaPal actually i can’t guess what happened with your collection view. Seem like everything you show me is good – trungduc Nov 10 '17 at 12:16
  • 1
    @trungduc I found the solution after disabling the Prefetch from Attributes Inspector(In storyboard) the error gets gone. Thank you for your valueable time and reply – Savita Pal Nov 10 '17 at 12:42
  • @SavitaPal prefect delegate right? That why we can’t find it in your code. Congratuation ;) – trungduc Nov 10 '17 at 12:43
  • @trungduc Yes.. – Savita Pal Nov 10 '17 at 12:49

0 Answers0