您的位置:首页 > 产品设计 > UI/UE

表视图的简单使用-UITableView

2015-08-20 20:28 344 查看
//

//  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的文件,进行本地数据加载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS ui uitableview