2012-01-09 157 views
1

我知道如何使用UITableview委託和數據源協議方法。所以,我可以和章節一起工作。現在我想使用單個查詢從數據庫中提取所有記錄,即從聯繫人順序firstname asc中選擇firstname,lastname。 (現在我使用26個人查詢,我知道這不是正確的解決方案)UITableview與按排序順序排列的名稱部分

現在我想分組的聯繫人根據其字母的部分。如果沒有聯繫人從字母S開始,那麼S的部分不應出現。你能否給我提供正確的代碼或任何教程?謝謝

你能改變這個腳本嗎? (void)read_data_fromDB { objectsForCharacters = [[NSMutableDictionary alloc] init];

sqlite3 *db = [eikardAppDelegate getNewDBConnection]; 
arr_sectionTitles = [[NSMutableArray alloc] init]; 
for(char c='A';c<='Z';c++) 
{ 
    NSMutableString *query = nil; 

    query = [NSMutableString stringWithFormat:@"select first_name, middle_name, last_name from phonebook where first_name like '%c%%';",c]; 


    const char *sql = [query UTF8String]; 
    sqlite3_stmt *selectAllStmt = nil; 

    if(sqlite3_prepare_v2(db,sql, -1, &selectAllStmt, NULL)!= SQLITE_OK) 
     NSAssert1(0,@"error preparing statement",sqlite3_errmsg(db)); 
    else 
    { 
     NSMutableArray *arr_persons = [[NSMutableArray alloc] init]; 
     while(sqlite3_step(selectAllStmt)==SQLITE_ROW) 
     { 
      //NSLog(@"Firstname : %@",query); 
      PersonInfo *person =[[PersonInfo alloc] init]; 

      char *chrstr =(char *)sqlite3_column_text(selectAllStmt, 0); 
      if(chrstr !=NULL) 
      { 
       person.str_firstName = [NSString stringWithUTF8String:chrstr]; 
       NSLog(@"Firstname : %@",person.str_firstName); 
      }  
      chrstr =(char *)sqlite3_column_text(selectAllStmt, 1); 
      if(chrstr !=NULL) 
      { 

       person.str_middleName = [NSString stringWithUTF8String:chrstr]; 
       NSLog(@"Middlename : %@",[NSString stringWithUTF8String:chrstr]); 
      } 
      chrstr =(char *)sqlite3_column_text(selectAllStmt, 2); 
      if(chrstr !=NULL) 
      { 
       person.str_lastName = [NSString stringWithUTF8String:chrstr]; 
       NSLog(@"Lastname : %@",person.str_lastName); 
      }    


      [arr_persons addObject:person]; 

      [person release]; 
     } 
     if([arr_persons count]>0) 
     { 
      NSString *keyValue = [NSString stringWithFormat:@"%c",c]; 
      [objectsForCharacters setObject:arr_persons forKey:keyValue]; 
      [arr_sectionTitles addObject:keyValue]; 

     } 
     //[arr_persons release]; 

    } 
    sqlite3_finalize(selectAllStmt); 
} 
sqlite3_close(db); } 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 

    return [arr_sectionTitles count]; 
} 


// Customize the number of rows in the table view. 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    NSString *secTitle = [arr_sectionTitles objectAtIndex:section]; 

    return [[objectsForCharacters objectForKey:secTitle] count]; 
} 
+0

您是否嘗試過NSPredicator? – 2012-01-09 11:30:48

+0

我不知道如何使用SQLite查詢使用NSPredicate。你能給我一個樣品嗎? – dinesh 2012-01-09 11:41:47

+0

您可以在獲取數組後檢索所有數據,然後在該數組上執行NSPredicate。 – 2012-01-09 11:50:03

回答

1

它取決於數據的來源。如果您使用的是NSFetchedResultsController,則可以使用initWithFetcRequest:managedObjectContext:sectionNameKeyPath:cacheName:以及sectionNameKeyPath:的屬性的鍵名稱。你會多節排序的結果。

如果你在數組中有來自查詢的結果數據(對於整個選擇,而不是僅僅是26個字符中的一個),那麼你最好重新排列數組中的有序數據。也就是說,主數組的每個元素都是索引中每個字母的結果數組。

如果使用[[UILocalizedIndexedCollat​​ion currentCollat​​ion] sectionIndexTitles]作爲節的標題,則很容易實現表的索引。您不必爲每個索引創建一個部分,您將在方法tableView中引用每個索引的正確部分:sectionForSectionIndexTitle:atIndex: