我有一个表格视图表单,用户应该在其中输入一些信息(问题和选择)。基本上,当用户单击最后一部分时,正在收集文本字段和其他元素。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section==0) {
AskCellQuestion *cell;
cell = [tableView dequeueReusableCellWithIdentifier:@"askQuestionCell"];
if (cell == nil) {
cell = [[AskCellQuestion alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"askQuestionCell"];
}
...
cell.questionText.delegate = cell;
return cell;
} else if (indexPath.section==1) {
if (indexPath.row < numberOfChoices) {
AskCellChoice *cell;
cell = [tableView dequeueReusableCellWithIdentifier:@"askChoiceCell"];
if (cell == nil) {
cell = [[AskCellChoice alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"askChoiceCell"];
}
...
cell.choiceText.delegate = cell;
...
return cell;
} else {
AskCellChoiceAdd *cell;
cell = [tableView dequeueReusableCellWithIdentifier:@"askChoiceAddCell"];
if (cell == nil) {
cell = [[AskCellChoiceAdd alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"askChoiceAddCell"];
}
return cell;
}
} else if (indexPath.section==2) {
...
}
// Configure the cell...
return 0;
}
在didSelectRowAtIndexPath
功能中,我试图访问这些文本字段并获取它们的值:
UITableView * questionFormView = [self tableView];
AskCellQuestion * questionCell = (AskCellQuestion *)[questionFormView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
NSString * questionText = questionCell.questionText.text;
NSLog(@"TXt: %@",questionText);
UIImage * questionImage = questionCell.questionImage;
NSLog(@"IMG: %@",questionImage);
NSString * questionVideo = questionCell.youtubeVideoID;
NSLog(@"VID: %@",questionVideo);
for (int i = 0; i < numberOfChoices; i++) {
AskCellChoice * choiceCell = (AskCellChoice *)[questionFormView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:1]];
NSLog(@"C TXt: %@",choiceCell.choiceText.text);
NSLog(@"C IMG: %@",choiceCell.choiceImage);
NSLog(@"C VID: %@",choiceCell.youtubeVideoID);
}
它在控制台中给出这个输出:
2012-03-17 01:04:46.700 x[2346:207] TXt: (null)
2012-03-17 01:04:46.701 x[2346:207] IMG: (null)
2012-03-17 01:04:46.701 x[2346:207] VID: (null)
2012-03-17 01:04:46.701 x[2346:207] C TXt: TEST CHOICE 1
2012-03-17 01:04:46.701 x[2346:207] C IMG: <UIImage: 0x6ec75f0>
2012-03-17 01:04:46.702 x[2346:207] C VID: FMij4sZioBM
2012-03-17 01:04:46.702 x[2346:207] C TXt: TEST CHOICE 2
2012-03-17 01:04:46.702 x[2346:207] C IMG: <UIImage: 0x6e7ce30>
2012-03-17 01:04:46.702 x[2346:207] C VID: GfIcEzFzqKE
简而言之,我无法访问第 0 节中的文本字段。我尝试添加第二行,它也返回 null。我尝试交换问题和选择单元格的部分,当时它为对应于选择的第 0 部分返回 null。问题单元运行良好。我还尝试增加节数,所以问题单元格在第 1 节,选择在第 2 节等等......然后它为第 1 节返回 null。我不知道发生了什么,这是一个非常奇怪的问题。
顺便说一句,我可以在等于第 0 节中行的索引路径时访问单元格。但是当它不是时,它会在我这样做时返回didSelectRowAtIndexPath
null 。indexPath
[NSIndexPath indexPathForRow:0 inSection:0]
例如在didSelectRowAtIndexPath
:
if (indexPath.section==4) {
NSLog(@"TEST 1 - %@", ((AskCellQuestion *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]).questionText.text);
//[self askTheQuestion];
} else if (indexPath.section==0) {
NSLog(@"TEST 2 - %@", ((AskCellQuestion *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]).questionText.text);
NSLog(@"TEST 3 - %@", ((AskCellQuestion *)[tableView cellForRowAtIndexPath:indexPath]).questionText.text);
}
当我将“fooBar”写入第 0 节中的问题输入并单击第 4 节中的单元格(“询问”按钮)时给出此输出:
2012-03-17 01:38:47.125 x2516:207] TEST 1 - (null)
并在我单击第 0 部分的单元格时给出这个,这是带有问题输入的单元格:
2012-03-17 01:38:44.782 x[2516:207] TEST 2 - fooBar
2012-03-17 01:38:44.793 x[2516:207] TEST 3 - fooBar
提前致谢...