2011-03-28 70 views
0

我有包括的項目作爲一個表視圖(HomeViewController):UITable觀點:didSelectRowAtIndexPath方法爲多行

位置>

報告>

設置>

我能夠做到這與「didselectrowatIndexPath」幫助單行,但當我試圖這樣做與多行(如果其他構造),沒有得到錯誤,但仍然無法點擊任何一個(位置,報告或設置)。我有全部導入.h文件稀土元素above.my代碼:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if ([[menuList objectAtIndex:indexPath.row] isEqual:@"LOCATIONS"]) 
    { 
     LocationViewController *locationViewController;  
     locationViewController = [[LocationViewController alloc] initWithNibName:@"LocationViewController" bundle:nil]; 
     locationViewController.menuList = [menuList objectAtIndex:indexPath.row]; 
     [self.navigationController pushViewController:locationViewController animated:YES]; 
    } 
    else if([[menuList objectAtIndex:indexPath.row] isEqual:@"REPORTING"]) 
    { 
     Reporting *reporting; 
     reporting = [[Reporting alloc] initWithNibName:@"Reporting" bundle:nil]; 
     reporting.menuList = [menuList objectAtIndex:indexPath.row]; 
     [self.navigationController pushViewController:reporting animated:YES]; 
    } 
    //[locationViewController release]; 
} 

而且要討論關於發佈聲明 幫幫我吧! 謝謝

+0

什麼是LocationViewController和報告的父類? – malinois 2011-03-28 13:47:19

+0

UITableViewDelegate和UITableViewDataSource – Alok 2011-03-28 13:56:45

+0

真的嗎?它不是UINavigationController?我在談論班級,而不是代表。 – malinois 2011-03-28 13:58:48

回答

1

isEqual測試對象的相等到另一個對象。如果menuList數組中的字符串全部大寫,那麼這很好。如果他們就像你在代碼之前的例子那麼你會遇到問題。另外,如果它們都是NSStrings,那麼你應該使用isEqualToString而不是isEqual。您可以通過執行以下操作來測試:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {  
    NSString *arrayValue = [menuList objectAtIndex:indexPath.row]; 
    NSString *myValue = @"LOCATION"; 

    NSLog(@"array value: '%@' my value: '%@'",arrayValue,myValue); 
} 

由於對象「超出範圍」,因此版本無效。

對象範圍是該變量的當前「可見」代碼庫。下面是一些例子:

- (void)aRandomFunction { 
    /* here is a variable/object. Its scope is the whole function because it has been 
     declared directly in the function. All constructs have access to it (within the function) */ 
    NSString *myString = @"My String"; 

    if(YES){ 
    NSLog(@"%@", myString); // myString is visible here because its in scope. 
    } 
} 

- (void)anotherRandomFunction { 
    if(YES){ 
    /* here, because we've declared the variable within the if statement 
     it's no longer a direct object of the function. Instead its a direct 
     child of the if statement and is therefore only "visible" within that 
     if statement */ 
    NSString *myString = @"My String"; 
    NSLog(@"%@", myString); // myString is visible here because its in scope. 
    } 
    NSLog(@"%@", myString); // but NOT available here because it is out of scope 
} 

因此,在本質上,一個變量的作用域是其直接父結構以及其父的孩子構建。

所以有兩種方法可以做你的例子。我最喜歡的是這樣的:

- (void)aFunctionToPushAViewController { 
    UIViewController *nextPage = NULL; 
    if(YES){ 
    nextPage = [[CustomViewController alloc] initWithNibName:nil bundle:nil]; 
    } 
    else { 
    nextPage = [[ADifferentViewController alloc] initWithNibName:nil bundle:nil]; 
    } 
    [self.navigationController pushViewController:nextPage animated:YES]; 
    [nextPage release]; 
} 

或...你可以釋放它在if語句...

- (void)aFunctionToPushAViewController { 
    if(YES){ 
    CustomViewController *nextPage = [[CustomViewController alloc] initWithNibName:nil bundle:nil]; 
    [self.navigationController pushViewController:nextPage animated:YES]; 
    [nextPage release]; 
    } 
    else { 
    ADifferentViewController *nextPage = [[ADifferentViewController alloc] initWithNibName:nil bundle:nil]; 
    [self.navigationController pushViewController:nextPage animated:YES]; 
    [nextPage release]; 
    } 
} 
+0

謝謝你非常容易,簡單和理解的方式! – Alok 2011-03-28 14:43:46

相關問題