標籤:ios delegate ios開發 objective-c 索引
索引是用來輔助查詢。
原則:
- 索引標題不能與顯示的標題完全一樣;
- 索引應該具有一定的代表性,能夠代表一個資料集合;
- 如果採用了索引列表視圖,一般情況下就不再使用擴充視圖。(容易點到)
會重新到的資料來源方法:
tableView: numberOfRowsInSection: ------擷取某節的行數
tableView:cellForRowAtIndexPath: -------Cell資料的實現
numberofSectionInTableView: ----------擷取節數
tableView:titleForHeaderInSection: --------區段標頭
sectionIndexTitlesForTableView: --------擷取索引
@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開發)