iOS基础控件--UITableView简单介绍

UITableView的简单使用

直接先上一段简单UITableView的使用代码。

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>//添加协议
{
    //声明tableView和数据源
    UITableView *table;
    NSMutableArray *dataSource;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createDataSource];
    [self createTableView];
}

//创建数据源
- (void)createDataSource{
    //初始化数据源数组
    dataSource = [NSMutableArray arrayWithCapacity:0];
    //循环e给数据源中添加元素
    for (int i = 0; i < 20; i++) {
        NSString *string = [NSString stringWithFormat:@"第%d行", i+1];
        [dataSource addObject:string];
    }
    //刷新视图
    [table reloadData];
}

//创建tableView视图
- (void)createTableView{
    //创建并设置位置大小和风格形式
    /*
     typedef NS_ENUM(NSInteger, UITableViewStyle) {
        UITableViewStylePlain,          // 无分组样式
        UITableViewStyleGrouped         // 分组样式
     };
     */
    table = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    //设置代理
    table.delegate = self;
    table.dataSource = self;
    //设置纵横滑块不显示
    table.showsVerticalScrollIndicator = NO;
    table.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:table];
}

#define mark --UITableViewDelegate
//设置列表中每个元素的行高,在非动态行高的情况下设置固定值。动态行高的情况需要计算。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 64;
}

#define mark --UITableViewDataSource
/*
//在样式为UITableViewStyleGrouped(分组样式)的时候需要实现下面的设置分组数量的代理方法,有几个分组设置返回值为几。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
 */

//设置列表中元素的个数  一般设置为数据源中元素的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return dataSource.count;
}

//创建tableView中的每一个cell,这里使用复用机制。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.textLabel.text = dataSource[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"点击了第%d行", indexPath.row);
}
@end

UITableView介绍

UITableView的代理协议有两个:UITableViewDelegate, UITableViewDataSource。

UITableView的样式有两种:UITableViewStylePlain(无分组样式)、UITableViewStyleGrouped( 分组样式)

UITableView是继承自UIScrollView的,所以属性在这里就不多说了,重点说一下代理方法。

UITableView的代理方法:

UITableViewDataSource协议的常用代理方法:

1、设置分组数量:这个方法在设置样式为UITableViewStylePlain时不需要实现,默认为1个分组

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

2、设置每组中列表数量:

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

3、设置列表中每个元素(cell):

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

4、设置分组的头标题:

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

5、设置分组的尾标题:

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

若头尾标题不同意需要根据section单独去设置。

UITableViewDelegate协议的常用代理方法:

1、设置行高:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

2、设置分组的头标题视图高度:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

3、设置分组的尾标题视图高度:

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;

4、自定义分组的头视图:

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;

5、自定义分组的尾视图:

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;

6、点击cell:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

UITableView的使用通常需要进行复杂的设置和复杂的计算,比如动态的cell高度,比如动态修改,比如动态插入,比如删除和编辑。但是无论多复杂,都是在这些代理方法中进行设置,所以万变不离其宗。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章