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

//  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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值