2011-03-28 99 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

相关问题