I am entirely confused. I am modifying my table and it's crashing while up via adding/deleting rows and reloading sections. So I stopped adding and deleting rows, and it still crashes. So I stopped reloading sections and it still crashes. So I just put the code:
[self.tableView beginUpdates];
[self.tableView endUpdates];
with this code it crashes, without it's fine.
Why?
2014-09-12 21:52:41.257 weatherApp[12616:60b] * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
* First throw call stack:
(0x2ea84f83 0x392ffccf 0x2e9bb7cb 0x3149eaef 0x3149cd8f 0x3149c843 0x31474e5b 0xeb82d 0xed059 0x313d48f7 0x3137bc27 0x3137b47d 0x312a1d59 0x30f1f62b 0x30f1ae3b 0x30f1accd 0x30f1a6df 0x30f1a4ef 0x30f6dc49 0x33d3a75d 0x2f770451 0x2ea44ea9 0x2ea4fa67 0x2ea4fa03 0x2ea4e1d7 0x2e9b8ebf 0x2e9b8ca3 0x33912663 0x3130514d 0x107599 0x3980cab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
Edit: Be advised I am calling this update in cellForRowAtIndexPath because I need to check for an update as new cells are displayed. The code that causes this update is the [self updateSection code, which right now only runs begin updates, end updates.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
WFHNotamTableViewCell *cell = (WFHNotamTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellTableIdentifier forIndexPath:indexPath];
if (!cell)
{
NSLog(#"Dequeue blank cell");
}
NSLog(#"Loading %d - %d", indexPath.section, indexPath.row);
NSString *group;
NSString *icao = [metarManager getIdentifierAtTotalIndex:indexPath.section andReturnGroup:&group];
NSMutableDictionary *notamDict = [notamManager getNotamFor:icao atVisibleIndex:indexPath.row];
NSDate *lastUpdate = [notamDict objectForKey:#"timeDownloaded"];
[self updateSection:indexPath.section andLastUpdate:lastUpdate];
return cell;
Thank you!
Related
I'm trying to delete multiple selections from a table view. Everything works fine until I scroll up or down, then it crashes and throws an exception.
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
Object can not be nil
This is how I am deleting the objects :
- (IBAction)deleteObjects:(id)sender {
NSArray *selectedRows = [self.tableView indexPathsForSelectedRows];
BOOL deleteSpecificRows = selectedRows.count > 0;
if (deleteSpecificRows)
{
NSMutableArray *stringsOfObjects = [NSMutableArray new];
for (NSIndexPath *selectionIndex in selectedRows) {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:selectionIndex];
[stringsOfObjects addObject:cell.textLabel.text];
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSString *plistPath = [path stringByAppendingPathComponent:#"AlertSubscriptions.plist"];
NSMutableArray *array = [[[NSMutableArray alloc] initWithContentsOfFile:plistPath] mutableCopy];
[array removeObjectsInArray:stringsOfObjects];
[self.alertSubArray removeObjectsInArray:stringsOfObjects];
[array writeToFile:plistPath atomically:YES];
[self.tableView deleteRowsAtIndexPaths:selectedRows withRowAnimation:UITableViewRowAnimationAutomatic];
}
Again this all works fine, unless I scroll up/down to select/deselect more cells so I subclassed my cells because I read that won't reuse cells on SO.
For reference:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
SubscriptionsTableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[SubscriptionsTableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil];
}
cell.textLabel.text = [self.alertSubArray objectAtIndex:indexPath.row];
cell.textLabel.textAlignment = NSTextAlignmentLeft;
return cell;
}
Ive tried it with a static cell and without. I've tried setting dequeueReusableCellWithIdentifier to a static cell and without. Neither work when I scroll
static NSString *CellIdentifier = #"Cell";
Error log:
2015-06-28 15:46:19.379 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
(0x186d3c2d8 0x1985680e4 0x186c234c0 0x10017e7d8 0x18b7b1404 0x18b79a4e0 0x18b7b0da0 0x18b7b0a2c 0x18b7a9f68 0x18b77d18c 0x18ba1e324 0x18b77b6a0 0x186cf4240 0x186cf34e4 0x186cf1594 0x186c1d2d4 0x1904336fc 0x18b7e2fac 0x1001646d4 0x198be6a08)
libc++abi.dylib: terminating with uncaught exception of type NSException
EDIT
So after trying what the others have told me to do I have done the following:
Set an exception breakpoint. The line that populates after crash is the [stringsOfObjects addObject:cell.textLabel.text]; line.
i have made sure my cellForRowAtIndexPath method was set up properly now:
}
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.backgroundColor = [UIColor whiteColor];
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
[self.tableView setRowHeight:45];
cell.textLabel.text = [self.alertSubArray objectAtIndex:indexPath.row];
cell.textLabel.textAlignment = NSTextAlignmentLeft;
return cell;
}
After that I still get a crash at adding the objects to the NSMutableArray so I looked in the debugger and made sure my UITableViewCell isn't nil like Robot mentioned to me, and it looks like it is but I don't know where to go from here: because in my mind it is not nil
As you can see, I have selected 6 rows but it only added 2 objects. I don't know why this is so difficult, why is it nil when some aren't? And why can I delete them perfectly fine without scrolling to select more?
So, after an extensive discussion in the comments the problem seems to be the following:
The logic of [stringsOfObjects addObject:cell.textLabel.text]; in the deleteObjects: method is wrong. This is because it is taking the text direct from the cells rather than the array backing store that populates the cells.
Cells can be scrolled offscreen and re-used so the text in them is no longer correct and, in fact, the cell no longer "exists" as it has been reused. If the cell doesn't "exist" an empty cell will be created where the text field might be nil. Note that cell re-use is a good thing; don't create cells and never re-use them or you will run out of memory fast.
Instead, take the text from your backing store that populates the cells themselves rather than from the cell directly. I would expect code something like:
[stringsOfObjects addObject:[self.alertSubArray objectAtIndex:selectionIndex.row]];
I have a JSON file: http://astrodeer.com.au/ingredients.php which has been been parsed in. I created two NSMutableArrays, one for the ingredients and one for the ratings for each product listed.
First of all I have populated my first table with the product name/brand:
When a user clicks on an item, it takes them to this page where I am attempting to populate the table with the ingredient names and their corresponding ratings.
This is what I have so far. I am getting an error at "numberOfRowsInSection"
"Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 4 beyond bounds [0 .. 3]'"
Ideally I am going to change the UIImage depending on the rating number using an if statement. But at the moment I am struggling to work out how to get the ingredient name to display.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section {
return [_selectedProduct.ingredientsArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"ingredientsCell";
UITableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
UILabel *ingLabel = (UILabel *)[myCell viewWithTag:1];
UIImage *ingRating = (UIImage *)[myCell viewWithTag:2];
ingLabel.text = _selectedProduct.ingredientsArray[indexPath.row];
return myCell;
}
I keep getting the following error when trying to insert a row:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]'
*** First throw call stack:
(0x31fe82a3 0x39ccc97f 0x31f33b75 0x8d18f 0x33fee5a9 0x33edb0c5 0x33edb077 0x33edb055 0x33eda90b 0x33edae01 0x33df9421 0x31fbd6cd 0x31fbb9c1 0x31fbbd17 0x31f2eebd 0x31f2ed49 0x35af62eb 0x33e44301 0xa49d 0x3a103b20)
libc++abi.dylib: terminate called throwing an exception
Does anyone know what's going on here?
Here is my code:
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(editingStyle == UITableViewCellEditingStyleDelete)
{
Caseload *deletedCaseload = [self.caseload objectAtIndex:indexPath.row];
[self.caseload removeObject:deletedCaseload];
[self.caseloadTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
else if
(editingStyle == UITableViewCellEditingStyleInsert)
{
Caseload *copiedEntry = [self.caseload objectAtIndex:indexPath.row];
Caseload *newEntry = [[Caseload alloc]init];
newEntry.name = copiedEntry.name;
newEntry.address = copiedEntry.address;
newEntry.phoneNumber = copiedEntry.phoneNumber;
newEntry.identNumber = copiedEntry.identNumber;
[self.caseload addObject:newEntry];
[self.caseloadTableView insertRowsAtIndexPaths:
[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationRight];
}
}
This types of error generate whenever you try to get object/value of an array at totalIndex + 1 such like for example if your array has 4 value (Make sure index start with 0) and you try to fetch value such like
[Myarray objectAtIndex:4] at that time this type of error occur.
So best why is use breakPoint and find out your problem by help of my suggestion.
The error is "NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]'". That means you're calling "objectAtIndex:" somewhere with an index of 2, and your NSArray only has entries at index 0 and index 1.
You need to figure out where you are setting the bogus index, which I suspect might start in your "numberOfRowsInSection:" method.
I am trying to populate a UITableview with an NSarray.
I'm building an app in which users can search for companies, and the search results are returned by the database in an array. This makes that I can never predict how big this array will be.
when I'm testing the app, it gets stuck if there is only one search result. the error:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** First throw call stack:
(0x1c9a012 0x10d7e7e 0x1c4fb44 0x5268 0xd68fb 0xd69cf 0xbf1bb 0xcfb4b 0x6c2dd 0x10eb6b0 0x2296fc0 0x228b33c 0x228b150 0x22090bc 0x220a227 0x22acb50 0x31edf 0x1c62afe 0x1c62a3d 0x1c407c2 0x1c3ff44 0x1c3fe1b 0x1bf47e3 0x1bf4668 0x1bffc 0x1ddd 0x1d05)
libc++abi.dylib: terminate called throwing an exception
this is what my code looks like:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return companies.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *celltitle=nil;
if([companiesArray count]>1){
celltitle = [[companiesArray objectAtIndex:indexPath.row] objectForKey:#"companyName"];
}
else
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.textLabel.text = [[companiesArray objectAtIndex:indexPath.row] objectForKey: #"companyName"];
cell.detailTextLabel.text = [[companiesArray objectAtIndex:indexPath.row] objectForKey: #"companyCity"];
return cell;
}
}
What should I do to prevent this error, if my array only contains 1 result?
Try this one
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return companiesArray.count;
}
I have a NSFetchedResultController with different section.
I have a crash when I try to search using UISearchDisplayController :
*** Assertion failure in -[UITableViewRowData rectForRow:inSection:], /SourceCache/UIKit/UIKit-2372/UITableViewRowData.m:1630
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request for rect at invalid index path (<NSIndexPath 0x1d2c4120> 2 indexes [0, 1])'
I checked and my search array has indeed two entries (the expected result):
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
It returns 1
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
It returns 2
Funny thing, if I have only one section, it works perfectly.
Help please! :)
Not sure if you figured it out, I would think you did but assuming you are using
[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
in your cellForRowAtIndexPath
do
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
} else {
cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
}
also make sure you are using self.tableView