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

UITableView---分组 省份城市列表+ 右列索引

时间:2015-05-25 13:06:51      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:uitableview   索引   plist   uitableviewcell   nsmutablearray   

UITableView分组显示省份城市列表+右侧索引

例题:分组名为省份名称,组内容为对应省份的城市名称,点击右边的索引,将对应的省份分组显示到到第一行。

显示效果如下:

技术分享


对应plist文件如下图所示:

技术分享技术分享技术分享


#import "ViewController.h"


//遵守协议

@interface ViewController () <UITableViewDelegate,UITableViewDataSource>


//UITableView

@property (nonatomic,weak)UITableView * tableView;


//数据数组

@property (nonatomic,strong) NSMutableArray * dataArray;


//cities字典

@property (nonatomic,strong) NSMutableDictionary * cityDic;


@property (nonatomic,strong)NSMutableArray * cityArray;


//provinces数组

@property (nonatomic,strong) NSMutableArray * procincesArray;


@property (nonatomic,strong) NSMutableString * proviNameStr;


//strong ---数据类型

//weak   ---控件之类的,切换页面后若果控件不显示,释放掉


@end


@implementation ViewController


- (void)viewDidLoad

{

    [superviewDidLoad];

    

    //加载数据

    [self_loadData];

    

    UITableView * table=[[UITableViewalloc]initWithFrame:CGRectMake(0,20, self.view.frame.size.width,self.view.frame.size.height-20)style:UITableViewStyleGrouped];

    table.delegate=self;

    table.dataSource=self;

    

   self.tableView=table;

    table.sectionFooterHeight=0;  //如果有代理的话,此行没有效果,以代理为先

    table.sectionHeaderHeight=100;

    [self.viewaddSubview:table];

}


#pragma mark - 懒加载

- (NSMutableArray *)procincesArray

{

    if (_procincesArray ==nil)

    {

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

        

        NSMutableDictionary * dic=[NSMutableDictionarydictionaryWithContentsOfFile:path];

        

       _procincesArray = dic[@"provinces"];

    }

    return_procincesArray;

    

}


#pragma mark - 加载数据

- (void) _loadData

{

    //dataArray的内容从 plist文件中取得

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

    NSMutableDictionary * dic=[NSMutableDictionarydictionaryWithContentsOfFile:path];

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

    self.procincesArray=dic[@"provinces"];

}



#pragma mark - UITableViewDatasource

//返回行数

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

{

    //NSLog(@"%@",self.procincesArray[section]);    //测试代码

   NSString * tem = self.procincesArray[section];

   return ((NSArray *)(self.cityDic[tem])).count;

}


//返回cell

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

{

   static NSString * identy=@"table";

    UITableViewCell * cell=[tableViewdequeueReusableCellWithIdentifier:identy];

   if (!cell)

    {

        cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identy];

    }

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

    NSMutableDictionary * dic=[NSMutableDictionarydictionaryWithContentsOfFile:path];

    self.proviNameStr=self.procincesArray[indexPath.section];


    self.cityArray= (dic[@"cities"])[self.proviNameStr];

//    NSLog(@" %@--%@",self.proviNameStr, dic[self.proviNameStr]);

//    NSLog(@"%@",self.cityArray);

//    NSLog(@"%li", indexPath.section);

//    cell.textLabel.text=self.procincesArray[indexPath.row];

    cell.textLabel.text=self.cityArray[indexPath.row];

    

   return cell;

    

}



#pragma mark - 返回分组数量

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView

{

    //NSLog(@"___++++%ld",self.procincesArray.count);

    returnself.procincesArray.count;  //分组数组长度

}


#pragma mark - UITableViewDelegate

//设置分组的头标题

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

{

   return self.procincesArray[section];

}



#pragma mark - 设置头部的高度

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{

   return 60;

}



#pragma mark - 设置尾部的高度

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section

{

   return 0;

}


#pragma mark - 索引列

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

{

    returnself.procincesArray;

}




- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    

}


@end


UITableView---分组 省份城市列表+ 右列索引

标签:uitableview   索引   plist   uitableviewcell   nsmutablearray   

原文地址:http://blog.csdn.net/qq_27364431/article/details/45967477

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