添加索引(IOS開發)

來源:互聯網
上載者:User

標籤: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開發)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.