Crash when searching in two section - ios

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

Related

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.

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

Custom UITableViewCells Alloc Problrm

So I have custom UITableViewCells (made with storyboard), and when I do not literally alloc, init the cells, the app crashes. However, since they are custom cells, I don't know how to alloc/init them.
This code crashes:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TableViewCells *cell = (TableViewCells *)[tableView dequeueReusableCellWithIdentifier:#"TableViewCellsID"];
return cell;
}
with this printed to the console:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
I understand this means its crashing because there is no cell allocated yet, however when I try to allocate it, I have to specify a cell style.
So, when I build the cell as follows, it doesn't crash:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TableViewCells *cell = (TableViewCells *)[tableView dequeueReusableCellWithIdentifier:#"TableViewCellsID"];
if (cell == nil)
cell = [[TableViewCells alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"TableViewCellsID"];
return cell;
}
The problem is the code that works (the second set), requires me to set a UITableViewStyle. And as far as I know, there is no UITableViewStyleCustom.
Any helps would be appreciated.
It crashes in first case because there is no cell to reuse, and you are calling dequeueReusableCellWithIdentifier.
EDIT -
For custom cell initialisation you can check following threads -
How to make custom TableViewCell with initWithStyle after 3.0
initWithFrame vs initWithStyle

Resources