UITableview:didselectrowatIndexPath用于多行

时间:2011-03-28 13:30:22

标签: iphone uitableview

我有一个表视图(HomeViewController),包含以下项目:

地点>

报告>

设置>

我可以在“didselectrowatIndexPath”的帮助下为单行执行此操作,但是当我尝试使用多行(if else构造)时,不会出现错误但仍无法单击任何一个(位置) ,报告或设置)。我已导入上述所有三个.h文件。我的代码:

- (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];
}

还想讨论发布声明 帮我! 感谢

1 个答案:

答案 0 :(得分: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];
  }
}