UITableView的数据源设置和简单介绍

本文介绍了UITableView数据源的设置,包括如何使控制器遵循UITableViewDataSource协议,设置数据源与控制器的关联,以及实现显示数据所需的方法。讲解了numberOfSectionsInTableView、tableView:numberOfRowsInSection:、tableView:cellForRowAtIndexPath:这三个关键方法的作用,并提到了UITableView的风格设置,如Grouped和Plain对于界面显示的影响。

视频为:传智播客2014年第四期3-6月份(MJ老师的视频) – 第六天(UITableView控件使用)课程

设置数据源对象必须遵守UITableView的数据源协议UITableViewDataSource

tableView数据源绑定:
1.一般使用控制器作为UITableView的数据源,控制器需要遵守 UITableViewDataSource 协议
2.添加UITableView的属性
3.将tableView的数据源与控制器关联 (使用代码关联;使用连线关联-与关联代理的连线方式相同)

@interface ViewController () <UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 设置数据源
    self.tableView.dataSource = self;
}

tableView展示数据一般需要实现如下方法:

1.调用数据源的下面方法得知一共有多少组数据 (如果只有一组数据则不用实现该方法,该方法的默认值就为1)

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

2.调用数据源的下面方法得知每一组有多少行数据

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

3.调用数据源的下面方法得知每一行显示什么内容

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

以上三个方法的执行顺序,打印结果如下:--没弄明白 numberOfSectionsInTableView 会调用三次

2016-01-27 22:43:02.430 01-汽车品牌[1495:120339] numberOfSectionsInTableView-一共有多少组数据
2016-01-27 22:43:02.431 01-汽车品牌[1495:120339] numberOfRowsInSection -- 1
2016-01-27 22:43:02.431 01-汽车品牌[1495:120339] numberOfRowsInSection -- 0
2016-01-27 22:43:02.431 01-汽车品牌[1495:120339] numberOfSectionsInTableView-一共有多少组数据
2016-01-27 22:43:02.432 01-汽车品牌[1495:120339] numberOfRowsInSection -- 1
2016-01-27 22:43:02.432 01-汽车品牌[1495:120339] numberOfRowsInSection -- 0
2016-01-27 22:43:02.434 01-汽车品牌[1495:120339] numberOfSectionsInTableView-一共有多少组数据
2016-01-27 22:43:02.434 01-汽车品牌[1495:120339] numberOfRowsInSection -- 1
2016-01-27 22:43:02.434 01-汽车品牌[1495:120339] numberOfRowsInSection -- 0
2016-01-27 22:43:02.435 01-汽车品牌[1495:120339] cellForRowAtIndexPath 第0组 第0行
2016-01-27 22:43:02.437 01-汽车品牌[1495:120339] cellForRowAtIndexPath 第0组 第1行
2016-01-27 22:43:02.437 01-汽车品牌[1495:120339] cellForRowAtIndexPath 第0组 第2行
2016-01-27 22:43:02.438 01-汽车品牌[1495:120339] cellForRowAtIndexPath 第1组 第0行
2016-01-27 22:43:02.453 01-汽车品牌[1495:120339] cellForRowAtIndexPath 第1组 第1行
2016-01-27 22:43:02.454 01-汽车品牌[1495:120339] cellForRowAtIndexPath 第1组 第2行
2016-01-27 22:43:02.454 01-汽车品牌[1495:120339] cellForRowAtIndexPath 第1组 第3行

分组的头部信息和尾部信息

/**
 *  第section组显示怎样的头部信息
 */
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    // section的数值代表当前组索引
}

/**
 * 第section组显示怎样的尾部信息
 */
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    // section的数值代表当前组索引
}

多组数据的基本使用-小项目代码

#import "MJViewController.h"

@interface MJViewController () <UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation MJViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // 设置数据源
    self.tableView.dataSource = self;
}

#pragma mark - 数据源方法
/**
 *  一共有多少组数据
 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSLog(@"numberOfSectionsInTableView-一共有多少组数据");
    return 2;
}

/**
 *  第section组有多少行
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"numberOfRowsInSection-%d", section);

    if (section == 0) {
        return 3;
    } else {
        return 4;
    }
}

/**
 *  每一行显示怎样的内容(cell)
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"cellForRowAtIndexPath-%d组%d行", indexPath.section, indexPath.row);
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

    if (indexPath.section == 0) { // 德系品牌(第0组)

        if (indexPath.row == 0) { // 第0组的第0行
            cell.textLabel.text = @"奥迪";
        } else if (indexPath.row == 1) { // 第0组的第1行
            cell.textLabel.text = @"宝马";
        } else if (indexPath.row == 2) {
            cell.textLabel.text = @"奔驰";
        }

    } else if (indexPath.section == 1) { // 日韩品牌(第1组)

        if (indexPath.row == 0) { // 第1组的第0行
            cell.textLabel.text = @"本田";
        } else if (indexPath.row == 1) { // 第1组的第1行
            cell.textLabel.text = @"丰田";
        } else if (indexPath.row == 2) {
            cell.textLabel.text = @"铃木";
        } else if (indexPath.row == 3) {
            cell.textLabel.text = @"三菱";
        }
    }

    return cell;
}

/**
 *  第section组显示怎样的头部信息
 */
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if(section == 0){
        return @"德系品牌";
    }
    else if(section == 1){
        return @"日韩品牌";
    } else {
        return @"欧系品牌";
    }
}

/**
 * 第section组显示怎样的尾部信息
 */
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    if(section == 0){
        return @"世界一流品牌";
    }
    else if(section == 1){
        return @"牛逼哄哄";
    } else {
        return @"价格昂贵";
    }
}

@end

运行结果界面如下:

注:运行界面的分组如果不是很明确,需要将UITableView的 style 属性改为 Grouped,Grouped适合组的头尾都有说明的情况下

style属性为Plain时:界面如下
这里写图片描述

注:有个小细节,从ios7以后,style属性为Plain时,在分组向上推的时候,分组的名称会自动停留在顶部;style属性为Grouped时则不会有这个效果。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值