添加到可变数组的对象数量不正确
我有一个带有表视图的视图控制器,其中包含 8 个单元格(部分)。我创建了一个文本字段,并将该文本字段作为子视图添加到 6 个单元格中,其余 2 个单元格,其中一个单元格包含文本视图,另一个单元格包含按钮因为我使用相同的文本字段,所以我分配了标签值,然后创建了一个可变数组,将文本字段添加到该数组中。这是实现代码:
考虑到 Mr.Lanc 给出的建议,我已经改变了实现代码
编辑
- (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;
}
现在,当我尝试使用以下语句打印数组时:
NSLog(@"Fields = %@",fields);我可以在控制台窗口中观察到以下内容:
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>>"
)
现在,我们可以从上面的 o/p 中观察到,要打印的字段数量为 5,即 4 个文本字段和带有按钮的单元格,但是根据我们在 -CellForRowAtIndexPath,6 中的编码单元格包含文本字段,因此字段数必须为 6,但我们得到错误的结果,即 5(4 个文本字段+按钮),这是一个错误。
这也导致我在表视图问题中保存数据,即在我的例子中,对于标题为“保存”的右侧导航栏按钮项目,我已经实现:
-(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];
}
}
现在问题在于索引位置值,因为值(字段)的数量)“fields”数组保存的是5,索引值将为0-4,但是如果我相应地给出值,即在保存时,我会在插入数据时得到一个更少的字段sqlite 数据库,我也得到空值在控制台中打印时,在表视图的第 5 个和第 7 个位置中。在上面的实现片段中,我们可以观察到我已使用索引位置插入到表视图部分值(索引)的位置。
当我根据表视图的部分索引值保存时,这些值会正确打印。但是当我不触摸表视图的第 5 个和第 7 个位置时,我会遇到崩溃问题。即 '*** -[NSMutableArray objectAtIndex: ]: 索引 5 超出范围 [0 .. 4]'
我不明白为什么只有 5 个字段值而不是 6 个字段值被插入到数组中。任何人都可以指导我正确的方向吗?
对这篇巨大的帖子感到抱歉,我想要为了提供信息,因此我将其看起来很详细,以便可以轻松回答该问题。
编辑
表视图的部分和行数的代码:
#pragma mark -
#pragma mark TableView Datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)atableView
{
return 8;
}
- (NSInteger)tableView:(UITableView *)atableView numberOfRowsInSection:(NSInteger)section
{
int arr = 1;
return arr;
}
索引路径行高度的实现:
- (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;
}
}
所有表视图单元格在滚动之前都不会加载。但是当我滚动时,控件将达到第5个,第 6 和第 7 个单元格。我发现这就是第 5 个和第 7 个单元格中的文本字段没有添加到“字段”数组中的原因。
我搜索的解决方案主要是 这种类型的帖子
预先感谢大家:)
I have a view controller with table view that contains 8 cells(sections).I have created a textField and added that text field as a subview to 6 cells,remaining 2 cells of which one cell contains a text view and the other with a button.Because I am using the same text field,I have assigned tag values and then created a mutable array,added the textfield to that array.Here is the implementation code:
Taking in regard the suggestion given by Mr.Lanc,I have 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 am trying to print the array using the following statement:
NSLog(@"Fields = %@",fields); I could observe the following in console window:
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 we can observe from the above o/p that number of fields that are getting printed is 5 i.e the 4 textfields and the cell with button,But as per our coding in -CellForRowAtIndexPath,6 cells contains textfield,hence the number of fields must be 6,but we are getting wrong result i.e. 5 (4 textfields+button) which is a blunder.
This is also leading me to saving the data in table view issue,i.e. for right navigation bar button item titled "Save" in my case,I have 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 with the index location values,since the number of values(fields) that "fields" array is holding is 5,the index values would be 0-4,but If I give the values accordingly,i.e. while saving,I would get a field less while inserting data in to sqlite database,Also I am getting null values in 5th and 7th locations of table view when printing in console.In the above implementation snippet,we can observe that I have inserted using the index location to that of table view section values(indices).
When I save according to section index values of table view,the values are getting printed properly.But I am getting crash problem when I don't touch the 5th and 7th locations of table view.i.e. '*** -[NSMutableArray objectAtIndex:]: index 5 beyond bounds [0 .. 4]'
I don't understand the reason why only 5 field values are getting inserted in to the array instead of 6.Can any one please guide me in right direction
Sorry for this huge post,I wanted to be informative,hence I have made it look detailed so that the issue can be answered with ease.
EDIT
The code for number of sections and rows for table view:
#pragma mark -
#pragma mark TableView Datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)atableView
{
return 8;
}
- (NSInteger)tableView:(UITableView *)atableView numberOfRowsInSection:(NSInteger)section
{
int arr = 1;
return arr;
}
Implementation of height for row at index 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 the table view cells are not loading before scrolling.But when I scroll then the control is reaching 5th,6th and 7th cells.I found out that's the reason why the textfield in 5th and 7th cells are not getting added to "fields" array.
I searched for solution mostly the posts of this type
Thanks all in advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您尝试过滚动桌面视图吗? I
如果您已在
if( cell == nil )
中添加此代码,则它可能不会创建所有文本字段,因为表格单元格被重用。最好为所有 8 个单元使用不同的单元标识符。那可以解决你的问题。Did you try scrolling the tableview? I
If you have added this code inside
if( cell == nil )
, then it may not create all the text fields, as the table cells are reused. Better you have different cell identifiers for all the 8 cells. That would solve your problem.我在此处找到了问题的解决方案
感谢每一位查看过的人的问题。
I found out the solution to the question here
Thanks every one who viewed the question.