UITableView的基本使用

UITableView的基本使用

  • tableview滚动的时候让section的header停留效果
  • 设置tableview的样式为plain样式
  • 返回header的String即可
  • cell的backgroundColorbackgroundView
  • 两者都可以改变cell的背景
  • backgroundView的优先级大于backgroundColor
  • cell的selectedBackgroundView :设置cell的选中显示的view
  • tableview的separatorColor:设置分割线的颜色
  • tableview的separatorStyle:设置分割线的样式

tableview的循环利用

  • 循环利用的原理:
  • 将移出屏幕的cell放在缓存池中,在上下滑动过程中需要新的cell的时候将缓存池中的闲置cell拿出来使用
  • 第一种是在缓存池中没有闲置的cell的时候自己创建
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    // 被static修饰的局部变量:只会初始化一次,在整个程序运行过程中,只有一份内存
    static NSString *ID = @"cell";

    // 先根据cell的标识去缓存池中查找可循环利用的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    // 如果cell为nil(缓存池找不到对应的cell)
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }

    cell.textLabel.text = [NSString stringWithFormat:@"testdata - %zd", indexPath.row];

    return cell;
}

  • tableview的循环利用方式2

  • 预先注册cell

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        // 注册某个标识对应的cell类型
        [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
    

}

- 在返回cell的代理方法中去缓存池中取cell,如果不存在就会按照之前注册的cell创建

```objc
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    // 去缓存池中查找cell,如果不存在就会按照之前注册的cell创建
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    cell.textLabel.text = [NSString stringWithFormat:@"testdata - %zd", indexPath.row];

    return cell;
}
  • tableview的循环利用方式3

  • 在storyboard中先注册好cell,在返回cell的方法中如果不存在就会按照storyboard中注册的cell创建

  • 拖一个tableviewController,选中tableview,显示Dynamic Prototypes,这个是tableview的动态创建cell,在这个模式下注册的动态的cell可以自动创建

  • 在下面的Prototypes cells中设置动态cell的个数,因为可以设置多个cell样本

  • cell的一些设置

    • 选中cell,Style设置cell的样式,custom为自定义,自定义的才可以往上面拖控件
    • Identifier为cell的重用标识
  • 在拖出的tableviewController中tableview已经Prototypes cells有了1个,但是在viewController中拖一个tableview在view上Prototypes cells显示为0,这个时候调整Prototypes cells的值即可

  • 代码部分:

    - (UITableViewCell *)cell:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath{
        static NSString *ID = @"cell";
        // 先根据cell的标识去缓存池中查找可循环利用的cell,查找不到会根据ID去storyboard中找对应标识的cell
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        cell.textLabel.text = [NSString stringWithFormat:@"cell - %zd", indexPath.row];
    
        return cell;
    

}


###tableview常用的方法
- 插入行 : 1.增加数据源一个数据 2.tableView insert行

```objc
 [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:2 inSection:0]] withRowAnimation:UITableViewRowAnimationRight];
  • 删除行 :1. 先要删除数据源的一个数据 2.tableView delete行
    [self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:2 inSection:0]] withRowAnimation:UITableViewRowAnimationRight];

  • 更新一行的数据 : 1.先更新数据 2.tableView reload 行

     [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:3 inSection:0]] withRowAnimation:UITableViewRowAnimationMiddle];
    
    
  • 以上的删除、增加、更新一行的方法在执行的时候要保证数据源的个数和cell的个数相匹配,否则会报错

tableview的编译模式

  • 实现tableview的左滑,实现下面的代理方法即可
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {  // 点击了“删除”

    } else if (editingStyle == UITableViewCellEditingStyleInsert) { // 点击了+

    }
}

  • tableview的编译模式

  • 先设置编译模式

  • 默认都是删除按钮

      [self.tableView setEditing:YES animated:YES];
    
    
  • 实现下面的代理方法可以指定特定行的编辑模式是增加还是删除

    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
        return indexPath.row % 2 == 0? UITableViewCellEditingStyleInsert: UITableViewCellEditingStyleDelete;
    

}


##tableview的批量操作
- 先设置tableview的允许在编辑模式进行多选操作:

```objc
self.tableView.allowsMultipleSelectionDuringEditing = YES;
  • 进入编译模式(这个时候就可以多选了)
[self.tableView setEditing:YES animated:YES];
  • 获得选中的多行
self.tableView.allowsMultipleSelectionDuringEditing = YES;

tableView在有数据的行才显示分割线

  • 第一种做法是讲tableView的风格改为group
  • 第二种做法是给tableView设置一个footViewfootView可以是UIView直接alloc init之后的即可

cell的循环利用

  • storyboard方式

  • 在storyboard中自定义cell并给cell绑定标识@"cell"

  • 在返回cell的代理方法中使用@"cell"标识让tableView去取cell,如果取到的cell为空,那么会检查storyboard中是否有相同方式的cell,如果有则创建一个

  • xib方式

  • 在xib中设置cell的标识

  • 使用tableView注册一个xib的cell

  • 在返回cell的代理方法中使用@"cell"标识让tableView去取cell,如果取到的cell为空,那么会检查tableview是否曾经注册过该cell,如果有则创建一个

  • 注意的是如果一个xib的cell有一个类,例如RHCell,在注册的时候不要使用RHCell类去注册,如果使用该类去注册在创建cell的时候就不会去加载xib中的内容。可能创建的方式是[[RHCell alloc] init];

     - (void)viewDidLoad{
        [super viewDidLoad];
        [self.tableview registerNib:[UINib nibWithNibName:NSStringFromClass([RHWeiboCell class]) bundle:nil] forCellReuseIdentifier:@"cell"];
    }
    
    
  • 代码方式

  • 自定义cell,在cell中添加子控件,和以前用过的cell自定义一样

  • 注册cell,由于是代码的方式在注册的时候使用类注册即可

    - (void)viewDidLoad{
        [super viewDidLoad];
        [self.tableview registerClass:[RHCell class] forCellReuseIdentifier:@"cell"];
    

}


```objc
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

    }
    return self;
}

你可能感兴趣的:(UITableView的基本使用)