index 1 beyond bounds [0 .. 0] Crashing while scrolls down - ios

I have five sections in my tableview.In my (section==4) I displaying from my array.When I scrolls down it will be crashing
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
my code:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (section == 4) {
return [mandArray count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 4) {
[self.label setNumberOfLines:10];
[self.label setText:[NSString stringWithFormat:#"%i. %#",indexPath.row + 1,[mandArray objectAtIndex:indexPath.row]]];
[cell.contentView addSubview:label];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section==4){
return [mandArray count] *20;
}
}

The code mentioned is correct. I think you are modifying the mandArray while table is being loaded.
The procedure which i follow to load a table from array is like this.
First initialise the array and load contents into array.
Then load table. Also I add row count delegate based on array count.
**If I change the contents of array then I call [tableview reload]**so changes will be reflected in the table also.
I think you are modifying the table while it is loaded and not calling reload method. because row count delegate method is called only once per load of tableview. And it uses same data and tries to access data based on count. So to update this call reload method.

Related

What causes outOfBounds error in cellForRowAtIndexPath?

I'm having the following issue raised by Crashlytics :
[__NSArrayM objectAtIndexedSubscript:]: index 5 beyond bounds for empty array
-TopicListViewController tableView:cellForRowAtIndexPath:]
While accessing the dataSource with indexPath.row.
We have some asynchronous data update updating the datasource, and that variable is nonatomic.
Would it be possible that cellForRowAtIndexPath is called while the dataSource is being updated? Hence causing to access an index that doesn't exist anymore?
Can it be because the variable is nonatomic?
Here's the relevant code :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row > [self.tableData count] - 1 || ![self.tableData isValidArray]) {
return nil; //Some protection to prevent this issue...
}
TopicCell * cell = (TopicCell *)[tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
cell.delegate = self;
NSDictionary * data = nil;
if (self.we_isSearching) {
data = self.we_searchResult[indexPath.row];
} else {
data = [self.tableData objectAtIndex:indexPath.row]; //Crashes here
}
"index 5 beyond bounds for empty array" simply states that either you didn't initialise your array or the range of the value you're accessing is out of the range of the array. You are trying to access index 5 in an empty/having less element array, or non initialised array that's why it is giving you "outOfBounds" in cellForRowAtIndexPath.
Would it be possible that cellForRowAtIndexPath is called while the dataSource is being updated?
Yes, cellForRowAtIndexPath will always be called when you're going to see a new tableview cell for example when you're scrolling the tableview Or in case you've added some kind of notification added to your datasource or by reloading the tableview.
You can put a break point at cellForRowAtIndexPath and check the stack trace maybe you get something that causes the tableview to reload.
Try to count from self.tableData in return of numberOfRowsInSection methods. Like
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.tableData count];
}
pass array count in numberOfRowsInSection of tableView method.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return array.count;
}
Your condition
if (indexPath.row > [self.tableData count] - 1 || ![self.tableData isValidArray])
is wrong. If there is 5 elements, the last indexPath.row will be index 4 so condition with real values will be:
if (4 > 5 - 1) --> if 4 > 4
So the valid condition is:
if (indexPath.row >= [self.tableData count] - 1)
But with correct condition you will have crash on:
return nil
Because obvisously your data source is different than table data source. Your model data source should be always same as table data source.

iOS Populate table with mutablearray data

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;
}

Deleting the last item in an array and UITableView

I have a tableView with one section in it which contains an array of values. Here is what the numberOfRowsInSection looks like
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return [allPasses count];
}
When you delete a row this code runs
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[allPasses removeObjectAtIndex:indexPath.row];
// Delete the row from the data source
PassWhizEntity *passToRemove = self.allPasses[indexPath.row];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
// Get the local context
NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
[passToRemove MR_deleteInContext:localContext];
[localContext MR_save];
}
This works great and you can delete any rows but when you delete the last row the app crashes and you get this error.
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
But I don't know how to fix this. I am sure its quite a simple fix but I have tried adding one to the array and I'm not sure what else to do. Any ideas would be appreciated!
It doesn't work properly because these lines are in the wrong order:
[allPasses removeObjectAtIndex:indexPath.row];
// Delete the row from the data source
PassWhizEntity *passToRemove = self.allPasses[indexPath.row];
all you have at the moment is a hidden issue until you get to the last item.
Reverse the order of those lines so that you get the item to delete before you remove it from the array.

cellForRowAtIndexPath - objectAtIndex beyond bounds

I'm having a little trouble with a tableView.
In this case, I have 4 sections.
Sections 0,1,2 have 1 cell.
Section 3 has 2 cells.
I'm now receiving the error,
Terminating app due to uncaught exception 'NSRangeException', reason:
'-[__NSCFArray objectAtIndex:]: index (1) beyond bounds (1)'
Now I understand this is trying to reference a cell that isn't within the section?
Where I'm struggling is the count for each section is correct, like below.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(#"Sections: %d", [_timetableDataArray count]);
return [_timetableDataArray count];
}
This returns, as expected,
Sections: 4
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSDictionary *sectionContents = [_timetableDataArray objectAtIndex: section];
NSLog(#"Section %d contains %d items", section, [sectionContents count]);
return [sectionContents count];
}
The log for the above returns,
Section 3 contains 2 items
Section 0 contains 1 items
Section 1 contains 1 items
Section 2 contains 1 items
This is absolutely correct.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Iterating --- section: %d row: %d", indexPath.section, indexPath.row);
}
The above method never even gets to NSLog. It crashes before this.
If I change my data array, so each section has 2 items, everything works fine.
Here is my data, http://pastebin.com/q5rbsvC8
I just don't understand where this crash is coming from when numberOfRowsInSection is returning the right number of rows per section.
I originally thought it might be a problem with 0-index's, but I changed these to start from 1 and it didn't help.
Any help/advice is greatly appreciated.
Thanks,
Adrian
Try to NSLog for section in - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section before
NSDictionary *sectionContents = [_timetableDataArray objectAtIndex: section];
to see if any unexpected values of section are coming in?

Error populating TableView with array: index 1 beyond bounds [0 .. 0]

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;
}

Resources