表视图的简单使用-UITableView

//

//  ViewController.m

//  city

//

//  Created by imac on 15/8/20.

//  Copyright (c) 2015
. All rights reserved.

//


#import "ViewController.h"


@interface ViewController ()<UITableViewDataSource>


@property(nonatomic,strong)NSDictionary *dataDic;

@property(nonatomic,strong)NSArray *dataArray;


@property(nonatomic,strong)UITableView *tableView;



@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];

    

//    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];

    

    [self.view addSubview:_tableView];

    

    _tableView.dataSource = self;

    

    [self dataArray];

    [self dataDic];

    

}

#pragma mark 城市数据

-(NSDictionary *)dataDic{

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

    _dataDic = [NSDictionary dictionaryWithContentsOfFile:path];

    return _dataDic;


}

#pragma mark 省级数据

-(NSArray *)dataArray{

    

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

    _dataArray = [NSArray arrayWithContentsOfFile:path];

    return _dataArray;

}

#pragma mark 分组数

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

    

    return _dataArray.count;

}

#pragma mark 每组个数

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

    

    NSString *key = _dataArray[section];

    NSArray *arrTemp = [NSArray array];

    arrTemp = [_dataDic valueForKey:key];

    return arrTemp.count;

    

}

#pragma mark 每行显示的内容

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

    

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

    

    NSString *key = _dataArray[indexPath.section];

    NSArray *arrTemp = [NSArray array];

    arrTemp = [_dataDic valueForKey:key];


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

    

    return cell;

}

#pragma mark 每组的标题

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

    

    return _dataArray[section];

}



@end


需要使用到两个plist的文件,进行本地数据加载


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值