2014-06-19 50 views
1

我有一個NSMutableArray,它是由一個sqlite查詢填充的,它返回我想在我的UITableview中按字母順序顯示的所有內容。UITableview從數據數組創建字母索引

我知道想就像你在聯繫人應用程序中看到顯示字母指數法等

但是我不知道如何設置的指數法,並從該數組創建部分以致每一部分是下一個字母表中的字母。

我將如何去取得我的數組並獲取所需的部分和索引?

+0

這個問題看似已關閉因爲它在蘋果文檔中得到了解決。 –

+0

@PeterDeWeese你能發送一個鏈接嗎?我無法找到有關主題的任何信息 –

+0

這是一個教程:http://www.iphonedevcentral.com/indexed-uitableview-tutorial/這裏是Apple Doc:https://developer.apple.com/library/ios /documentation/uikit/reference/UITableView_Class/Reference/Reference.html –

回答

0

假設你的數組(NSMutableArray的myarray的),用字母看起來是這樣的:

(
    { 
    headerTitle = V; 
    rowValues =   (
        { 
      id = 60; 
      name = "Valley of the Giants (2)"; 
      "real_name" = "Valley of the Giants"; 
      "wine_count" = " (2)"; 
     } 
    ); 
}, 
    { 
    headerTitle = R; 
    rowValues =   (
        { 
      id = 47; 
      name = "Rothbury Estate (6)"; 
      "real_name" = "Rothbury Estate"; 
      "wine_count" = " (6)"; 
     }, 
        { 
      id = 48; 
      name = "Rouge Homme (4)"; 
      "real_name" = "Rouge Homme"; 
     }, 
        { 
      id = 45; 
      name = "Rawson\U2019s Retreat (7)"; 
      "real_name" = "Rawson\U2019s Retreat"; 
     }, 
        { 
      id = 46; 
      name = "Rosemount Estate (36)"; 
      "real_name" = "Rosemount Estate"; 
     } 
    ); 
}, 

在表視圖中,部分的設置數量

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return [myArray count]; 
} 

行的一個部分號碼:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return [[[myArray objectAtIndex:section] objectForKey:@"rowValues"] count]; 
} 

部分標題

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ 
    return [[myArray objectAtIndex:section] valueForKey:@"headerTitle"]; 
} 

然後裏面的cellForRowAtIndexPath你可以像這樣訪問

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    NSString *name= [[[[myArray objectAtIndex:indexPath.section] objectForKey:@"rowValues"] objectAtIndex:indexPath.row] valueForKey:@"name"]; 

    // And create your cell like you usually do... 
} 

您的信息的東西,如果你需要排序的字母順序排列,你可以不喜歡它:

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"headerTitle" ascending:YES]; 
[myArray sortUsingDescriptors:[NSArray arrayWithObject:descriptor]];