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

UItableView基本步骤(代码实现中还包含快速创建数组)

2015-03-06 16:27 211 查看
//  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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐