我有一个包含8个单元格(section)的表视图的视图控制器。我创建了一个文本框,并将该文本框作为子视图添加到6个单元格中,剩下的2个单元格分别包含一个文本视图和一个按钮。由于我正在使用相同的文本框,我已经分配了标记值,然后创建了一个可变数组,并将文本框添加到该数组中。以下是实现代码:
考虑到Lanc先生给出的建议,我已更改了实现代码。
编辑
现在,从上面的输出结果中可以观察到打印出来的字段数量是5,即4个文本字段和一个带按钮的单元格。但是根据我们在-CellForRowAtIndexPath中的编码,应该有6个单元格包含文本字段,因此字段的数量必须为6,但我们得到了错误的结果,即5(4个文本字段+按钮),这是一个严重错误。
这也导致了我在表视图中保存数据的问题。对于右侧导航栏按钮项目“保存”(在我的情况下),我已经实现了:
现在的问题出在索引位置数值上,因为"fields"数组所持有的值(字段)数量是5,索引值应该是0-4。但如果我按照这样的值来保存,插入SQLite数据库时会少一个字段。同时,在控制台打印时,表格视图中第5个和第7个位置的值为空。在上述实现片段中,我们可以观察到我使用了表格视图部分值(索引)的索引位置进行插入。
当我按照表格视图的部分索引值进行保存时,值会被正确打印。但是,当我不触摸表格视图的第5个和第7个位置时,会出现崩溃问题,即“***-[NSMutableArray objectAtIndex:]: index 5 beyond bounds [0 .. 4]”。
我不明白为什么只有5个字段值被插入到数组中而不是6个。请问有人能指导我正确的方向吗?
对于这篇文章过长,我表示抱歉,但我想详细说明问题,以便更容易回答问题。
编辑:
表格视图中的节和行数的代码如下:
考虑到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>>"
)
现在,从上面的输出结果中可以观察到打印出来的字段数量是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:]: index 5 beyond bounds [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个单元格中的文本字段未被添加到“fields”数组的原因。
我搜索了解决方案,大多数帖子都是像this这样的类型。
提前感谢大家 :)
self.fields
中。因为即使在“save”方法中,他也将其作为UITextField检索 - Ilanchezhian