Заголовки разделов представления таблицы iPhone - PullRequest
0 голосов
/ 13 декабря 2010

У меня есть TableView с пользовательскими ячейками.

Мне нужно иметь возможность разместить 2 заголовка раздела под названием Инструкция и Справочник.

Мне нужно 7 строк для раздела «Инструкция» и 3 строки для раздела «Справочник».

Вот как я закодировал мой вид таблицы. Я знаю, что это не идеально, но я новичок в этом:

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return [listOfItems count];}

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

static NSString *CellIdentifier = @"Cell";

CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}

switch (indexPath.row) {
    case 0:
        cell.customLabel.text = @"Anatomy";
        cell.customImage.image = [UIImage imageNamed:@"anatomy.png"];
        cell.arrowImage.image = [UIImage imageNamed:@"arrow.png"];

        break;
    case 1:
        cell.customLabel.text = @"Indications";
        cell.customImage.image = [UIImage imageNamed:@"Info.png"];
        cell.arrowImage.image = [UIImage imageNamed:@"arrow.png"];

        break;
    case 2:
        cell.customLabel.text = @"Medications";
        cell.customImage.image = [UIImage imageNamed:@"pill.png"];
        cell.arrowImage.image = [UIImage imageNamed:@"arrow.png"];

        break;
    case 3:
        cell.customLabel.text = @"Equipment";
        cell.customImage.image = [UIImage imageNamed:@"spanner.png"];
        cell.arrowImage.image = [UIImage imageNamed:@"arrow.png"];
        break;
    case 4:
        cell.customLabel.text = @"Procedure";
        cell.customImage.image = [UIImage imageNamed:@"procedure.png"];
        cell.arrowImage.image = [UIImage imageNamed:@"arrow.png"];
        break;
    case 5:
        cell.customLabel.text = @"Complications";
        cell.customImage.image = [UIImage imageNamed:@"complication.png"];
        cell.arrowImage.image = [UIImage imageNamed:@"arrow.png"];
        break;
    case 6:
        cell.customLabel.text = @"Procedure Video";
        cell.customImage.image = [UIImage imageNamed:@"procedure.png"];
        cell.arrowImage.image = [UIImage imageNamed:@"arrow.png"];
        break;
    case 7:
        cell.customLabel.text = @"Calculations";
        cell.customImage.image = [UIImage imageNamed:@"math.png"];
        cell.arrowImage.image = [UIImage imageNamed:@"arrow.png"];
        break;
    case 8:
        cell.customLabel.text = @"Videos & Images";
        cell.customImage.image = [UIImage imageNamed:@"Pin.png"];
        cell.arrowImage.image = [UIImage imageNamed:@"arrow.png"];
        break;
    case 9:
        cell.customLabel.text = @"Feedback";
        cell.customImage.image = [UIImage imageNamed:@"feedback.png"];
        cell.arrowImage.image = [UIImage imageNamed:@"arrow.png"];
        break;
    default:
        break;
}
return cell;}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
    AnatomyWeb1 *test1 = [[AnatomyWeb1 alloc] initWithNibName:@"AnatomyWeb1" bundle:nil];

    [self.navigationController pushViewController:test1 animated:YES];

    [test1 release];
}
if (indexPath.row == 1) {
    Indications *test1 = [[Indications alloc] initWithNibName:@"Indications" bundle:nil];

    [self.navigationController pushViewController:test1 animated:YES];

    [test1 release];
}
if (indexPath.row == 2) {
    Medication *test6 = [[Medication alloc] initWithNibName:@"Medication" bundle:nil];

    [self.navigationController pushViewController:test6 animated:YES];

    [test6 release];
}
if (indexPath.row == 3) {

    ProcedureWeb1 *test3 = [[ProcedureWeb1 alloc] initWithNibName:@"ProcedureWeb1" bundle:nil];

    [self.navigationController pushViewController:test3 animated:YES];

    [test3 release];
}
if (indexPath.row == 4) {
    ProcedureWeb3 *test3 = [[ProcedureWeb3 alloc] initWithNibName:@"ProcedureWeb3" bundle:nil];

    [self.navigationController pushViewController:test3 animated:YES];

    [test3 release];
}
if (indexPath.row == 5) {
    Complications2 *test4 = [[Complications2 alloc] initWithNibName:@"Complications2" bundle:nil];

    [self.navigationController pushViewController:test4 animated:YES];

    [test4 release];
}
if (indexPath.row == 6) {
    [self video1];
}
if (indexPath.row == 7) {
    Calculations1 *test3 = [[Calculations1 alloc] initWithNibName:@"Calculations1" bundle:nil];

    [self.navigationController pushViewController:test3 animated:YES];

    [test3 release];

}
if (indexPath.row == 8) {
    VideosAndImages *test3 = [[VideosAndImages alloc] initWithNibName:@"VideosAndImages" bundle:nil];

    [self.navigationController pushViewController:test3 animated:YES];

    [test3 release];

}
if (indexPath.row == 9) {
    Feedback *test11 = [[Feedback alloc] initWithNibName:@"Feedback" bundle:nil];

    test11.previousViewController=self;

    test11.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

    [self presentModalViewController:test11 animated:YES];

    [test11 release];
}}

Спасибо за любые предложения, ребята.

1 Ответ

2 голосов
/ 13 декабря 2010

Так же, как вы использовали indexPath.row, вы использовали бы indexPath.section для идентификации текущего активного раздела.Например, в cellForRowAtIndexPath у вас может быть два вложенных оператора switch, один для раздела, один для строки.

Заполните другой метод для tableView (не требуя здесь полноты):

numberOfSectionsInTableView вернет 2 (два раздела), numberOfRowsInSection вернет 7 или 3 в зависимости от активного раздела, titleForHeaderInSection вернет (при необходимости) строку, которую вы хотите видеть в верхней части вашего раздела.

И в личной заметке: вы можете не захотеть называть свое мнение test1 , лучше объявить его как

AnatomyWeb * anatomyWeb = ...
Indications * indications = ...

Вы используете test3 несколько раз, и стиль именования "testX" может бытьболь для отладки

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