IOS学习笔记——UITableView (二)

本篇学习笔记将在上一篇学习笔记事例的基础上加上几个常用的数据源方法,分别是在头部和尾部显示文字以及在界面右边加上索引条这三个方法,并使用数据模型来填充数据。

首先创建一个新的类来作为数据模型,头文件代码如下:

#import <Foundation/Foundation.h>

@interface Province : NSObject

@property (nonatomic,copy) NSString *header;
@property (nonatomic,copy) NSString *footer;
@property (nonatomic,strong) NSArray *cities;

@end

为了方便起见,可以在该类中添加一个静态方法来快速创建一个数据模型

头文件方法声明如下:

+ (id) provinceWithHeader:(NSString *)header Footer:(NSString *)footer Cities:(NSArray *)cities;

接下来实现该方法,代码如下:

+(id)provinceWithHeader:(NSString *)header Footer:(NSString *)footer Cities:(NSArray *)cities{
    Province *province = [[Province alloc]init];
    
    province.header = header;
    province.footer = footer;
    province.cities = cities;
    
    return province;
}

这样就算简单了创建好了一个数据模型类。

然后在控制器  -(void) viewDidLoad 方法中初始化数据模型并加入一个数组中,代码如下:

- (void)viewDidLoad
{
    [super viewDidLoad];
    UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    tableView.dataSource = self;
    [self.view addSubview:tableView];
	
    _allProvince = @[
                   [Province provinceWithHeader:@"江苏" Footer:@"尾部测试" Cities:@[@"苏州",@"南京",@"常州",@"常熟",@"张家港"]],
                   [Province provinceWithHeader:@"湖北" Footer:@"尾部测试" Cities:@[@"武汉"]],
                   [Province provinceWithHeader:@"四川" Footer:@"尾部测试" Cities:@[@"成都",@"绵阳"]],
                   [Province provinceWithHeader:@"湖南" Footer:@"尾部测试" Cities:@[@"郑州",@"洛宁"]],
                   [Province provinceWithHeader:@"广东" Footer:@"尾部测试" Cities:@[@"深圳",@"广州",@"潮汕"]],
                   ];
}

这样数据模型就算完全搞定了。

下面列出头部和尾部显示文字以及在界面右边加上索引条这三个方法声明

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

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;


只要实现这三个方法,就可以在分组的UITableView控件的上方和下方看到你指定的文字(一般在上方都会显示该分组的组名,在下方一般会显示该分组的概述)以及在控件右侧看到一个纵向的导航条。

下面贴出完整的项目代码:

#import "WYViewController.h"
#import "Province.h"

@interface WYViewController ()<UITableViewDataSource>
{
    NSArray *_allProvince;
}

@end

@implementation WYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    tableView.dataSource = self; //指定数据源对象为控制器
    [self.view addSubview:tableView];
	
    _allProvince = @[
                   [Province provinceWithHeader:@"江苏" Footer:@"尾部测试" Cities:@[@"苏州",@"南京",@"常州",@"常熟",@"张家港"]],
                   [Province provinceWithHeader:@"湖北" Footer:@"尾部测试" Cities:@[@"武汉"]],
                   [Province provinceWithHeader:@"四川" Footer:@"尾部测试" Cities:@[@"成都",@"绵阳"]],
                   [Province provinceWithHeader:@"湖南" Footer:@"尾部测试" Cities:@[@"郑州",@"洛宁"]],
                   [Province provinceWithHeader:@"广东" Footer:@"尾部测试" Cities:@[@"深圳",@"广州",@"潮汕"]],
                   ];
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    return  _allProvince.count; /*返回分组的个数*/
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    Province *p = _allProvince[section];
    return  p.cities.count; /*返回每组有几行*/
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    Province *p = _allProvince[indexPath.section];
    cell.textLabel.text = p.cities[indexPath.row];
    return  cell; /*为每一组的每一行返回一个UITableViewCell*/
}

- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [_allProvince[section] header]; /*设置头部文字*/
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    return [_allProvince[section] footer]; /*设置尾部文字*/
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    NSMutableArray *array = [NSMutableArray array];
    for (Province *p in _allProvince) {
        [array addObject:p.header];
    }
    return array;/*遍历所有的省份,加入到一个可变数组中,返回*/
}
@end

运行结果如下图:


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值