2012-03-16 29 views
0

我有一個表格視圖窗體,用戶應該輸入一些信息(問題和選擇)。基本上,當用戶點擊最後一節,文本字段和其他元素正在收集。無法到達從UITableViewController的UITableView的第0節

- (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。我嘗試交換問題和選擇單元格的段落,那段時間它返回null的部分0對應於選擇。問題單元格運作良好。我也嘗試增加節號,所以問題單元格在第1部分,選擇是第2節等...然後它返回null爲第1節。我無法弄清楚發生了什麼事情,這是一個非常奇怪的問題。

順便說一句,我可以訪問didSelectRowAtIndexPath中的單元格,當indexPath等於0節中的行的索引路徑。但是當它不是時,它在我做[NSIndexPath indexPathForRow:0 inSection:0]時返回null。

例如在didSelectRowAtIndexPath

2012-03-17 01:38:47.125 x2516:207] TEST 1 - (null) 
:當我寫「fooBar」的的問題輸入第0和點擊細胞在第4(「向」按鈕)

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); 
} 

給出了這樣的輸出

,並給出了這個,當我點擊的部分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 

在此先感謝...

回答

1

我在考慮按下添加單元格(indexPath部分== 1,行== numberOfChoices),在0,0行的單元格是在屏幕之外。據我瞭解UITableViews,UITableViewCells正在重用。通常,如果單元格在屏幕之外,它將進入可重用池。 (如果我錯了,請糾正我。)在屏幕外調用單元格可能會或可能不會給出正確的值。我建議將值存儲在其他地方的單元格中(如@property),以便稍後可以檢索它們。

+0

哇,我不知道。非常感謝您快速回答! – berkersonmez 2012-03-16 23:57:42

相關問題