你好,歡迎來到IOS教程網

 Ios教程網 >> IOS訊息 >> 關於IOS >> 顯示索引

顯示索引

編輯:關於IOS

//漢字索引

- (void)viewDidLoad {     [super viewDidLoad];          NSString *path = [[NSBundle mainBundle] pathForResource:@"city" ofType:@"plist"];     NSArray *cities = [NSArray arrayWithContentsOfFile:path];     self.keys = [[[NSMutableArray alloc]init]autorelease];     //創建26個可變數組     NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];     for (char character = 'a'; character <= 'z'; character++) {         NSMutableArray *array = [[NSMutableArray alloc]init];         [dic setObject:array forKey:[NSString stringWithFormat:@"%c",character]];         [array release];         [self.keys addObject:[NSString stringWithFormat:@"%c",character]];     }     //將數據按拼音首字母分別放入數組     for (NSString *city in cities) {         //獲取拼音首字母         NSString *initial = [NSString stringWithFormat:@"%c",pinyinFirstLetter([city characterAtIndex:0])];         NSMutableArray *array = [dic objectForKey:initial];         [array addObject:city];     }          //將空組去掉     for (char character = 'a'; character <= 'z'; character++) {         NSString *key = [NSString stringWithFormat:@"%c",character];         NSArray *array = [dic objectForKey:key];         if (!array.count) {             [dic removeObjectForKey:key];         }     }     self.keys = [[dic.allKeys sortedArrayUsingSelector:@selector(compare:)] mutableCopy];     //self.keys = [[dic allKeys] mutableCopy];;     self.list = dic;     [dic release];        }         //顯示索引行 -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {     return self.keys; }   //英文索引-----------   ------------    -----------        -------------------       --------------------    ------------       ----------------        -----------------------------        ------------------      ----------------------  - (void)viewDidLoad {     [super viewDidLoad];          NSString *path = [[NSBundle mainBundle] pathForResource:@"sortednames" ofType:@"plist"];     self.names = [NSDictionary dictionaryWithContentsOfFile:path];     self.keys = [self.names allKeys];     //將key按照字母表升序排列     self.keys = [self.keys sortedArrayUsingSelector:@selector(compare:)]; }   - (void)didReceiveMemoryWarning {     [super didReceiveMemoryWarning];     // Dispose of any resources that can be recreated. }   -(void)dealloc{     [super dealloc]; }   #pragma mark - Table view data source   - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {     return self.names.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {     NSString *currentKey = [self.keys objectAtIndex:section];     NSArray *currentGroup = [self.names objectForKey:currentKey];     return currentGroup.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {     static NSString *CellIdentifier = @"CellIdentifier";     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];     if (!cell) {         cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];     }     NSString *currentKey = [self.keys objectAtIndex:indexPath.section];     NSArray *currentGroup = [self.names objectForKey:currentKey];     NSString *currentName = [currentGroup objectAtIndex:indexPath.row];     cell.textLabel.text = currentName;       return cell; }   -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {     return self.keys; }    
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved