2010-12-13 46 views
0

我有一個帶有自定義單元格的TableView。iPhone Table View Section Headers

我需要能夠把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]; 
}} 

感謝您的任何建議傢伙。

+2

您需要粘貼代碼(而不是屏幕抓圖)到問題中,選擇它並將其標記爲代碼(編輯器中的「二進制」圖標)。 :-) – 2010-12-13 16:20:08

+0

爲什麼你張貼截圖而不是代碼? – vikingosegundo 2010-12-13 16:28:43

+0

當我回家時,會發布代碼,只是走到我的車。對不起,我認爲可能有太多的代碼發佈。 – StefanHanotin 2010-12-13 16:38:45

回答

2

與您使用indexPath.row相同,您可以使用indexPath.section來標識當前活動的部分。例如在cellForRowAtIndexPath中,您可以有兩個嵌套的switch語句,一個用於該部分,另一個用於該行。

填充了的tableView另一方法(未這裏聲稱完整性):

numberOfSectionsInTableView將返回2(兩部分), numberOfRowsInSection將返回7或3根據活動部分的, titleForHeaderInSection將返回(如果需要)你想在你的部分頂部看到的字符串。

而且在個人註釋:你可能不想打電話給你的看法test1的,更好的聲明爲

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

您正在使用TEST3多時間和「testX」的命名風格,可以是痛苦的調試

+0

嗨奧拉夫,謝謝你的回覆。不知道如何納入這一點,因爲我不知道目前活躍的部分等。你可以詳細說明這一點?也感謝關於我的命名約定的建議。我真的必須改變它們! :) – StefanHanotin 2010-12-13 18:36:25

+0

例如tableview會自動調用' - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section'。添加一個開關或者如果情況來檢測哪個部分是活動的。類似於 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath'。 indexPath有兩個字段:行和節。 – Olaf 2010-12-14 09:46:43

+0

非常感謝您的幫助Olaf。你說的是什麼,我的問題解決了。現在對於那些命名約定! – StefanHanotin 2010-12-14 11:46:06