A strange UITableView crash : NSRangeException - ios

I have encountered a strange crash on my code.
Here's the code :
UIView *tableHeaderView = self.tableView.tableHeaderView;
CGRect frame = tableHeaderView.frame;
frame.size.height = 0;
tableHeaderView.frame = frame;
self.tableView.tableHeaderView = tableHeaderView;
The crash occurred on the last line of the code.
And, here is the log
Terminating app due to uncaught exception 'NSRangeException', reason:
'*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
*** First throw call stack:
(0x345ae2a3 0x3c25397f 0x344f9b75 0x1a7055 0x363ebb8f 0x363d702b 0x363d7599 0x363d740d 0x3642b22b 0x1a0d77 0x363d5595 0x3642a14b 0x3642a091 0x36429f75 0x36429e99 0x364295d9 0x364294c1 0x364764d1 0x36428861 0x36428713 0x364a4399 0x364508fb 0x36691619 0x364a39b9 0x364a1fe7 0x26c8a1 0x221997 0x34ee96fd 0x34e291f9 0x34e29115 0x3428b45f 0x3428ab43 0x342b2fcb 0x344f474d 0x342b342b 0x3421703d 0x34583683 0x34582ee9 0x34581cb7 0x344f4ebd 0x344f4d49 0x380b82eb 0x3640a301 0x10819f 0x3c68ab20)
libc++abi.dylib: terminate called throwing an exception
I really don't have any idea what happened on my code.
Any advice are helpful for me!
Thank you.

You definitely initialise some array in your class firstly check your array by placing NSLog that it should have some value or it is null. I think it is null thats why they creating a problem.

If you want to change the headerView for a table, this is the proper way to do it:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[UIView alloc] initWithFrame:kHeaderFrame];
return headerView;
}

Related

UICollectionView's performBatchUpdates crash iOS7

I noticed a strange difference in how performBatchUpdates:completion: method of UICollectionView works in iOS7 (bad - crashes) and iOS8 (good). Here's the code I use:
[self.swapItems removeObject:self.swapItems[indexPath.row]];
[self.swapItemsGrid performBatchUpdates:^{
[self.swapItemsGrid deleteItemsAtIndexPaths:#[indexPath]];
} completion:^(BOOL finished) {
[self layoutViews];
}];
In iOS8 it works fine, while in iOS7 it crashes with the following error:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
A little debugging has shown that in iOS8 the performBatchUpdates:completion: method calls the data source method collectionView:numberOfItemsInSection:, while in iOS7 it doesn't, hence the error where a cell is attempted to be created with the data of an object that is in the data array no more.
Has anyone else run into this issue? Perhaps you have a solution in mind?
self.swapItemsGrid might does not have item at index indexPath.
To avoid this, use this to check indexPath exits in array or not:
-(BOOL)checkIndexPathExits:(NSIndexPath*)indexPath inArray:(NSArray*)array{
if (indexPath.section < array.count) {
if (indexPath.row < [array[indexPath.section] count]) {
return YES;
}
}
return NO;
}
Hope this could help.

tableview is nil and the app crashes ios xcode

IN my program,I am setting the constraints for my view using nslayoutconstraints.the view consists of a tableview,collectionview and a main view.When the view is loaded,the app crashes with the following error
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]'
*** First throw call stack:
(0x27126d67 0x34c61c77 0x27047237 0x2704701b 0xe1333 0xe0bc1 0x1c6ca7 0x1d24e1 0x9b59cb 0x9b59b7 0x9b9411 0x270ecc41 0x270eb361 0x27038981 0x27038793 0x2e3e8051 0x2a62a981 0x1d72b5 0x351fdaaf)
libc++abi.dylib: terminating with uncaught exception of type NSException
The value of the tableview is "NIL"
NSDictionary *viewsDictionary = #{#"tableView":_tableView,
#"mainView": _mainView,
#"movieDetail":_movieDetailView
};
This dictionary is used to set the height constraint in the tableview as below
NSArray *constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:#"V:[tableView(tableHeight)]"
options:0
metrics:metrics
views:viewsDictionary];
[_tableView addConstraints:constraint_V];
Why is it that the value of tableview is nil?This tablview is an IBOUTLET from storyboard.When i allocate memory programatically the code runs.but the tableview is not loaded with data.Can anyone get me the reason?
Thank you in advance
Make sure you're not calling this too early and the table view has not yet been created. Perhaps you are executing this code in "viewDidLoad", maybe try viewWillAppear, or viewDidLayoutSubviews.
Please ensure you're synthesizing correctly to that property.
#synthesize yourIBOutletTableView = _tableView;

Getting "unrecognized selector sent to instance" error message on 1 of 4 background image loads within same code?

I have created 4 buttons named cell1, cell2 cell3 and cell4
I am attempting to set the background image in the following for loop.
Cells 1, 2 and 4 load successfully with the background image.
I received the following message for cell3.
All cells were created the same, the tag for each button was set 1, 2, 3 and 4.
I am a loss as to why cell3 (button) does not load. I have reviewed similar previously answered question on this topic but am drawing a blank.
Here is the code that is failing:
for (int i = 1; i <= 4; ++i) {
UIButton *cellIndex = (UIButton *)([self.view viewWithTag:i]);
NSLog(#"==> viewDidLoad cellIndex1 = (%i)", cellIndex.tag);
[cellIndex setBackgroundImage:[UIImage imageNamed:#"L0background.png"] forState:UIControlStateNormal];
}
Here is the results for the background load:
2013-08-30 09:50:07.898 match[863:11f03] ==> viewDidLoad cellIndex1 = (1)
2013-08-30 09:50:07.899 match[863:11f03] ==> viewDidLoad cellIndex1 = (2)
2013-08-30 09:50:07.899 match[863:11f03] ==> viewDidLoad cellIndex1 = (3)
2013-08-30 09:50:07.900 match[863:11f03] -[UIView setBackgroundImage:forState:]: unrecognized selector sent to instance 0x7d68230
2013-08-30 09:50:07.901 match[863:11f03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setBackgroundImage:forState:]: unrecognized selector sent to instance 0x7d68230'
*** First throw call stack:
(0x1912012 0x1695e7e 0x199d4bd 0x1901bbc 0x190194e 0x27bf 0x6bb1c7 0x6bb232 0x60a3d5 0x60a76f 0x60a905 0x613917 0x22f5 0x5d7157 0x5d7747 0x5d894b 0x5e9cb5 0x5eabeb 0x5dc698 0x277ddf9 0x277dad0 0x1887bf5 0x1887962 0x18b8bb6 0x18b7f44 0x18b7e1b 0x5d817a 0x5d9ffc 0x202d 0x1f55 0x1)
libc++abi.dylib: terminate called throwing an exception
(lldb)
Your cellIndex is declared inside the for loop. Its local to the for loop. You are trying to set a background image outside the for loop where it has no knowledge of cellIndex object. Have you declared a cellIndex object outside that for loop?
If not, then you will have to set the background image inside your for loop.
The problem is clear: [self.view viewWithTag:i]) is returning an UIView instead of an UIButton when i is 3.
You can verify that yourself by adding NSLog(#"Returned view: %#", cellIndex) just below that line. This log will also show which view is returned. You can then use that information to identify the culprit. I suspect that there's another view in the view hierarchy with 3 as tag. Double-check your XIBs/Storyboards to make sure the only view that has 3 as its tag is your button.
Like the others have said, UIView does not respond to the method in question. The object at index 4 is not a UIButton.
To help guard against this crash, do this:
UIButton *buttonObject;
for (int i = 1; i <= 4; ++i) {
id cellIndex = [self.view viewWithTag:i];
if ([cellIndex isKindOfClass:[UIButton class]]) {
buttonObject = (UIButton *)cellIndex;
NSLog(#"==> viewDidLoad cellIndex1 = (%i)", cellIndex.tag);
[buttonObject setBackgroundImage:[UIImage imageNamed:#"L0background.png"] forState:UIControlStateNormal];
} else {
NSLog(#"Not a button, returned %#",[cellIndex class]);
}
}

UICollectionView Exception Crash

I have a mystery problem with UICollectionView.
I add my collection view to my tableview cell like so:
UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
UICollectionView *collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100) collectionViewLayout:layout];
[collection setDataSource:self];
[collection setDelegate:self];
[collection setBackgroundColor:[UIColor whiteColor]];
[collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"Cell"];
collection.tag = kTagPhotoCollectionView;
[cell.contentView addSubview:collection];
I'm implementing all the necessary delegate and data source methods and you can see I've set the datasource and delegate and have implemented them in my header.
When I go and press a UIButton which displays a UIImagePickerController and the app crashes with this error:
2013-08-13 20:17:02.578 [619:60b] *** Assertion failure in -[PUCollectionView _createPreparedSupplementaryViewForElementOfKind:atIndexPath:withLayoutAttributes:applyAttributes:], /SourceCache/UIKit/UIKit-2891/UICollectionView.m:1397
2013-08-13 20:17:35.502 Sparky[619:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView dataSource is not set'
*** First throw call stack:
(0x30a02ed3 0x3aa2b6c7 0x30a02da5 0x3138c72f 0x3305f647 0x32f4adf9 0x32f47139 0x32eea543 0x32b72feb 0x32b6e817 0x32b6e6a9 0x32b6e0bd 0x32b6decf 0x32b67bfd 0x309cdf71 0x309cb8ff 0x309cbc4b 0x30936541 0x30936323 0x353ca343 0x32f4f8a5 0x5f835 0x3af40ab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
I have set the datasource, so I have no idea why this would occur.
I faced with the similar problem, but my app crashed after I programmatically pop my UICollectionViewController. In some reason (I think it's just a bug in SDK) self.collectionView was alive after its' controller destroy, thus causing this failure:
*** Assertion failure in -[UICollectionView _createPreparedSupplementaryViewForElementOfKind:atIndexPath:withLayoutAttributes:applyAttributes:], /SourceCache/UIKit/UIKit-2935.137/UICollectionView.m:1305
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView dataSource is not set'
The solution is just override -dealloc in UICollectionViewController and release self.collectionView manually. ARC code:
- (void)dealloc {
self.collectionView = nil;
}
Hope this will save time for somebody.

how to solve NSRangeException

hi i am new to iphone programing,i am using EGOPhotViewer and want to show images by using this code,
for ( recipeImages in recipeImages.imgArray) {
photo = [[MyPhoto alloc] initWithImageURL:[NSURL URLWithString:recipeImages.recipie_img_url]name:recipeImages.recipe_name];
NSLog(#"%#",recipeImages.recipie_img_url);
MyPhotoSource *source = [[MyPhotoSource alloc] initWithPhotos:[NSArray arrayWithObjects:photo ,nil]];
photoController = [[EGOPhotoViewController alloc] initWithPhotoSource:source];
}
[APPDELEGATE.navigationController pushViewController:photoController animated:YES];
and i get this error
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 2147483648 beyond bounds [0 .. 0]'
*** First throw call stack:
(0x225f012 0x2084e7e 0x2214b44 0x9e1e4 0xa46c4 0x1b45dc9 0x22b90c5 0x2213efa 0x1a7a482 0x1a8d73b 0xa9d7c 0xa6a4e 0xa5081 0xa0499 0x10af753 0x10afa7b 0x10bd590 0x10c55bd 0x10c5eab 0x10c64a3 0x10c6098 0x5bad6 0x2098705 0xfcf920 0xfcf8b8 0x1090671 0x1090bcf 0x108fd38 0xfff33f 0xfff552 0xfdd3aa 0xfcecf8 0x2c15df9 0x2c15ad0 0x21d4bf5 0x21d4962 0x2205bb6 0x2204f44 0x2204e1b 0x2c147e3 0x2c14668 0xfcc65c 0x263a 0x2545)
libc++abi.dylib: terminate called throwing an exception
i solved this by writing this code
NSMutableArray *localImagesArray = [[NSMutableArray alloc] init];
for ( recipeImages in recipeImages.imgArray) {
photo = [[MyPhoto alloc] initWithImageURL:[NSURL URLWithString:recipeImages.recipie_img_url]name:recipeImages.recipe_name];
NSLog(#"%#",recipeImages.recipie_img_url);
NSLog(#"%#", [photo debugDescription]);
[localImagesArray addObject:photo];
}
MyPhotoSource *source = [[MyPhotoSource alloc] initWithPhotos:localImagesArray];
photoController = [[EGOPhotoViewController alloc] initWithPhotoSource:source];
[APPDELEGATE.navigationController pushViewController:photoController animated:YES];
}
[__NSArrayI objectAtIndex:]: index 2147483648 beyond bounds [0 .. 0]'
2147483648 is NSNotFound. Somewhere in your code, or the code of a library you are using, something like indexOfObject: is being used on one array, and that index is being used to get a value from another array, and it is failing.
Your for loop looks very suspect. You're assigning a value to photoController at the end of each iteration, meaning only the value you assign last will actually get used. I'm not familiar with the library you're using but you probably want to be building up an array of MyPhoto objects before passing them to a single photoController.
Make sure initWithImageURL: name: inside MyPhoto returns self.
Verify this with NSLog(#"%#", [photo debugDescription]);

Resources