2010-10-04 111 views
1
- (void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

if ([indexPath section] == 0) { 
    switch ([indexPath row]) { 

    case 0: 
    [self renameExercise]; 
    [[self tableView] deselectRowAtIndexPath:indexPath 
      animated:YES]; 
    break; 

    case 1: 
    EditRootNoteViewController *newController = [[EditRootNoteViewController alloc] initWithNibName:@"EditNoteView" 
          bundle:nil]; 
    [newController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal]; 
    [newController setDelegate:self]; 
    [self presentModalViewController:newController 
      animated:YES]; 
    [newController release]; 
    break; 

xCode'期待EditRootNoteViewController之前的表達式......但爲什麼來?這段代碼在這個開關之外工作......這可能是某種線索,但我一點也沒有。爲什麼這段代碼無法工作?

回答

2

這是因爲你不能在switch語句中聲明一個變量作爲case的第一條語句。

請參閱this questionthis question瞭解更多信息。

1

這是您的整個switch語句嗎?如果是這樣,你忘記了

default: 
break; 

部分。

確保您的問題包含完整的方法,或至少一個完整的方法,使我們更容易幫助。編輯: 哦!在第二次查看之後,如果在switch語句中聲明新變量,則必須在大括號內進行。不知道爲什麼,幾周前我遇到了這個問題。也許有人可以詳細說明爲什麼這是必要的?

+0

這不是一個完整的方法,我認爲我在幫助之後不包括這些東西,但是我可以看到這可能是第一個想法。謝謝。 – griotspeak 2010-10-04 00:59:37

+0

交換語句不需要有默認情況。 – JeremyP 2010-10-04 09:23:26

2

嘗試把代碼塊:

... 
    switch ([indexPath row]) { 

    case 0: 
    [self renameExercise]; 
    [[self tableView] deselectRowAtIndexPath:indexPath 
      animated:YES]; 
    break; 

    case 1: { 
    EditRootNoteViewController *newController = [[EditRootNoteViewController alloc] initWithNibName:@"EditNoteView" bundle:nil]; 
    [newController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal]; 
    [newController setDelegate:self]; 
    [self presentModalViewController:newController animated:YES]; 
    [newController release]; 
    } break; 
    } 

或者更好的是,提取的代碼放到一個單獨的方法。

+0

dag。只有一個答案可以是正確的?好吧...我投了這個......但其他答案很徹底,爲什麼......但這也是一個很好的答案。 (我知道這不是那麼嚴肅,但這是我所能做的,謝謝你) – griotspeak 2010-10-04 01:04:50

相關問題