2013-03-25 56 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

为什么呢?