My app crashes when I try to insert a new entry. I received this error previously, and I was not able to fix it. Can anyone help?
Here is my error displayed in the console:
2013-09-14 15:41:00.370 Probation App[9919:907] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]'
*** First throw call stack:
(0x31fe82a3 0x39ccc97f 0x31f33b75 0xd7b81 0x33eb228d 0x33f34f81 0x328f6277 0x31fbd5df 0x31fbd291 0x31fbbf01 0x31f2eebd 0x31f2ed49 0x35af62eb 0x33e44301 0x542fd 0x3a103b20)
libc++abi.dylib: terminate called throwing an exception
and here is my code:
#pragma mark UITableViewDataSource Methods
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:#"cell"];
if( nil == cell)
{
cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier:#"cell"];
}
if (indexPath.row <self.probationers.count)
{
Probationer *thisProbationer = [self.probationers objectAtIndex:indexPath.row];
cell.textLabel.text = thisProbationer.probationerName;
cell.detailTextLabel.text = thisProbationer.probationerID;
}
else
{
cell.textLabel.text = #"Add Probationer";
cell.textLabel.textColor = [UIColor lightGrayColor];
cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
-(NSInteger)tableView: (UITableView *)tv numberOfRowsInSection:(NSInteger)section
{
NSInteger count = self.probationers.count;
if(self.editing)
{
count = count +1;
}
return count;
//return [self.probationers count];
}
//Deleting an Entry
-(void) tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle) editing forRowAtIndexPath: (NSIndexPath *) indexPath
{
if (editing == UITableViewCellEditingStyleDelete)
{
[self.probationers removeObjectAtIndex:indexPath.row];
[tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
}
#pragma mark UITableViewDelegate Methods
-(void)tableView:(UITableView *)tv didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Probationer *chosenProbationer = [self.probationers objectAtIndex:indexPath.row];
ProbationerController *detailedViewController = [[ProbationerController alloc]init];
detailedViewController.delegate = self;
detailedViewController.currentProbationer = chosenProbationer;
if (indexPath.row <self.probationers.count && !self.editing)
{
[self.navigationController pushViewController:detailedViewController animated:YES];
}
if (indexPath.row == self.probationers.count && self.editing)
{
AddProbationerController *addProbationer = [[AddProbationerController alloc] init];
[self.navigationController pushViewController:addProbationer animated:YES];
}
[tv deselectRowAtIndexPath:indexPath animated:YES];
//selectedIndexPath = indexPath;
//[self.navigationController pushViewController:detailedViewController animated:YES];
}
The number of rows should always come from the data source (the bit you have commented out //return [self.probationers count];). Don't try to just add to the number. Add to the data source and then refresh the table view.
The stack trace say it all. Its definitely because of you self.probationers array.
Why don't you NSLog the number of items in the array and the number of rows/sections in your tableview.
When you are trying to associate indexPath.row and self.probationers you have to make sure they match with number of elements.
Also follow the basics while accessing elements of array(as they are much prone to get exceptions)
Nil check for the accessing array.
Know the exact count of the array, by logging.
Make sure you are trying to access any objects more than the available array indexes.
Related
I have an iOS 7/8 application. In a view I have a static UITableView with given number of cells - 17 in my case.
One of the cells contains another UITableView with dynamic cells. In the case described they are 20.
Because of the difference in the number of cells (+3) I get
NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 17 beyond bounds [0 .. 16]
exception when I set the 18th cell in the dynamic view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == _dynamicTableView) {
NSLog(#"%lu", (unsigned long)[[_filter types] count]);
return [[_filter types] count];
} else {
return 17;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == _dynamicTableView) {
static NSString *cellIdentifier = #"type";
TypeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[TypeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier];
}
NSArray *types = [_filter types];
Type *type = [types objectAtIndex:[indexPath row]];
[cell.nameLabel setText:[type name]];
return cell;
} else {
return [super tableView:tableView cellForRowAtIndexPath:indexPath];
}
}
When I go to the Storyboard and increase the number of static cells to something like 30 everything works fine. tableView:numberOfRowsInSection... method removes the unused cells - only 17 static and 20 dynamic cells are shown.
I am aware that the source of my problem is having two UITableView-s in one controller and the large amount of 'if'-s.
Try to check with tags, you can give different tags to each table and return number of cells according to that.
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView.tag == 1) // Tag for _dynamicTableView
{
return [[_filter types] count];
}
else {
return 17;
}
}
I have tableview with searchDisplayController. When I search for some text I successfully display the results on screen. My problem arises when I want to add a cell to existing results. Adding a cell must be done when searchDisplayController is active.
That is:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSMutableDictionary * dict ;
BOOL isSearchView = tableView == self.searchDisplayController.searchResultsTableView;
if (isSearchView) {
dict = [searchTasks objectAtIndex:indexPath.row];
[self.searchDisplayController.searchResultsTableView deselectRowAtIndexPath:indexPath
animated:YES];
}
else{
dict = [self.tasks objectAtIndex:indexPath.row];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Task * task = [self getsomeTask];
NSIndexPath * ip = [NSIndexPath indexPathForRow:indexPath.row+1
inSection:indexPath.section];
if (isSearchView) {
[searchTasks insertObject:task atIndex:ip.row];
[self.searchDisplayController.searchResultsTableView
insertRowsAtIndexPaths:#[ip]
withRowAnimation:UITableViewRowAnimationRight];
}
after this is executed, and after the last line insertRowsAtIndexPaths:#[ip],
the code executes:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.searchDisplayController.isActive) {
return [searchTasks count];
} else {
return [tasks count];
}
}
Which is fine and here it selects the first option which is true according to program logic. But the it crashed and after this execution it never calls
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
delegate function of UITableview.
And gives me error:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
I cannot understand the reason why it does not call the cellForRowAtIndexPath function after rowcount function.
Any suggestions?
I want to display data in uitableview when the particular uitextfield is tapped. I am not getting the data in taleview. Please check the below and help me.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier ];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[cell.textLabel setFont:[UIFont fontWithName:#"Bold" size:20]];
if (ethnicityField.tag == 1)
{
cell.textLabel.text = [data objectAtIndex:indexPath.row];
}
if (languageField.tag == 2)
{
cell.textLabel.text = [langData objectAtIndex:indexPath.row];
}
Here, when I click on the first textfield it shows the empty table and crashes.
It's not taking the exact tag value. Please help.
- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section{
int row = 0;
if (ethnicityField.tag == 1)
{
row = [data count];
}
if (languageField.tag == 2)
{
row = [langData count];
}
return row;
}
Crash report:
Actually I have 2 arrays, one with 5 values and another with 11 values, the problem is both textfield takes first array and if I select the 5th object in second textfield it shows the following error.
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 9 beyond bounds [0 .. 4]'
I believe ethnicityField and languageField always have the same tag so numberOfRowsInSection always returns [data count];
What you could do is checking whether a textfield is active when you display your table.
- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section{
int row = 0;
if ([ethnicityField isFirstResponder])
{
row = [data count];
}
else if ([languageField isFirstResponder])
{
row = [langData count];
}
return row;
}
I'm trying to add custom cells to a static UITableViewController. I use SWTableViewCell to get the swipable cell and I'm using a custom -xib for the cell layout.
I seems to work fine when I only need to use one cell, but when there are more then one I get this: Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
This happens when I use dequeueReusableCellWithIdentifier in the cellForRowAtIndexPath when the second cell should appear (when I begin to scroll).
Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
self.images = selectedReport.image;
imagesArray = [self.images.allObjects mutableCopy];
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
[self.tableView registerNib:[UINib nibWithNibName:#"PictureCell" bundle:nil] forCellReuseIdentifier:#"PictureCell"];
}
- (NSInteger) tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger) section
{
if(section == 0){
return 10;
}else if(section == 1){
return imagesArray.count; //It is the second section which is dynamic
}
return 1;
}
- (UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath
{
static NSString* cellIdentifier = #"PictureCell";
if (indexPath.section == 0 || indexPath.section == 2)
{ //Takes the rows from the storyboard
return [super tableView:tableView cellForRowAtIndexPath:indexPath];
}
else
{
NSLog(#"%i",indexPath.row);
NSLog(#"%i",indexPath.section);
PictureTableViewCell *cell = (PictureTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; //Crash here when indexPath.row == 1
[cell setCellHeight:cell.frame.size.height];
cell.containingTableView = tableView;
indexPath.section, indexPath.row];
Image *tempImage = imagesArray[indexPath.row];
cell.image.image = [UIImage imageWithData:tempImage.image];
cell.lblDescription.text = tempImage.image_description;
cell.rightUtilityButtons = [self rightButtons];
cell.delegate = self;
return cell;
}
}
I have checked that numberOfRowsInSection returns 2 when section == 1.
Thank you for any assistance!
Use this instead
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
or in viewDidLoad, register the class like
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"PictureCell"];
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"PictureCell" forIndexPath:indexPath];
}
I have a editable UITableView (with adding and deleting elements) in my application.
It working strange.
While I have only one or two lines in it, everything is working perfect.
But if I add more items, I have an exception '*** -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]'
I was unable to put breakpoints and handle it.
Maybe, somebody can help me?
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int plusRow = 0;
if ([[self tableView] isEditing]) plusRow = 1;
return [typeList count] + plusRow;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if ([indexPath row] > ([typeList count]-1)) {
NSString * appendCellDescription = #"";
if ([[self tableView] isEditing]) appendCellDescription = #"Add";
[[cell textLabel] setText:appendCellDescription];
} else {
NSLog(#"Accessing array: %d", [indexPath row]);
[[cell textLabel] setText:[[typeList getObject:[indexPath row]] description]];
}
return cell;
}
#pragma mark - Editing table view
- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(#"Row: %d, count: %d", [indexPath row], [typeList count]);
if (indexPath.row > [typeList count]-1) {
return UITableViewCellEditingStyleInsert;
} else {
return UITableViewCellEditingStyleDelete;
}
}
- (IBAction)btnModifyClick:(id)sender{
if ([[self tableView] isEditing]) {
[[self tableView] setEditing:FALSE animated:YES];
[[self tableView] reloadData];
} else {
[[self tableView] setEditing:TRUE animated:YES];
[[self tableView] reloadData];
}
}
Generally This Error describe, When mistake in Array Index such like you array have 3 item and you tring to get/abstract 4th item from Array, at that time this type of Error generate.
Best way to find error use BreakPint. and Debug your project line by line. and find where your app. crush ?? i am sure that it cruse Around any array.
EDIT:
I thinks mistake in array name typeList check it in cellForRowAtIndexPath method with BreakPoint.
numberOfRowsInSectionis returning higher number of items count than the one fetched in cellForRowAtIndexPath datasource method, try debugging the returned value:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int plusRow = 0;
if ([[self tableView] isEditing]) plusRow = 1;
NSLog(#"%i",[typeList count] + plusRow);
return [typeList count] + plusRow;
}
I have found a way, to solve my problem.
I just changed "Sections" to 0 in XCode properties in storyboard TableView.
And now working ok.
Thanks to everyone to response!