Xcode_7 iOS_9 索引栏 Objective-C (11)

来源:互联网 发布:ubuntu安装1080驱动 编辑:程序博客网 时间:2024/05/21 17:59

1、在上一节项目上作一点修改,去掉searchBar以及其协议,去掉原有数据源,重新添加新的数据源,team_dictionary.plist,目录结构和新数据源内容如下:




2、ViewController.h:

////  ViewController.h//  TestProject////  Created by 侯家奇 on 16/8/17.//  Copyright © 2016年 侯家奇. All rights reserved.//#import <UIKit/UIKit.h>@interface ViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate>@property (nonatomic, strong) NSDictionary *dicData;@property (nonatomic, strong) NSArray *listGroupname;@end



3、ViewController.m:

////  ViewController.m//  TestProject////  Created by 侯家奇 on 16/8/17.//  Copyright © 2016年 侯家奇. All rights reserved.//#import "ViewController.h"#import "CustomCell.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib    NSBundle *bundle = [NSBundle mainBundle];    NSString *plistPath = [bundle pathForResource:@"team_dictionary" ofType:@"plist"];    self.dicData = [[NSDictionary alloc] initWithContentsOfFile:plistPath];    NSArray *tempList = [self.dicData allKeys];    self.listGroupname = [tempList sortedArrayUsingSelector:@selector(compare:)];}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    NSString *groupName = [self.listGroupname objectAtIndex:section];    NSArray *listTeam = [self.dicData objectForKey:groupName];    return [listTeam count];}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {    return [self.listGroupname count];}- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {    NSString *groupName = [self.listGroupname objectAtIndex:section];    return groupName;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    static NSString *CellIdentifier = @"CellIdentifier";    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];        NSUInteger section = [indexPath section];    NSUInteger row = [indexPath row];    NSString *groupName = [self.listGroupname objectAtIndex:section];    NSArray *listTeam = [self.dicData objectForKey:groupName];    cell.myLabel.text = [listTeam objectAtIndex:row];    return cell;    }- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {    NSMutableArray *listTitle = [[NSMutableArray alloc] initWithCapacity:[self.listGroupname count]];    for (NSString *item in self.listGroupname) {        NSString *title = [item substringToIndex:1];//把A组后面的组字给去掉        [listTitle addObject:title];    }    return listTitle;}@end


0 0