Calling uitableview while clicking uitextfield-Data not displaying -iOS - ios

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

Related

UITableView is crashing on NSArray index 1 beyond bounds

My table keeps crashing. "Index 1 beyond bounds".
I return the an array count for the number of rows in the section, but as soon as I scroll to the second row (index 1) the app crashes.
I have two sections. One with a set number of rows and the second section is a dynamically created based upon the number of items in the array.
Any thoughts?
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2; //two seperate areas
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return 16;
} else {
NSLog(#"Section: %ld, NumberOfRows: %lu", (long)section, (unsigned long)[self.arrImages count]);
return [self.arrImages count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
switch (section) {
case 0:
cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
break;
case 1:
cell = [tableView dequeueReusableCellWithIdentifier:#"cellPhoto"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cellPhoto"];
}
NSLog(#"Row: %lu\n%#", (unsigned long)row, [[self.arrImages objectAtIndex:row] objectForKey:#"imageThumbnailURL"]);
cell.imageView.image = [self imageFromURL:[[self.arrImages objectAtIndex:row] objectForKey:#"imageThumbnailURL"] fromCache:YES];
NSLog(#"completed");
break;
}
return cell;
}
#pragma mark UITableViewDelegate methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger section = [indexPath section];
[self doneData];
switch (section) {
case 1:
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[self performSegueWithIdentifier:#"segue_showphoto" sender:self];
break;
}
}
Here is my crash log
2015-02-12 19:32:51.817 Tops[17236:5681531] Row: 0
https://somethinghere.jpeg
2015-02-12 19:32:51.825 Tops[17236:5681531] completed
2015-02-12 19:32:51.855 Tops[17236:5681531] Row: 1
https://somthinghere.jpeg
2015-02-12 19:32:51.856 Tops[17236:5681531] completed
2015-02-12 19:32:51.862 Tops[17236:5681531] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
The implementation I was missing was:
-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath {
return 0;
}

Nested UITableView in a UITableView Causes NsRangeException with Index Out of Bound in iOS

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

TableView Crashes when Trying to Insert

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.

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 5 beyond bounds [0 .. 4]' in IOS

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
data_web *current_data ;
current_data = [[structured_question data_webs] objectAtIndex:indexPath.row];
NSString *is_submited = [NSString stringWithFormat:#"%#",[current_data content_2]];
if ([is_submited compare:#"Y"] == NSOrderedSame)
cell.accessoryType = UITableViewCellAccessoryCheckmark;
else
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
Above is my script. something is wrong in my script and I cannot find a solution.
In the comment you provided the code for your numberOfRowsInSection method as follows:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(section == 0)
return [[testM data_webs] count];
else
return [[testS data_webs] count];
}
However, your willDisplayCell ignores indexPath.section when you access current_data. When the cell from a section that has more rows is displayed, your code will crash.
You should change your code to something like this:
NSArray *data_webs;
// This part mimics your "numberOfRowsInSection'
if (indexPath.section == 0) {
data_webs = [testM data_webs];
} else {
data_webs = [testS data_webs];
}
data_web *current_data ;
current_data = [data_webs objectAtIndex:indexPath.row];
I do not know what's structured_question in your code was, and why you were using it, but the above should eliminate the crash in the objectAtIndex: method.

iOS array count returning error

I am trying to count my array to set as the number of rows in my table and then multiply it by 2 because I have an invisible cell in between each cell to make the cells look separated. Every time I try to build, I get this error:
Terminating app due to uncaught exception 'NSRangeException', reason:
'* -[__NSArrayM objectAtIndex:]: index 3 beyond bounds [0 .. 2]'
This is the code I am using:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [xmlParser.calls count] * 2;
}
cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
static NSString *CELL_ID2 = #"SOME_STUPID_ID2";
JointCAD *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row];
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"texture3.png"]];
[self.tableView setSeparatorColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"texture3.png"]]];
if (indexPath.row % 2 == 1) {
UITableViewCell * cell2 = [tableView dequeueReusableCellWithIdentifier:CELL_ID2];
if (cell2 == nil) {
cell2 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CELL_ID2];
[cell2.contentView setAlpha:0];
[cell2 setUserInteractionEnabled:NO];
[cell2 setBackgroundColor:[UIColor clearColor]];
}
return cell2;
}
TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.callTypeLabel.text = currentCall.currentCallType;
cell.locationLabel.text = currentCall.location;
cell.unitsLabel.text = currentCall.units;
cell.stationLabel.text = [#"Station: " stringByAppendingString:currentCall.station];
cell.contentView.layer.backgroundColor = [UIColor whiteColor].CGColor;
cell.contentView.layer.frame = CGRectMake(0, 0, cell.contentView.frame.size.width, cell.contentView.frame.size.height);
cell.contentView.layer.borderWidth = 1.0;
cell.contentView.layer.borderColor = [UIColor blackColor].CGColor;
if ([currentCall.county isEqualToString:#"W"]) {
cell.countyImageView.image = [UIImage imageNamed:#"blue.png"];
} else {
cell.countyImageView.image = [UIImage imageNamed:#"green.png"];
}
if ([currentCall.callType isEqualToString:#"F"]) {
cell.callTypeImageView.image = [UIImage imageNamed:#"red.png"];
} else {
cell.callTypeImageView.image = [UIImage imageNamed:#"yellow.png"];
}
return cell;
}
EDIT:
Made a closer look. Just add the /2:
JointCAD *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row / 2];
And it's going to work.
Just noticed the same answer below from richard.
As mayuur noticed:
in - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section:
// n = [xmlParser.calls count]
// Ok, we have n*2 items (but calls has only n!):
return [xmlParser.calls count] * 2;
// calls has n items in it, but you return n*2 items
And then in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath:
// n = [xmlParser.calls count]
// give me object from calls at index (which will be in range [0; n*2 - 1],
// which could be beyond [0; n-1] which you originally have)
JointCAD *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row];
So you should remove multiply by 2 or suggest something by your own, you'll always going to get this error unless xmlParser.calls is an empty array.
The error occured as you're doing something like cell.textLabel.text = [array objectAtIndex:indexPath.row]; it over bounds it limits. However of your requirement to left invisible (blank) cells in between, you can set some if...else conditions in UITableView datasource cellForRowAtIndexPath method, that prevents limit of array. Also set proper return count for numberOfCells.
Something like, if you've 4 values in your array. So there'll be 3 invisible cells should made.
1st Cell
blank
2nd Cell
blank
3rd Cell
blank
4th Cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [xmlParser.calls count] + (([xmlParser.calls count]%2)+1);
}
In your cellForRowAtIndexPath method,
do condition like,
if(indexPath.row==0)
{
cell.textLabel.text = [array objectAtIndex:indexPath.row];
}
else if((indexPath.row+1)%2!=0)
{
cell.textLabel.text = [array objectAtIndex:indexPath.row];
}
else
{
NSLog(#"%d is blank cell",indexPath.row);
}
You are multiplying the array count, no big deal
return [xmlParser.calls count] * 2;
but you should then divide the indexPath.row by to 2 to get the real index of the datasource. The table assumes that you have a datasource with the size 2n, so the table will access 0 .. 2n using the indexpath.
JointCAD *currentCall = [[xmlParser calls] objectAtIndex:(indexPath.row/2)]

Resources