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;
}
Related
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.searchDisplayController.searchResultsTableView) {
return searchResults.count;
} else {
return self.countOfLocArr.count;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
AtmLocationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if(!cell) {
AtmLocationTableViewCell *cell = [[AtmLocationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
NSMutableDictionary *locDictionary;
if(tableView == self.searchDisplayController.searchResultsTableView) {
locDictionary = searchResults[indexPath.row];
}else{ }
cell.placeName.text = locDictionary[#"name"];
return cell;
}
Getting error
* Assertion failure in -[UISearchResultsTableView _configureCellForDisplay:forIndexPath:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3512.60.7/UITableView.m:7971
2016-12-01 18:21:04.096 Mobee[6459:2145589] * Terminating app due to
uncaught exception 'NSInternalInconsistencyException', reason:
'UITableView (; layer = ; contentOffset:
{0, 0}; contentSize: {320, 396}>) failed to obtain a cell from its
dataSource ()'
*** First throw call stack: (0x20fb9b8b 0x20776dff 0x20fb9a61 0x217a072b 0x256d64a3 0x258d3cb9 0x258d3d91 0x258c326d 0x256673bd
0x2579129f 0x256ec46d 0x257923e3 0x257920bb 0x25791f2d 0x255c2521
0x255c24b1 0x255aa3eb 0x25791c75 0x255ccd9f 0x25760b5b 0x2575feed
0x258e967f 0x25ea5f73 0x2575fc53 0x2598e135 0x2598dd5f 0x255b5355
0x217e6867 0x20f7ba67 0x20f7b657 0x20f799bf 0x20ec8289 0x20ec807d
0x224e4af9 0x255f32c5 0xde303 0x20b74873) libc++abi.dylib: terminating
with uncaught exception of type NSException
Check this :
1) You are returning a number larger than your Array Count from tableView:numberOfRowsInSection:. Don't.
2) One or more of your cell# outlets is not hooked up in your nib, or is not hooked up to a UITableViewCell (or subclass). Hook them up properly.
refer for more :https://stackoverflow.com/a/14192093/3901620
It may happen that cellForRowAtIndexPath is returning nil.
check these: Assertion Failure 1, Assertion Failure 2 and Assertion Failure 3
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 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.
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.
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