我有一个表视图,在两个部分中都有两个部分,我都放置了UITextField。但是当我为每个文本字段使用标签时,它为两个部分共享相同的标签,因此我的应用程序崩溃了。如果第一部分包含 4 个文本字段,则第 2 部分大约包含 2 个文本字段。
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if (Array1) {
[Array1 setObject:textField.text atIndexedSubscript:[textField tag]-1];
NSLog(@"Array1 after edit: %@",Array1);
}
if (Array2) {
[Array2 setObject:textField.text atIndexedSubscript:[textField tag]-1]; // app crashing at this line
NSLog(@"Array2 after edit: %@",Array2);
}
}
它给出了这个错误
*由于未捕获的异常"NSRangeException"而终止应用程序,原因:"* -[__NSArrayM setObject:atIndex:]:索引 2 超出 边界 [0 .. 0]'
第一个抛出调用堆栈:
我的故事像这样查看代码
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2; //count of section
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0 ) {
return [Array1 count];
} else {
return [Array2 count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell=(CustomCell *)[ProfileTable dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomeCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if (indexPath.section==0) {
[cell.SInfoTxt setPlaceholder:[[ProfileTableArray objectAtIndex:indexPath.row] valueForKey:@"FN"]];
cell.SInfoTxt.text=[Array1 objectAtIndex:indexPath.row];
} else {
[cell.SInfoTxt setPlaceholder:[[profileTblCustomArray objectAtIndex:indexPath.row] valueForKey:@"FN"]];
cell.SInfoTxt.text=[Array2 objectAtIndex:indexPath.row];
}
cell.SInfoTxt.tag=indexPath.row+1;
//set cell background clear color
cell.backgroundColor=[UIColor clearColor];
return cell;
}
您可以使用其框架原点来了解其单元格 indexPath,而不是在委托中使用 tag UITextfield
。
- (void)textFieldDidEndEditing:(UITextField *)textField
{
CGPoint origin = textField.frame.origin;
CGPoint point = [textField convertPoint:origin toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];
if (indexPath.section==0) {
[Array1 setObject:textField.text atIndexedSubscript:[textField tag]-1];
NSLog(@"Array1 after edit: %@",Array1);
}
if (indexPath.section==1) {
[Array2 setObject:textField.text atIndexedSubscript:[textField tag]-1];
NSLog(@"Array2 after edit: %@",Array2);
}
}
代码中
cell.SInfoTxt.text=[Array1 objectAtIndex:indexPath.row];
cell.SInfoTxt.text=[Array2 objectAtIndex:indexPath.row];
如果每个数组的对象总数不同。
您必须在此委托方法中设置不同的计数 -numberOfRowsInSection
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
两个部分有两个数组。因此,数组中的计数可能不同。最好这样使用。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0 ) {
return [Array1 count];
} else {
return [Array2 count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell=(CustomCell *)[ProfileTable dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomeCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if (indexPath.section==0) {
[cell.SInfoTxt setPlaceholder:[[ProfileTableArray objectAtIndex:indexPath.row] valueForKey:@"FN"]];
cell.SInfoTxt.text=[Array1 objectAtIndex:indexPath.row];
cell.SInfoTxt.tag=1000+indexPath.row; // We can easily identifies using tag.
} else {
[cell.SInfoTxt setPlaceholder:[[profileTblCustomArray objectAtIndex:indexPath.row] valueForKey:@"FN"]];
cell.SInfoTxt.text=[Array2 objectAtIndex:indexPath.row];
cell.SInfoTxt.tag=2000+indexPath.row;
}
return cell;
}