显示索引

来源:互联网 发布:逛遍淘宝 编辑:程序博客网 时间:2024/05/21 21:42

//汉字索引

- (void)viewDidLoad

{

    [superviewDidLoad];

    

    NSString *path = [[NSBundlemainBundle] pathForResource:@"city"ofType:@"plist"];

   NSArray *cities = [NSArrayarrayWithContentsOfFile:path];

    self.keys = [[[NSMutableArrayalloc]init]autorelease];

    //创建26个可变数组

    NSMutableDictionary *dic = [[NSMutableDictionaryalloc]init];

   for (char character ='a'; character <= 'z'; character++) {

        NSMutableArray *array = [[NSMutableArrayalloc]init];

        [dicsetObject:array forKey:[NSStringstringWithFormat:@"%c",character]];

        [arrayrelease];

        [self.keysaddObject:[NSStringstringWithFormat:@"%c",character]];

    }

    //将数据按拼音首字母分别放入数组

   for (NSString *cityin cities) {

        //获取拼音首字母

        NSString *initial = [NSStringstringWithFormat:@"%c",pinyinFirstLetter([citycharacterAtIndex:0])];

       NSMutableArray *array = [dic objectForKey:initial];

        [arrayaddObject:city];

    }

    

    //将空组去掉

   for (char character ='a'; character <= 'z'; character++) {

       NSString *key = [NSStringstringWithFormat:@"%c",character];

       NSArray *array = [dic objectForKey:key];

       if (!array.count) {

            [dicremoveObjectForKey:key];

        }

    }

    self.keys = [[dic.allKeyssortedArrayUsingSelector:@selector(compare:)]mutableCopy];

    //self.keys = [[dic allKeys] mutableCopy];;

   self.list = dic;

    [dicrelease];

    


}





//显示索引行

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

{

    return self.keys;

}


//英文索引-----------   ------------    -----------        -------------------       --------------------    ------------       ----------------        -----------------------------        ------------------      ---------------------- 

- (void)viewDidLoad

{

    [superviewDidLoad];

    

    NSString *path = [[NSBundlemainBundle] pathForResource:@"sortednames"ofType:@"plist"];

    self.names = [NSDictionarydictionaryWithContentsOfFile:path];

    self.keys = [self.namesallKeys];

    //key按照字母表升序排列

    self.keys = [self.keyssortedArrayUsingSelector:@selector(compare:)];

}


- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


-(void)dealloc{

    [superdealloc];

}


#pragma mark - Table view data source


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return self.names.count;

}

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

{

   NSString *currentKey = [self.keysobjectAtIndex:section];

   NSArray *currentGroup = [self.namesobjectForKey:currentKey];

   return currentGroup.count;

}

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

{

   static NSString *CellIdentifier =@"CellIdentifier";

   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

   if (!cell) {

        cell = [[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier]autorelease];

    }

   NSString *currentKey = [self.keysobjectAtIndex:indexPath.section];

   NSArray *currentGroup = [self.namesobjectForKey:currentKey];

   NSString *currentName = [currentGroup objectAtIndex:indexPath.row];

    cell.textLabel.text = currentName;


   return cell;

}


-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

{

    return self.keys;

}





原创粉丝点击