2013-03-25 55 views
0

我有一個NSArray其中包括一個鍵列表,這個數組來自.plist。TableView部分

在這一刻我寫這個數組在UITableView,但這不是排序和分區。 我想對此數組進行排序,並希望在此數組中的每個字符的第一個字符開始的UITableView中有章節。

作爲例子: Sectionname: 「A」 CELLTEXT: 「Ahorn酒店」

我希望你能得到它。 我現在代碼:

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

NSArray * sections = [temp allValues]; 

NSUInteger *tablesections = [sections count]; 

return tablesections; 


} 

和:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath 

    NSArray * values = [temp allValues]; 

[EingabeListe addObjectsFromArray:values]; 

char szDecryptetKey[256]; 
sleep(0.5); 


NSString *cellValue = [values objectAtIndex:indexPath.row]; 
const char *cString = [cellValue cStringUsingEncoding:NSASCIIStringEncoding]; 

DecryptKey(cString, szDecryptetKey); 

NSString *pnssDecryptetKey = [NSString stringWithFormat:@"%s",szDecryptetKey]; 


cell.textLabel.font = [UIFont systemFontOfSize:11.0]; 
cell.textLabel.text = pnssDecryptetKey; 

return cell; 

感謝

回答

1

我可能不會在單個陣列中離開這個。我會把它放到一個NSDictionary,其中字母表的每個字母都是一個字母表中的每個第一個字母(和一個部分)。然後獲取單個部分的內容就像在字典中查找想要的第一個字母一樣簡單。

首先按字母順序排列數組。這已被問了很多次,但這裏的one answer

接下來,迭代數組並將其添加到基於第一個字母的字典中。字典中的每個「值」都是一個數組,而不僅僅是一個單獨的項目。所以當你第一次遇到一個字母(比如'g')時,你會在字典中創建「g」鍵並添加一個NSMutable數組作爲值。

作爲一個方面說明,我沒有添加代碼,因爲這聽起來像一個家庭作業(當然,我可能是錯的)。雖然我想幫忙,但我不想爲你做。這就是說,如果不清楚或者你想獲得更多幫助,我會很樂意提供)。

0

因爲你需要修改代碼。我將解釋,

步驟:

  1. 初始化基礎上,按字母順序,按字母順序排列和過濾器使用源陣列。
  2. 現在dataSource字典包含按字母過濾的源數據數組。
  3. 現在沒有。部分將沒有。字典中的數組。
  4. 爲數據源字典中的每個部分加載數據源數組。

初始化字母陣列和數據源陣列

alphabet = [[NSArray alloc]initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K", 
            @"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",nil]; 

    dataSource = [[NSMutableDictionary alloc]initWithCapacity:[alphabet count]]; 

    sourceArray = [NSArray arrayWithObjects:@"Azz",@"ax",@"aje",@"B",@"C",@"Ca",@"D",@"DD",@"E",@"EE",@"F",@"G",@"F", nil]; 

篩選源陣列和作爲字母表

for(int i = 0; i<[alphabet count]; i ++) 
{ 
NSArray *filteredArray = [sourceArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF BEGINSWITH[C] %@", [alphabet objectAtIndex:i]]]; 
    if([filteredArray count]>0) 
    dataSource setObject:[filteredArray sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)] forKey:[alphabet objectAtIndex:i]]; // Dictionary containing sorted array of data with key as alphabets 

} 

數據與關鍵值添加到字典和U需要定製沒有。段和行的委託方法。查看示例代碼。

像這樣

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

    // Return the number of sections. 
    return [[dataSource allKeys] count]; 
} 

     - (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section 
{ 
       return [[dataSource allKeys] objectAtIndex:section]; 
} 

使用此代碼,它的工作對我來說很好。

整個源代碼:

 - (void)viewDidLoad 
     { 
      [super viewDidLoad]; 


      alphabet = [[NSArray alloc]initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K", 
         @"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",nil]; 

      dataSource = [[NSMutableDictionary alloc]initWithCapacity:[alphabet count]]; 

      sourceArray = [NSArray arrayWithObjects:@"Azz",@"ax",@"aje",@"B",@"C",@"Ca",@"D",@"DD",@"E",@"EE",@"F",@"G",@"F", nil]; 

      for(int i = 0; i<[alphabet count]; i ++) 
      { 
       NSArray *filteredArray = [sourceArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF BEGINSWITH[C] %@", [alphabet objectAtIndex:i]]]; 
       if([filteredArray count]>0) 
        [dataSource setObject:[filteredArray sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)] forKey:[alphabet objectAtIndex:i]]; // Dictionary containing sorted array of data with key as alphabets 

      } 
      NSLog(@"Filtered Array %@", dataSource); 


     } 


     #pragma mark - Table view data source 

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

    // Return the number of sections. 
    return [[dataSource allKeys] count]; 
} 

     - (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section 
{ 
       return [[dataSource allKeys] objectAtIndex:section]; 
} 


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


      return [[dataSource objectForKey:[[dataSource allKeys] objectAtIndex:section]] count]; 

     } 

     - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
     { 

      return 20; 
     } 
     - (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath 
     { 
      return NO; 
     } 


     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
     { 

      originalSource = [dataSource objectForKey:[[dataSource allKeys] objectAtIndex:indexPath.section]]; 

      static NSString *CellIdentifier = @"Cell"; 
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
      if (cell == nil) { 
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
      } 
      cell.textLabel.text = [NSString stringWithFormat:@"%@",[originalSource objectAtIndex:indexPath.row]]; 
      return cell; 

     } 


     #pragma mark - Table view delegate 

     - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
     { 

     } 

輸出會是這樣。

enter image description here

希望這個代碼將幫助ü。我知道它很難理解整個代碼。有任何疑問隨時問。

0

我通常爲這些類型的應用程序使用免費的Sensible TableView框架。你直接把數組放到框架中,它會自動爲你排序和創建所有的部分。應該帶你幾分鐘實施,所以我建議檢查一下。

0

嗨,感謝它工作得很好。 但我只能看到我的.plist的第一個字符。

我想錯線是這個:

NSString *cellValue = [values objectAtIndex:indexPath.row]; 

但這裏是我的代碼:

[super viewDidLoad]; 
self.EingabeListe = [NSMutableArray arrayWithCapacity:20]; 
NSIndexPath *indexPath = 0; 


alphabet = [[NSArray alloc]initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K", 
      @"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",@"123",nil]; 

datasource = [[NSMutableDictionary alloc]initWithCapacity:[alphabet count]]; 

int m = 1; 
int n = 0; 
NSArray * values = [temp allValues]; 
int c = [values count]; 

// 

char szDecryptetKey[256]; 
sleep(0.5); 


while (m != 0) { 
    if (n == c){ 
     m = 0; 
    }else{ 
     NSString *cellValue = [values objectAtIndex:indexPath.row]; 
     const char *cString = [cellValue cStringUsingEncoding:NSASCIIStringEncoding]; 



     NSString *pnssDecryptetKey = [NSString stringWithFormat:@"%s",szDecryptetKey]; 

     [EingabeListe addObject:pnssDecryptetKey]; 
     pnssDecryptetKey = 0; 
    } 
    n++; 

} 

for(int i = 0; i<[alphabet count]; i ++) 
{ 
    NSArray *filteredArray = [EingabeListe filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF BEGINSWITH[C] %@", [alphabet objectAtIndex:i]]]; 
    if([filteredArray count]>0) 
     [datasource setObject:[filteredArray sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)] forKey:[alphabet objectAtIndex:i]]; // Dictionary containing sorted array of data with key as alphabets 

} 
} 

在我的.plist一些測試項和值是這樣的:

Value Key sqq hi zzz例如 egg bb

但my.plist我只能看到: SQQ SQQ SQQ

爲什麼呢?