標籤:
該指數是用來協助查詢。
原則上:
- 索引的標題是不完全一樣的標題顯示;
- 指數應該具有一定的代表性,它可表示一組資料;
- 假設索引列表視圖。在一般情況下不再使用擴充視圖。
(easy指向)
會又一次到的資料來源方法:
tableView: numberOfRowsInSection: ------擷取某節的行數
tableView:cellForRowAtIndexPath: -------Cell資料的實現
numberofSectionInTableView: ----------擷取節數
tableView:titleForHeaderInSection: --------區段標頭
sectionIndexTitlesForTableView: --------擷取索引
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGl5YWt1bjE5OTA=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" >
@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; NSBundle *bundle = [NSBundle mainBundle]; NSString *plistPath = [bundle pathForResource:@"team_dictionary" ofType:@"plist"]; // 擷取屬性列表檔案裡的所有資料 self.dicData = [[NSDictionary alloc] initWithContentsOfFile:plistPath]; NSArray *tempList = [self.dicData allKeys]; // 對key進行排序 // selector為SEL類型,sortedArrayUsingSelector方法的參數必須是selector self.listGroupname = [tempList sortedArrayUsingSelector:@selector(compare:)];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}#pragma mark 重寫資料來源方法- (NSInteger) tableView: (UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ // 依照節索引從小組名數組中獲得組名 NSString *groupName = [self.listGroupname objectAtIndex:section]; // 將組名作為key,從字典中取出球隊數組集合 // 由於儲存的時候是雜湊結構 NSArray *listTeams = [self.dicData objectForKey:groupName]; // 這一節一共同擁有多少球隊 return [listTeams count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"CellIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } // 獲得選擇的節 A組即第1節 NSUInteger section = [indexPath section]; // 獲得選擇節中選中的行索引 -- A組第1、第2.。。
NSUInteger row = [indexPath row]; // 依照節索引從小組名數組中獲得組名 -- A組 NSString *groupName = [self.listGroupname objectAtIndex:section]; // 將組名作為key,從字典中取出球隊數組集合 NSArray *listTeams = [self.dicData objectForKey:groupName]; cell.textLabel.text = [listTeams objectAtIndex:row]; return cell;}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ // 該節行數 return [self.listGroupname count];}- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ // 該節的節頭 NSString *groupName = [self.listGroupname objectAtIndex:section]; return groupName;}- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ // 加入索引 NSMutableArray *listTitles = [[NSMutableArray alloc] initWithCapacity:[self.listGroupname count]]; // 把A組改為A for (NSString *item in self.listGroupname) { NSString *title = [item substringToIndex:1]; // 擷取的第一個字元 [listTitles addObject: title]; // 字串數組追加的最後位置 } return listTitles;}@end
注意:
在這個故事中要配置版本tableview委託協議。。!!
加入指數(IOS開發)