2012-05-19 24 views
0

好吧,進入我的下一個困境。我做了大量的谷歌搜索,似乎有幾種方法可以解決這個問題,但似乎沒有一個適合我的表格視圖結構。我使用的Xcode 4.3.2 ARC和故事板。我有一個普通的視圖表視圖。我的表格由一系列城市及其部門組成(每行一個城市),當用戶選擇一個城市時,他們將被推送到關於該城市的詳細視圖。我想按字母順序整理我的表格,並帶有部分標題。任何建議,提示,教程鏈接,將不勝感激。表視圖組織成字母節

我有一個NSMutable陣列設置像這樣在我的表視圖的.m

NSMutableArray *dept; 

- (void)viewDidLoad 

{ 
[super viewDidLoad]; 

dept = [[NSMutableArray alloc] init]; 

City *thedept = [[City alloc] init]; 
[thedept setCityname:@"Albany"]; 
[thedept setPhone:@"5551237890"]; 
[dept addObject:thedept]; 

thedept = [[City alloc] init]; 
[thedept setCityname:@"Boston"]; 
[thedept setPhone:@"5556543435"]; 
[dept addObject:thedept]; 

thedept = [[City alloc] init]; 
[thedept setCityname:@"Springfield"]; 
[thedept setPhone:@"5553456323"]; 
[dept addObject:thedept]; 

thedept = [[City alloc] init]; 
[thedept setCityname:@"Tallahassee"]; 
[thedept setPhone:@"5552450988"]; 
[dept addObject:thedept]; 

thedept = [[City alloc] init]; 
[thedept setCityname:@"Vacouver"]; 
[thedept setPhone:@"5551312555"]; 
[dept addObject:thedept]; 

節頭可以只是字母表(A,B,C,d ...)的信。我想我需要通過他們的第一個字母預先整理我的城市,並且可能會根據字母命名該部分(如「sectionAcities」)。我給出的城市只有幾個,我總共有大約100個。

回答

1

您需要使用以下UITableViewDataSource方法才能實現此目的。

sectionIndexTitlesForTableView: 
tableView:titleForHeaderInSection: 
tableView:sectionForSectionIndexTitle:atIndex: 

以下是在那個developer.apple.com TheElements應用目前的鏈接,真正做到這一點 - http://developer.apple.com/library/ios/#samplecode/TheElements

+0

rishi,我看過那個教程謝謝。我知道的方法,我只是不知道如何實例化它與我的數組結構。我注意到TheElements示例使用了一個plist,這似乎讓我感到困惑。 – Chris

0

在你的情況,你必須使用對象的使用下面的代碼,而不是做簡單的排序基於排序在樣品

self.tableArray = [[self.tableArray sortedArrayUsingComparator:^ NSComparisonResult(對象*第一,對象*秒){

NSString *name1=[first name]; 
    NSString *name2=[second name]; 
    return [name1 compare:name2]; 

}]mutableCopy] ; 

在此之後,您將獲得您的排序數組,並且您可以使用此數組來創建數組字典。

+0

請將您的網站網址放入您的個人資料中,而不是在所有答案中四處飛濺。 –