Wrong number of objects added to mutable array

I have a view controller with a table view that contains 8 cells (sections). I created a text box and added this text box as a subview in 6 cells, the remaining 2 cells, of which one cell contains a text view and the other with a button. Since I use the same text field, I assigned tag values ​​and then created a modified array, added a text field to this array. Here is the implementation code:

Considering the suggestion given by Mr. Lank, I changed the implementation code

EDIT

- (UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

{

  NSString *identifier = @"UITableViewCell"; UITableViewCell *cell = (UITableViewCell *)[atableView dequeueReusableCellWithIdentifier:identifier]; atableView.backgroundColor = [UIColor clearColor]; textField = [[[UITextField alloc]initWithFrame:CGRectMake(15, 12, 300, 24)]autorelease]; textField.textColor = [UIColor whiteColor]; textField.delegate = self; tagValues = textField.tag; switch (indexPath.section) { case 0: { NSString *cellIdentifierA = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row]; if (cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierA]; cell.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"reminderbutton.png"]]; } textField.placeholder = @"Enter name"; textField.tag = 101; textField.text = reminderInstance.Name; textField.autocorrectionType = UITextAutocorrectionTypeNo; [cell.contentView addSubview:textField]; } break; case 1: { NSString *cellIdentifierB = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row]; if (cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierB]; cell.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"reminderbutton.png"]]; } textField.tag = 102; textField.text = reminderInstance.Event; [cell.contentView addSubview:textField]; } break; case 2: { NSString *cellIdentifierC = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row]; if (cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierC]; cell.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"reminderbutton.png"]]; } textField.placeholder = @"Click here to set date and time"; textField.inputView = self.datePicker; textField.text = reminderInstance.Date; textField.tag = 103; [cell.contentView addSubview:textField]; } break; case 3: { NSString *cellIdentifierD = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row]; if (cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierD]; cell.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"reminderbutton.png"]]; } textField.tag = 105; textField.text = reminderInstance.numDays; textField.inputView = self.reminderPicker; [cell.contentView addSubview:textField]; } break; case 4: { NSString *cellIdentifierE = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row]; if (cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierE]; cell.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"reminderbutton.png"]]; } checkboxButton = [[[UIButton alloc] initWithFrame:CGRectMake(16,1,120, 44)]autorelease]; [checkboxButton setImage:[UIImage imageNamed:@"ewee.png"] forState:UIControlStateNormal]; [checkboxButton addTarget:self action:@selector(toggleButton:) forControlEvents:UIControlEventTouchUpInside]; NSString *one = reminderInstance.selString; NSNumber* i = [NSNumber numberWithInt:[one intValue]]; BOOL isOn = [i boolValue]; if(isOn) { [checkboxButton setImage:[UIImage imageNamed:@"checkarrow.png"] forState:UIControlStateNormal]; } else { [checkboxButton setImage:[UIImage imageNamed:@"ewee.png"] forState:UIControlStateNormal]; } [checkboxButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft]; [checkboxButton setImageEdgeInsets:UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0)]; [cell addSubview:checkboxButton]; UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(55, 10, 225, 24)]; label.text = @"Every Year"; label.textColor = [UIColor whiteColor]; label.backgroundColor = [UIColor clearColor]; [cell addSubview:label]; cell.textLabel.textColor = [UIColor whiteColor]; [label release]; } break; case 5: { NSString *cellIdentifierF = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row]; if (cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierF]; cell.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"reminderbutton.png"]]; } textField.placeholder = @"Enter the number here"; textField.text = num; textField.text = reminderInstance.number; textField.tag = 106; textField.userInteractionEnabled = YES; textField.keyboardType = UIKeyboardTypeNumberPad; [cell.contentView addSubview:textField]; } break; case 6: { NSString *cellIdentifierG = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row]; if (cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierG]; cell.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"reminderbutton.png"]]; } cell.backgroundColor = [UIColor clearColor]; textView.clipsToBounds = YES; textView = [[UITextView alloc]initWithFrame: CGRectMake(-2, -3, 307, 154)]; UIImageView *imgView = [[[UIImageView alloc]initWithFrame: textView.frame]autorelease]; imgView.image = [UIImage imageNamed: @"reminderbuttonxl.png"]; [textView addSubview: imgView]; [textView sendSubviewToBack: imgView]; textView.backgroundColor = [UIColor clearColor]; textView.delegate = self; textView.tag = 11; textView.text = reminderInstance.msgBody; tagValues = textView.tag; textView.textColor = [UIColor whiteColor]; [cell.contentView addSubview:textView]; [textView release]; } break; case 7: { NSString *cellIdentifierH = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row]; if (cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierH]; cell.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"reminderbutton.png"]]; } textField.tag = 107; textField.inputView = self.groupPicker; tagValues = textField.tag; textField.text = reminderInstance.remGroup; [cell.contentView addSubview:textField]; } break; default: break; } [self.fields addObject:textField]; NSLog(@"Fields = %@",fields); return cell; 

}

Now when I try to print an array using the following statement:

NSLog (@ "Fields =% @", fields); In the console window, I could observe the following:

 Fields = ( "<UITextField: 0x9d043a0; frame = (15 12; 300 24); clipsToBounds = YES; opaque = NO; tag = 101; layer = <CALayer: 0x9d044e0>>" ) Fields = ( "<UITextField: 0x9d043a0; frame = (15 12; 300 24); clipsToBounds = YES; opaque = NO; tag = 101; layer = <CALayer: 0x9d044e0>>", "<UITextField: 0x9d07d50; frame = (15 12; 300 24); clipsToBounds = YES; opaque = NO; tag = 102; layer = <CALayer: 0x9d07ca0>>" ) Fields = ( "<UITextField: 0x9d043a0; frame = (15 12; 300 24); clipsToBounds = YES; opaque = NO; tag = 101; layer = <CALayer: 0x9d044e0>>", "<UITextField: 0x9d07d50; frame = (15 12; 300 24); clipsToBounds = YES; opaque = NO; tag = 102; layer = <CALayer: 0x9d07ca0>>", "<UITextField: 0x7108650; frame = (15 12; 300 24); clipsToBounds = YES; opaque = NO; tag = 103; layer = <CALayer: 0x7108620>>" ) Fields = ( "<UITextField: 0x9d043a0; frame = (15 12; 300 24); clipsToBounds = YES; opaque = NO; tag = 101; layer = <CALayer: 0x9d044e0>>", "<UITextField: 0x9d07d50; frame = (15 12; 300 24); clipsToBounds = YES; opaque = NO; tag = 102; layer = <CALayer: 0x9d07ca0>>", "<UITextField: 0x7108650; frame = (15 12; 300 24); clipsToBounds = YES; opaque = NO; tag = 103; layer = <CALayer: 0x7108620>>", "<UITextField: 0x5c7bf90; frame = (15 12; 300 24); clipsToBounds = YES; opaque = NO; tag = 105; layer = <CALayer: 0x5c7c920>>" ) Fields = ( "<UITextField: 0x9d043a0; frame = (15 12; 300 24); clipsToBounds = YES; opaque = NO; tag = 101; layer = <CALayer: 0x9d044e0>>", "<UITextField: 0x9d07d50; frame = (15 12; 300 24); clipsToBounds = YES; opaque = NO; tag = 102; layer = <CALayer: 0x9d07ca0>>", "<UITextField: 0x7108650; frame = (15 12; 300 24); clipsToBounds = YES; opaque = NO; tag = 103; layer = <CALayer: 0x7108620>>", "<UITextField: 0x5c7bf90; frame = (15 12; 300 24); clipsToBounds = YES; opaque = NO; tag = 105; layer = <CALayer: 0x5c7c920>>", "<UITextField: 0x9d08b20; frame = (15 12; 300 24); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x9d08af0>>" ) 

Now, as can be seen from the above o / p, the number of fields that are printed is 5, i.e. 4 text fields and a cell with a button. But according to our coding in -CellForRowAtIndexPath, 6 cells contain a text field, therefore, the number of fields should be 6, but we get the wrong result, i.e. 5 (4 text fields + button), which is an error.

It also leads me to save data in a table view issue, i.e. for the right button of the navigation bar, the "Save" item in my case, I implemented:

 -(IBAction)save:(id)sender { //Code for saving entered data using sqlite UITextField *fieldOne = [self.fields objectAtIndex:0]; UITextField *fieldTwo = [self.fields objectAtIndex:1]; UITextField *fieldThree = [self.fields objectAtIndex:2]; UITextField *fieldFour = [self.fields objectAtIndex:3]; UITextField *fieldFive = [self.fields objectAtIndex:5]; UITextField *fieldSix = [self.fields objectAtIndex:7]; sqlite3_stmt *statement = nil; const char *dbpath = [databasePath UTF8String]; if (sqlite3_open(dbpath, &remindersDB) == SQLITE_OK && textField.text != nil) { if (self.navigationItem.rightBarButtonItem.title == @"Save") { NSLog(@"am in the save loop"); NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO reminders(name,event,date,bfr,val,num,bod,grp) VALUES (\"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\")", fieldOne.text, fieldTwo.text,fieldThree.text,fieldFour.text,isSelected,fieldFive.text,textView.text,fieldSix.text]; const char *insert_stmt = [insertSQL UTF8String]; sqlite3_prepare_v2(remindersDB, insert_stmt, -1, &statement, NULL); if (sqlite3_step(statement) == SQLITE_DONE) { UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"\nReminder Saved" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil,nil]; [alert show]; [alert dismissWithClickedButtonIndex:0 animated:YES]; [alert release]; } else { UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Reminder not saved" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; [alert release]; } } //Check the last inserted row id rowID = sqlite3_last_insert_rowid(remindersDB); NSLog(@"last inserted rowId = %d",rowID); sqlite3_finalize(statement); sqlite3_close(remindersDB); [self.tableView reloadData]; } } 

Now the problem is related to the location values ​​of the index, since the number of values ​​(fields) in which the "fields" array is located is 5, the index values ​​will be 0-4, but if I give the values ​​accordingly, i.e. while saving, I would get the field smaller when inserting data into sqlite database. I also get zero values ​​in the 5th and 7th places of the table view when printing in the console. In the above implementation fragment, we can notice that I inserted using the location of the index in relation to the value of the table sections (indexes).

When I save according to the index values ​​of a table view section index, the values ​​print correctly. But I get a crashing problem when I do not touch the 5th and 7th locations of the table view.ie '*** - [NSMutableArray objectAtIndex:]: index 5 is out of bounds [0 .. 4]'

I don’t understand the reason why only 5 field values ​​are inserted into the array, not 6. Can someone please direct me in the right direction.

Sorry for this huge post, I wanted to be informative, so I made it more detailed so that I could easily answer the question.

EDIT

Code for the number of sections and rows to represent the table:

 #pragma mark - #pragma mark TableView Datasource - (NSInteger)numberOfSectionsInTableView:(UITableView *)atableView { return 8; } - (NSInteger)tableView:(UITableView *)atableView numberOfRowsInSection:(NSInteger)section { int arr = 1; return arr; } 

Implementing height for a string along a pointer path:

 - (CGFloat)tableView:(UITableView *)atableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { float f; if (indexPath.section==6&&indexPath.row==0) { f=140.0f; return f; } else { f = 44.0f; return f; } } 

All table view cells do not load until scrolling. But when I scroll, the control reaches the 5th, 6th and 7th cells. I found out that the reason the text box in the 5th and 7th cells is not added to the "fields".

I was looking for a solution, mainly this type posts

Thanks to everyone in advance :)

+3
source share
2 answers

I found a solution to the issue here

Thanks to everyone who reviewed the issue.

0
source

Have you tried scrolling a table? I

If you added this code inside if( cell == nil ) , then it may not create all the text fields, since the table cells are reused. Better you have different cell IDs for all 8 cells. This will solve your problem.

+1
source

All Articles