iOS Populate table with mutablearray data - ios

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

Related

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

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.

Error with detailTextLabel from NSArray in UITableView

I'm creating a Table View with the Detail Right style of cells (prototype cells). In implementing the self.detailLabelText.text on an array I have I'm receiving an error message when I attempt to load the table. Below is the code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:simpleTableIdentifier];
}
switch (pickerSelection)
{
case 0:
cell.textLabel.text = [visitList1 objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [windows1 objectAtIndex:indexPath.row];
break;
case 1:
cell.textLabel.text = [visitList2 objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [windows2 objectAtIndex:indexPath.row];
break;
}
return cell;
}
I'm receiving the error message:
Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayI objectAtIndex:]: index 2 beyond bounds [0 .. 1]'
The table loads correctly with the main textLabel, and I can load a string after cell.detailTextLabel = so I know that my setup is correct. I also can NSLog both of the arrays I'm trying to load, just not in the detailLabelText.
I know the error message has to due with the expected length of an array, I just can't figure out what or where. Sorry for an elementary question. I'm a noob.
UPDATE: Here is my numberOfRowsInSelection method:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (pickerSelection)
{
case 0:
return [visitList1 count];
return [windows1 count];
break;
case 1:
return [visitList2 count];
return [windows2 count];
break;
}
return 10;
}
Each of the arrays have over 3 items. i threw that last "return 10;" in there just in case something wasn't working right with the switch statement, but it doesn't seem to matter.
Update 2: When I replace my windows1 array with static strings they populate in the detail area of the table. That's great (although I now seem to have some sort of problem with my original window arrays).
Either the windows1 array or windows2 array only has 2 items in it, and your table datasource is telling it there are 3 rows to display. As Kevin asked, edit your question with the -tableView:numberOfRowsInSection: method you implemented, as the problem is almost certainly in there.

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

table view controller cs193p iTunesU 9 lesson

Hi I'm studying a 9TH lesson of iTunesU CS193P about table view and compiler report me this error
NSInternalInconsistencyException', reason: '-[__NSCFArray removeObjectAtIndex:]: mutating method sent to immutable object'
my simulator is iPad 6.1
so I have
a class called GraphViewController.m
#define FAVORITE_KEY #"GraphViewController.Favorite"
- (IBAction)addFavorite:(id)sender
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *favorites = [[defaults objectForKey:FAVORITE_KEY] mutableCopy];
if (!favorites) favorites = [NSMutableArray array];
[favorites addObject:self.program];
// NSLog(#"contenuto favorites %#",favorites);
[defaults setObject:favorites forKey:FAVORITE_KEY];
[defaults synchronize];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"GraphTableView"]) {
NSArray *program = [[NSUserDefaults standardUserDefaults]objectForKey:FAVORITE_KEY];
[segue.destinationViewController setPrograms:program];
}
}
(setPrograms is the setter where i have the data to send at my tableviewcontroller called CalculatorTVC.m)
a class called CalculatorTVC.m
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.programs count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cellTable";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
id program = [self.programs objectAtIndex:indexPath.row];
cell.textLabel.text = [#"y = " stringByAppendingString:[CalculatorBrain descriptionProgram:program]];
NSLog(#"contenuto di program %#",program);
return cell;
}
(programs is a public property where I put the data from GraphViewController.m)
In my storyboard I have a split view ...in MasterViewController i have toolbar with bar button item wired with table view controller (CalculatorTVC.m) in popover style identifier is GraphTableView and i have a round rect button that is addFavorite describe here up
the error is
* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray removeObjectAtIndex:]: mutating method sent to immutable object'
* First throw call stack:
(0x1ca1012 0x10dee7e 0x1ca0deb 0x1d21c4f 0x1d21911 0x4a5b 0x8f65 0xdd8fb 0xdd9cf 0xc61bb 0xd6b4b 0x732dd 0x10f26b0 0x229dfc0 0x229233c 0x22a0238 0x6b6e3 0x4f1476 0x870989a 0x4f2555 0x489ef9 0x46ab99 0x46ac14 0x10f2705 0x262c0 0x262a64 0x10f2705 0x262c0 0x26258 0xe7021 0xe757f 0xe66e8 0x55cef 0x55f02 0x33d4a 0x25698 0x1bfcdf9 0x1bfcad0 0x1c16bf5 0x1c16962 0x1c47bb6 0x1c46f44 0x1c46e1b 0x1bfb7e3 0x1bfb668 0x22ffc 0x24bd 0x23e5)
libc++abi.dylib: terminate called throwing an exception
(lldb)
please help me
thanks for your patience
regards
ok I find where it crashes....
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cellTable";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
id program = [self.programs objectAtIndex:indexPath.row];
cell.textLabel.text = [#"y = " stringByAppendingString:[CalculatorBrain descriptionProgram:program]];
NSLog(#"contenuto di program %#",program);
return cell;
}
the crash in the line
cell.textLabel.text = [#"y = " stringByAppendingString:[CalculatorBrain descriptionProgram:program]];
if I put NSLOG before this line i see in the output NSLOG result...but if I put NSLOG after this I don't see anything in the output
NSArray *program = [[NSUserDefaults standardUserDefaults]objectForKey:FAVORITE_KEY];
[segue.destinationViewController setPrograms:program];
Here you're passing an immutable array to the destination view controller. If it is expecting a mutable array and tries to modify it, you'll get that crash. You need mutableCopy here as well.
If the property should be a mutable array, you should get compiler warnings. Don't ignore these!
You're crashing on remove, not add, by the way, so you haven't included the right code in your question. Enable exception breakpoints to find the line you're crashing on.

Crash when searching in two section

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

Resources