// ViewController.m
// TableView基础
#import "ViewController.h"
#define kHead "head"
@interface ViewController ()<UITableViewDataSource , UITableViewDelegate> //<2>
{
NSArray *_allAry;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UITableView *myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
[self.view addSubview:myTableView];
//必须实现tableview的数据源才能使用tableview <1>
myTableView.dataSource = self;
myTableView.delegate = self;
// NSDictionary *sdDic = @{@"head": @"山东",
// @"foot": @"山东欢迎您!",
// @"cities" :@[@"烟台",@"济南",@"青岛",@"威海",@"淄博",@"潍坊",@"德州"]
// };
_allAry = @[
@{@kHead: @"山东",
@"foot": @"山东欢迎您!",
@"cities" :@[@"烟台",@"济南",@"青岛",@"威海",@"淄博",@"潍坊",@"德州"]
},
@{@kHead: @"北京",
@"foot": @"北京欢迎您!",
@"cities" :@[@"海淀区",@"石景山区",@"朝阳区",@"东城区",@"西城区",@"昌平区"]
},
@{@kHead: @"江苏",
@"foot": @"江苏欢迎您!",
@"cities" :@[@"南京",@"徐州",@"苏州",@"常州"]
},
@{@kHead: @"浙江",
@"foot": @"浙江欢迎您!",
@"cities" :@[@"杭州",@"宁波",@"义务"]
}
];
}
#pragma mark 3-1. 分几组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return _allAry.count;
}
#pragma mark 3-2. 返回每个分区有几行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[_allAry[section] objectForKey:@"cities"] count];
}
#pragma mark 3-3. 每一行显示的内容
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
cell.textLabel.text = [_allAry[indexPath.section] objectForKey:@"cities"][indexPath.row];
return cell;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [_allAry[section] objectForKey:@kHead];
}
-(NSString*)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
return [_allAry[section] objectForKey:@"foot"];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 70;
}
#pragma 调整每行的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end
UItableView基本步骤(代码实现中还包含快速创建数组)
最新推荐文章于 2017-08-11 23:15:42 发布