iOS开发-UI (八)TableView

知识点:

1.UITableView使用

2.UITableView分段功能

3.UITableViewCell重用机制

 

=======================

UITableView使用

    1.UITableView作用

    2.UITableView创建

    - (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style;

UITableViewStyle:

UITableViewStylePlain       列表模式

    UITableViewStyleGrouped       分组模式

// 实例化一个表格视图

    //UITableViewStylePlain 列表模式

    //UITableViewStyleGrouped 分组模式

    

UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];

    //设置代理

    tableView.delegate = self;

    tableView.dataSource = self;

    [self.view addSubview:tableView];

 

 

    3.UITableView关联数据(上面)

      1)tableView通过代理关联数据

   

    4.NSIndexPath

      主要用来标识当前cell的在tableView中的位置

      该对象有section和row两个属性,

      前者标识当前cell处于第几个section中

      后者代表在该section中的第几行

 

    5.UITableViewCell介绍

      1)创建方式

       - (id)initWithStyle:(UITableViewCellStyle)style 

  reuseIdentifier:(NSString *)reuseIdentifier

//当某一个视图控制器受到导航控制器管理的时候,如果在self.view之上添加的第一个子视图是UIScrollView或者UIScrollView的子类,那么这个对象的坐标会自动往下偏移64个单位

    //关闭此优化机制

    //self.automaticallyAdjustsScrollViewInsets = NO;

UITableViewCellStyle:

UITableViewCellStyleDefault

UITableViewCellStyleValue1

UITableViewCellStyleValue2

UITableViewCellStyleSubtitle

 

   UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

=======================

UITableView分段功能

   1.设置tableView的样式

     UITableViewStyleGrouped

   2.设置代理

     1)设置段数:默认返回1

     - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

=======================

UITableView常用方法

UITableViewDataSource 

UITableViewDelegate

@interface RootViewController ()<UITableViewDelegate,UITableViewDataSource>

 #pragma mark- UITableViewDelegate&UITableViewDataSource

//返回组数 (可选实现)

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

        return 2;

}

 

//返回一组里面有几行(默认为1组)

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

   

    return 20;

}

//每一行都需要返回一个UITableViewCell类型的对象

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

    //NSIndexPath 表格视图中的坐标对象

    // section->组

    // row->行

    

    

    //创建UITableViewCell类型的对象

    /*

     参数1:cell的类型

     参数2:复用标识

     */

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

    

    //设置cell的标题为

    cell.textLabel.text = @"大家好";

    //设置图片

    cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%03ld", indexPath.section * 20 + indexPath.row + 1]];

    

    return cell;

    

}

  1)设置行高

  - (CGFloat)tableView:(UITableView *)tableView 

       heightForRowAtIndexPath:(NSIndexPath *)indexPath

//设置行高

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

 

2)设置段头标题

  - (NSString *)tableView:(UITableView *)tableView 

  titleForHeaderInSection:(NSInteger)section

//返回组头标题

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [NSString stringWithFormat:@"第%ld组组头",section];
}

 

3)设置段尾标题

  - (NSString *)tableView:(UITableView *)tableView 

  titleForFooterInSection:(NSInteger)section

//返回组尾标题

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

    return @"我是组尾";

}

 

4)删除/插入一行(两个一起用)

//编辑事件的回调方法

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        //删除

        //首先删除数据源

        [self.dataArr removeObjectAtIndex:indexPath.row];

        //刷新UI

        //reloadData 重新加载一遍数据

        //[_tableView reloadData];

        //带动画刷新(删除)

        [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];

    }else{

        //插入
        //首先在数据源当中插入新数据

        [self.dataArr insertObject:@"西安" atIndex:indexPath.row];

        //刷新UI

        //[_tableView reloadData];

        //带动画刷新(插入)

        [_tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
    }
}

//返回的编辑类型

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

    /*

     UITableViewCellEditingStyleDelete //删除

     UITableViewCellEditingStyleInsert  //插入

 

     */

    //return UITableViewCellEditingStyleDelete;

    return UITableViewCellEditingStyleInsert;

}

 

 

5)定制删除上面的文字

  - (NSString *)tableView:(UITableView *)tableView  titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath

 

//tableView调用

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths 

      withRowAnimation:(UITableViewRowAnimation)animation;

- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths 

              withRowAnimation:(UITableViewRowAnimation)animation;

 

6)进入编辑和取消编辑模式

  @property(nonatomic,getter=isEditing) BOOL editing

 

7)如何让指定行可以编辑

  - (BOOL)tableView:(UITableView *)tableView 

      canEditRowAtIndexPath:(NSIndexPath *)indexPath

  

 //是否允许编辑

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{

    //第一行不允许编辑例子

    /*

    if (indexPath.row == 0) {

        return NO;

    }

     */
    return YES;
}

 

8)如何做索引

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

  //返回索引

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

    NSMutableArray *newArr = [NSMutableArray new];

    //注意:索引的数量应该跟组数相等,如果索引的数量大于组数,则剩余的索引将无效

    for (char i  = 'A'; i <= 'Z'; i++) {

        [newArr addObject:[NSString stringWithFormat:@"%c组",i]];
    }
    return newArr;

}

 

9)如何跳转到指定某一段某一行

  - (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath 

        atScrollPosition:(UITableViewScrollPosition)scrollPosition 

        animated:(BOOL)animated;

 

10)如何移动一行  

  - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)

sourceIndexPath toIndexPath: (NSIndexPath *)destinationIndexPath{

//移动某一行

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{

    //sourceIndexPath 初始行数

    //destinationIndexPath 目标行数

  //保存一份

    id obj = self.dataArr[sourceIndexPath.row];
    //删除

    [self.dataArr removeObjectAtIndex:sourceIndexPath.row];

    //插入到目标位置
    [self.dataArr insertObject:obj atIndex:destinationIndexPath.row];

    for (NSString *str in self.dataArr) {

        NSLog(@"str = %@",str);
    }
}

 

11)选中指定行

  - (void)tableView:(UITableView *)tableView 

    didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

//选中某一行

//didSelectRowAtIndexPath   正确

//didDeselectRowAtIndexPath 错误

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

    NSLog(@"选中的行数为%ld",indexPath.row);

    /*

     UITableViewScrollPositionTop 移动某一行到屏幕的顶部

     UITableViewScrollPositionMiddle 移动某一行到屏幕的中间

     UITableViewScrollPositionBottom 移动某一行到屏幕的底部

     */

    [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}

 

12)处理accessoryButton按下的事件

  - (void)tableView:(UITableView *)tableView

accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath

 

=======================

UITableViewCell复用机制

   

    1.cell重用方式

- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier; 

    2.复用的问题

第一次dequeue的时候可能还不存在该cell,所以需要判断

如果队列中没有该cell的话,则需要alloc一个

#pragma mark- UITableViewDelegate&UITableViewDataSource

//返回一组里面有几行(默认为1组)

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

    return 20;

}

//每一行都需要返回一个UITableViewCell类型的对象

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


    //在每一个UITableView当中,都会拥有一个复用队列(数组),每当需要返回一个UITableViewCell类型的对象的时候,首先去复用队列里面查找是否拥有相同类型的对象,如果有,就拿出来再次使用

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    //如果复用队列当中没有找到,就创建新对象

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

    }
    NSLog(@"修改前显示的内容为%@",cell.textLabel.text);

    //设置cell的标题为

    cell.textLabel.text = [NSString stringWithFormat:@"%ld行",indexPath.row + 1];
    NSLog(@"修改后显示的内容为%@",cell.textLabel.text);
    return cell;

}

 

posted @ 2017-02-07 21:38  CUG  阅读(504)  评论(2编辑  收藏  举报