Вызов нового представления путем выбора ошибки строки UITableView - PullRequest
0 голосов
/ 22 апреля 2011

У меня есть некоторые проблемы с извлечением определенных ViewControllers в зависимости от того, какая строка выбрана в моем UITableView. У меня есть таблица, которая содержит 3 раздела, по 4 строки в каждом разделе.

Когда строка выбрана, я настроил вызов нового представления для каждой строки, так что в общей сложности будет вызываться 12 видов в зависимости от того, какая строка выбрана. Проблема в том, что вызываются только первые 4 вида, когда я нажимаю на строку номер 5, он отображает новый вид, но подтягивает первый вид.

Я настроил свой UITableView, используя следующий код;

carbData = [[NSArray alloc] initWithObjects:@"What Are Carbs?",@"Why Do I Need Carbs?",@"Simple Vs. Complex",@"Fun Five Facts",nil];
    proteinData = [[NSArray alloc] initWithObjects:@"What is Protein?",@"Why Do I Need Protein?",@"Complete Vs. Incomplete",@"Fun Five Facts",nil];
    fatData = [[NSArray alloc] initWithObjects:@"What Are Fats?",@"Why Do I Need Fats?",@"Saturated Vs. Unsaturated",@"Fun Five Facts",nil];
    [self setTitle:@"Nutrition Table"];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.carbData count];
    return [self.proteinData count];
    return [self.fatData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];

    if (cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SimpleTableIdentifier] autorelease];
    }   
    if(indexPath.section == 0)
        cell.text = [carbData objectAtIndex:indexPath.row];
    if(indexPath.section == 1)
        cell.text = [proteinData objectAtIndex:indexPath.row];
    if (indexPath.section == 2)
        cell.text = [fatData objectAtIndex:indexPath.row];
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if(section == 0)
        return @"Carbohydrates";
    if (section ==1)
        return @"Protein";
    else if (section ==2)
        return @"Fats";
}

Затем я использую следующий код, чтобы определить, какое представление вызывать, когда выбрана определенная строка;

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


        if ([[carbData objectAtIndex:indexPath.row] isEqual:@"What Are Carbs?"]) {
            CarbsView1 *controller = [[CarbsView1 alloc] initWithNibName:@"CarbsView1" bundle:nil];
            controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
            [self presentModalViewController:controller animated:YES];
            [controller release];
        }
        if ([[carbData objectAtIndex:indexPath.row] isEqual:@"Why Do I Need Carbs?"]) {
            CarbsView2 *controller = [[CarbsView2 alloc] initWithNibName:@"CarbsView2" bundle:nil];
            controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
            [self presentModalViewController:controller animated:YES];
            [controller release];
        }
        if ([[carbData objectAtIndex:indexPath.row] isEqual:@"Simple Vs. Complex"]) {
            CarbsView3 *controller = [[CarbsView3 alloc] initWithNibName:@"CarbsView3" bundle:nil];
            controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
            [self presentModalViewController:controller animated:YES];
            [controller release];
        }
        if ([[carbData objectAtIndex:indexPath.row] isEqual:@"Fun Five Facts"]) {
            CarbsView4 *controller = [[CarbsView4 alloc] initWithNibName:@"CarbsView4" bundle:nil];
            controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
            [self presentModalViewController:controller animated:YES];
            [controller release];
        }
        if  ([[proteinData objectAtIndex:indexPath.row] isEqual:@"What is Protein?"]) {
            ProteinView1 *controller = [[CarbsView4 alloc] initWithNibName:@"ProteinView1" bundle:nil];
            controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
            [self presentModalViewController:controller animated:YES];
            [controller release];
        }

Приложение прекрасно компилируется и собирается, все работает так, как мне бы хотелось, но когда я смотрю на используемые названия, когда я пытаюсь выбрать «Что такое белок?» это подтягивает взгляд на "что такое углеводы?" который находится в первом операторе if.

Любая помощь будет высоко ценится

1 Ответ

1 голос
/ 22 апреля 2011

"Что такое белок?"находится в разделе 1, строка 0. "Что такое углеводы?"находится в разделе 0, строка 0. Обратите внимание, что ваш метод didSelectRowAtIndexPath, вы нигде не проверяете номер раздела.Поэтому, когда вы нажимаете секцию 1, строку 0, ваш первый оператор if возвращает true.Я думаю, вы хотите что-то еще, как:

if (indexPath.section == 0) {
    if(indexPath.row == 0) {
        //present appropriate view controller
    } else if (indexPath.row == 1) {

    } else if (indexPath.row == 2) {

    } ...
} else if (indexPath.section == 1) {
    if (indexPath.row == 0) {

    } else if (indexPath.row == 1) {

    } ...
} ...
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...