UITableView数据视图基本使用&协议&UITableViewCell单元格使用

UITableView的用途

数据视图
用来显示大量相同格式数据的视图
例如:通讯录、好友列表、朋友圈

UITableView 基本用法

  • 头文件要遵守的协议:
    UITableViewDelegate数据视图的普通代理协议
    UITableViewDataDelegate处理数据视图的数据的代理协议
  • 要知道的方法
    alloc
    initWithFrame(位置) style(风格grouped or plain)
    .delegate
    .datasource
  • 定义一个数据视图
  • .m实现文件中创建数据视图,并添加到当前视图中
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //创建数据视图
    //P1:数据视图的位置
    //P2:视图的风格
    //UITableViewStylePlain 普通平铺风格
    //UITableViewStyleGrouped 分组显示风格
    _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
}
  • 实现协议中@required要求实现的方法
    -(NSInteger)tableView:(UITableView * )tableView numberOfRowsInSection:(NSInteger)section
    获取每组元素的个数(行数)
    -(UITableViewCell*)tableView:(UITableView * )tableView cellForRowAtIndexPath:(NSIndexPath * )indexPath
    给每行填充数据,即设置单元格
//获取每组元素的个数(行数)
//必须要实现的协议函数
//程序在显示数据视图时调用
//返回值 表示每组元素的个数
//P1:数据视图对象本身
//P2:哪一组需要的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}
//设置数据视图的组数
//不是必须实现的方法,默认返回值为1组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellStr = @"cell";
    //尝试获取可以复用的单元格,单元格足够多的时候,划出屏幕的单元格可以复用为刚滑进屏幕的单元格
    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:cellStr];
    if(cell == nil){
        //创建一个单元格对象
        //P1:单元格的样式
        //P2:单元格的复用标记
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr];
    }
    
    NSString *str = [NSString stringWithFormat:@"%ld组%ld行",indexPath.section,indexPath.row];
    //将单元格的文字内容赋值
    cell.textLabel.text = str;
    return cell;
}

UITableView协议

  • 用到的方法
    heightForRowAtIndexPath 设置单元格的高度
    titleForHeaderInSection 设置每组头部的标题
    titleForFooterInSection 设置每组尾部的标题
    heightForHeaderInSection 设置头部高度
    heightForFooterInSection 设置尾部高度

  • UITableViewDataDelegate处理数据视图的数据的代理协议
    例子:分A-Z个组,每组5个数据显示

  • 在视图声明文件中先写一个可变数组假装一下源数据
    NSMutableArray *_arrayData;

  • .m实现文件中

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //创建数据视图对象
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 40, 320, 536) style:UITableViewStyleGrouped];
    //设置代理对象
    _tableView.delegate = self;
    //设置数据代理对象
    _tableView.dataSource = self;
    
    _tableView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:_tableView];
    //按照数据视图组数、行数给可变数组赋值
    _arrayData = [[NSMutableArray alloc]init];
    for(int i='A';i<'Z';i++){
        NSMutableArray * arraySmall = [[NSMutableArray alloc]init];
        for(int j=0;j<5;j++){
            NSString *str = [NSString stringWithFormat:@"%c%d",i,j];
            [arraySmall addObject:str];
        }
        [_arrayData addObject:arraySmall];
    }
}
  • 实现协议方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _arrayData.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_arrayData[section] count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *str = @"cell";
    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:str];
    if(cell ==nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }

    cell.textLabel.text = _arrayData[indexPath.section][indexPath.row];
    return cell;
}

//设置单元格的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
//设置每组头部标题
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"lalala";
}
//设置每组尾部的标题
-(NSString*)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    return @"尾部";
}
//设置头部高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}
//设置尾部高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 60;
}

高级协议

  • 用到的方法
    autoresizingMask自动调整子视图的大小
    .tableHeaderView数据视图的头部视图的设定
    .tableFooterView数据视图的尾部视图
    reloadData 更新数据视图,更新加载数据

commitEditingStyle 可以显示编辑状态,当手指在单元格上移动时
didSelectRowAtIndexPath 选中单元格时
didDeselectRowAtIndexPath 取消选中单元格时
editingStyleForRowAtIndexPath 每个单元格左侧显示效果,默认为删除

  • UITableViewCell
    tableView dequeueReusableCellWithIdentifier 获取可以复用的单元格
    alloc initWithStyle
    (UITableViewCellStyleSubtitle 需要设置子标题时用,默认是Default)
    cell.textLabel.text 设置单元格文字
    cell.detailTextLabel.text = @"子标题"
    cell.imageView.image = image 设置默认的图片

数据视图与导航视图结合

  • 在AppDelegate.m文件初始化后执行的application方法中将导航控制器设置为根视图控制器
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    UINavigationController* nav = [[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]];
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];
    
    return YES;
}
  • ViewController.h中
@interface ViewController : UIViewController

{
    //数据视图
    UITableView* _tableView;
    //数据源
    NSMutableArray* _arrayData;
    //添加导航按钮
    UIBarButtonItem* _btnEdit;
    UIBarButtonItem* _btnFinish;
    UIBarButtonItem* _btnDelete;
    //设置编辑状态
    BOOL _isEdit;
}
@end
  • ViewController.m文件中
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    //自动调整子视图的大小
    _tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight |UIViewAutoresizingFlexibleWidth;
    _tableView.delegate=self;
    _tableView.dataSource=self;
    
    //数据视图的头部视图的设定
    _tableView.tableHeaderView = nil;
    //数据视图的尾部视图
    _tableView.tableFooterView = nil;
    
    [self.view addSubview:_tableView];
    
    //初始化数据源数组
    _arrayData = [[NSMutableArray alloc]init];
    
    for(int i=1 ; i<20 ; i++){
        NSString* str = [NSString stringWithFormat:@"A %d",i];
        [_arrayData addObject:str];
    }
    
    //当数据视图的数据源发生变化时
    //更新数据视图,重新加载数据
    [_tableView reloadData];
    
    [self createBtn];
}
-(void)createBtn{
    _isEdit = NO;
    //创建功能按钮
    _btnEdit = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(pressEdit)];
    _btnFinish = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(pressFinish)];
    _btnDelete = [[UIBarButtonItem alloc]initWithTitle:@"删除" style:UIBarButtonItemStylePlain target:self action:@selector(pressDelete)];
    
    self.navigationItem.rightBarButtonItem = _btnEdit;
}
//可以显示编辑状态,当手指在单元格上移动时
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    //根据索引删除数据源对应的数据
    [_arrayData removeObjectAtIndex:indexPath.row];
    //数据源更新
    [_tableView reloadData];
    
}
//选中单元格时
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"选中单元格 %ld, %ld",indexPath.section, indexPath.row);
}
//取消选中的时候
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
     NSLog(@"取消选中单元格 %ld, %ld",indexPath.section, indexPath.row);
}
//单元格显示效果协议,默认为删除
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    //插入状态 UITableViewCellEditingStyleInsert;
    //空状态 UITableViewCellEditingStyleNone;
    //多选状态 UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert
    return UITableViewCellEditingStyleDelete;
}

-(void)pressEdit{
    _isEdit = YES;
    self.navigationItem.rightBarButtonItem = _btnFinish;
    [_tableView setEditing:YES];
    self.navigationItem.leftBarButtonItem = _btnDelete;
}
-(void)pressDelete{
    _isEdit = YES;
    self.navigationItem.rightBarButtonItem = _btnEdit;
    [_tableView setEditing:NO];
    self.navigationItem.leftBarButtonItem = nil;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _arrayData.count;
}
//默认组数返回1
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString* str = @"ID";
    //尝试获取可以复用的单元格,单元格足够多的时候,划出屏幕的单元格可以复用为刚滑进屏幕的单元格
    UITableViewCell* cell = [_tableView dequeueReusableCellWithIdentifier:str];
    if(cell == nil){
        //UITableViewCellStyleDefault 设置子标题也不会显示
        //UITableViewCellStyleSubtitle
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
    }
    cell.textLabel.text = [_arrayData objectAtIndex:indexPath.row];
    //设置子文字标题
    cell.detailTextLabel.text = @"子标题";
    
    NSString* imageStr = [NSString stringWithFormat:@"icon%d",indexPath.row%3+1];
    UIImage* image = [UIImage imageNamed:imageStr];
//    UIImageView* imageView = [[UIImageView alloc]initWithImage:image];
    //设置默认的图片
    cell.imageView.image = image;
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

你可能感兴趣的:(UITableView数据视图基本使用&协议&UITableViewCell单元格使用)