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 ?