Представление UITable: didselectrowatIndexPath для нескольких строк - PullRequest
0 голосов
/ 28 марта 2011

У меня есть табличное представление (HomeViewController), состоящее из следующих элементов:

местоположения>

Отчетность>

Настройка>

Я могусделать это с помощью «didselectrowatIndexPath» для одной строки, но когда я пытаюсь сделать это с несколькими строками (если еще конструкция), не получаю ошибку, но все равно не могу нажать на любую (местоположения, отчеты или настройки) .Iимпортировали .h файлы всех трех вышеуказанных кодов .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];
}

Также хотите обсудить заявление об освобождении, помогите мне!Спасибо

1 Ответ

1 голос
/ 28 марта 2011

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];
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...