UITableView отправляет только один контроллер

Итак, у меня есть свой UITableView, с 2 разделами и 1 ячейкой в ​​каждом, и если я нажимаю первый, он работает, затем второй, он переходит к первому контроллеру. RootViewController — это navigationController, пытающийся передать ViewControllers.

Вот код для tableView:

// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 0)
        return 1;
    else
        return 1;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if(section == 0){
        return @"Terminal/SSH Guides";
    }else{
        return @"Cydia Tutorials";
    }
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    // Set up the cell...
    if(indexPath.section == 0){
        cell.text = @"Changing Password for root";
    } else {
        cell.text = @"Hiding Sections";
    }
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    id newController;
    switch (indexPath.row) {
        case 0:
            newController = [[rootpassword alloc] initWithNibName:@"rootpassword" bundle:nil];
            break;
        case 1:
            newController = [[hidingsections alloc] initWithNibName:@"hidingsections" bundle:nil];
            break;
        default:
            break;
    }
    [self.navigationController pushViewController:newController animated:TRUE];
    [tableView deselectRowAtIndexPath:indexPath animated:TRUE];
}

У меня также возникают проблемы с добавлением дополнительных разделов и строк/ячеек в разделы.

Спасибо.


person Mitochondria    schedule 16.01.2011    source источник


Ответы (2)


Я считаю, что вы должны сделать переключатель в "didSelectRowAtIndexPath" над indexPath.section вместо indexPath.row, поскольку оба раздела имеют только одну строку.

person fsaint    schedule 16.01.2011
comment
Кажется, это работает, но что, если я хочу добавить больше разделов и более одной строки в каждый? Спасибо. - person Mitochondria; 16.01.2011
comment
Есть много способов сделать это в зависимости от имеющихся у вас данных. Обычно у вас будет переключатель в разделе массив для каждого раздела с данными для настройки ячейки. Ознакомьтесь с этим примером mobisoftinfotech.com/blog/iphone/introduction-to- табличное представление - person fsaint; 16.01.2011
comment
А, спасибо. Удалось заставить его работать с некоторыми циклами if/switch. - person Mitochondria; 16.01.2011

У вас есть только одна строка в каждом разделе, поэтому indexPath.row всегда будет равен нулю. В didSelectRowAtIndexPath вы должны проверить раздел, а не строку (предполагая, что вы намерены сохранить только одну строку в разделе).

person par    schedule 16.01.2011