码迷,mamicode.com
首页 > 其他好文 > 详细

tabview分组。仿qq列表

时间:2015-05-26 21:16:51      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

代码如下

#import "ViewController.h"

#import "JRProvince.h"

 

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

 

/** 省份数据数组*/

@property(nonatomic,strong) NSMutableArray * province;

 

/** 各个地市数据*/

@property(nonatomic,strong) NSDictionary * cities;

 

/** tabView*/

@property(nonatomic,weak) UITableView * tableView;

 

 

 

@end

 

@implementation ViewController

 

 

-(NSMutableArray *)province{

 

    if (_province==nil) {

        _province=[NSMutableArray array];

    }

    return _province;

}

 

- (void)viewDidLoad {

    [super viewDidLoad];

 

    //加载数据

    [self  _loadData];

 

    

    //创建视图

    [self  _initSubviews];

    

    

}

 

 

#pragma mark - 加载数据

- (void) _loadData{

 

    //1 读取数据

    NSString *path=[[NSBundle mainBundle] pathForResource:@"cities.plist" ofType:nil];

    NSDictionary * dic=[NSDictionary dictionaryWithContentsOfFile:path];

    

    //2 封装省份和首字母

    NSArray * prov=dic[@"provinces"];

    for (NSString * str in prov) {

        JRProvince * pro=[JRProvince getProWithName:str];

        [self.province addObject:pro];

    }

    

    //3 封装地市信息

    self.cities=dic[@"cities"];

    

    //4 省份排序

    /*

    [self.province sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {

        

        JRProvince * m1=obj1;

        JRProvince * m2=obj2;

        

        return m1.pinyin>m2.pinyin;

    }];*/

    

    

    

    

}

 

#pragma mark - 加载视图

- (void) _initSubviews{

    UITableView * tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];

    tableView.sectionFooterHeight=0;

    tableView.delegate=self;

    tableView.dataSource=self;

    self.tableView=tableView;

    

   

    

//    tableView.sectionIndexColor=[UIColor redColor];

//    tableView.sectionIndexBackgroundColor=[UIColor greenColor];

//    tableView.sectionIndexTrackingBackgroundColor=[UIColor yellowColor];

    

    [self.view addSubview:tableView];

 

}

 

 

#pragma mark - UITableViewDatasource

 

#pragma mark * 返回行数

- (NSInteger ) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    

    //结合省份的数据数组计算每个省份的地市个数

    JRProvince * pro=self.province[section];

    NSArray *tempArray=self.cities[pro.name];

    

    return tempArray.count;

}

 

#pragma mark * 返回分组数量

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{

    return self.province.count;

}

 

 

 

#pragma mark * 返回cell

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString * identy=@"mytable";

    UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:identy];

    if (!cell) {

        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identy];

        cell.selectionStyle=UITableViewCellSelectionStyleNone;

    }

    

    //根据省份获取城市列表,并且根据行索引返回对应的城市

    JRProvince * pro=self.province[indexPath.section];

    NSArray *tempArray=self.cities[pro.name];

    cell.textLabel.text=tempArray[indexPath.row];

    return cell;

    

}

 

#pragma mark * 返回分组标题

- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    JRProvince * pro=self.province[section];

    return pro.name;

}

 

 

#pragma mark * 返回分组索引

- (NSArray *) sectionIndexTitlesForTableView:(UITableView *)tableView{

 

    //循环遍历省份的数组,返回对应的首字母作为索引

    NSMutableArray * indexArray=[NSMutableArray array];

    

    for (JRProvince * pro in self.province) {

        

        //防止重复添加

        if(![indexArray containsObject:[NSString stringWithFormat:@"%c",pro.pinyin]]){

           [indexArray addObject:[NSString stringWithFormat:@"%c",pro.pinyin]];

        }

        

        

    }

    return indexArray;

}

 

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{

 

 

    /*

    int tem=0;

    for (int i=0;i<self.province.count ;i++) {

        JRProvince * pro=self.province[i];

        if (pro.pinyin==[title characterAtIndex:0]) {

            tem=i;

            break;

        }

    }

    */

    

    int tem=0;

    for (JRProvince * pro in self.province) {

        

        if (pro.pinyin==[title characterAtIndex:0]) {

            break;

        }

 

        tem++;

     }

    

    

    return tem;

}

 

 

#pragma mark * 选中Cell

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

 

    NSIndexPath * index=[NSIndexPath indexPathForRow:0 inSection:indexPath.section];

    [self.tableView scrollToRowAtIndexPath:index atScrollPosition:UITableViewScrollPositionTop animated:YES];

}

 

@end

 

tabview分组。仿qq列表

标签:

原文地址:http://www.cnblogs.com/yy515700/p/4531612.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!