iOS - UITableView 详解

人一切的痛苦,本质上都是对自己的无能的愤怒。

UITableView 作为 iOS 中最常见的控件 ,在APP中出现频率颇高,作为第一篇文章,就索性将其整理一下


目录

1. UITableView的基本概念
2. UITableView的最基本属性
3. UITableViewCell的基本属性

UITableView 的基本概念

  • UITableView 是用于展示列表信息的非常方便与常用的工具。
  • UITableView 是 UIScrollView 的子类 ,因此具有能够滑动的特性,能够上下滑动,同时,在cell是可编辑的情况下,能向左滑动(后面cell中会有讲解)。

UITableView 的基本属性

  • UITableView分为两种样式
    • UITableViewStylePlain
      iOS - UITableView 详解_第1张图片
      UITableViewStylePlain 没有分组的样式.png
  • UITableViewStyleGrouped


    iOS - UITableView 详解_第2张图片
    UITableViewStyleGrouped 有分组的样式
  • 使用时,需准守的协议

    • 按照协议去写返回多少个分区,每个分区返回多少行,每行显示什么,每行多高,每个分区分区头多高,分区尾多高之类的
  • 关于 **表头 表尾 分区头 分区尾 **

需要注意的是 分区头和分区尾是会悬停的

  • 表头 tableHeaderView 在tableView最上面,和tableView一起滚动
  • 表尾 tableFooterView 在tableView最下面,一般用来去掉多余的cell的分割线
tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
  • 分区头 分区头一旦到达tableView顶部就会悬停在最上方,直到该分区全部消失
  • 分区尾 分区尾一旦出现在tableView底部就会悬停在最下方,直到该分区全部显示
    iOS - UITableView 详解_第3张图片
    分区头分区尾演示
  • 关于tableViewCell的重用

** 需要注意的是 tableView 必须使用重用机制 , 当cell滑出屏幕的时候 cell会被系统回收,在下面cell将要显示时会调用之前回收的cell,这样就不必每次都创建销毁,节省内存同时节省时间,如果不使用重用机制,每次都创建新的cell 是非常消耗内存和性能的,因此,使用重用是非常非常重要的**

   UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
   if (cell == nil) {
       cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
   }

UITableViewCell的基本属性

  • cell 样式
    • UITableViewCellStyleDefault 默认样式 没有副标题


      UITableViewCellStyleDefault
    • UITableViewCellStyleValue1


      UITableViewCellStyleSubtitle
    • UITableViewCellStyleValue2


      UITableViewCellStyleValue2
    • UITableViewCellStyleSubtitle


      UITableViewCellStyleValue1
  • cell分割线
    • 位置 颜色
// 设置端距,这里表示separator离左边和右边均80像素
tableView.separatorInset = UIEdgeInsetsMake(0,80, 0, 80); 
tableView.separatorStyle =
UITableViewCellSeparatorStyleSingleLine;
tableView.separatorColor = [UIColor blueColor];
iOS - UITableView 详解_第4张图片
分割线设置
  • cell选中样式

cell.selectionStyle = UITableViewCellSelectionStyleNone;
/*
我一般会取消选中 也就是使用UITableViewCellSelectionStyleNone
总是感觉有那个选中状态很丑
这里就不一一截图列举选中样式了
UITableViewCellSelectionStyleNone
UITableViewCellSelectionStyleBlue
UITableViewCellSelectionStyleGray
UITableViewCellSelectionStyleDefault
*/


- cell右边指示样式

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
/*
UITableViewCellAccessoryNone 没有
UITableViewCellAccessoryDisclosureIndicator 箭头
UITableViewCellAccessoryDetailDisclosureButton 感叹号和箭头
UITableViewCellAccessoryCheckmark 对勾
UITableViewCellAccessoryDetailButton 感叹号
*/


- cell左侧滑动编辑
> 一定要记得删除数据源中的数据,否则指示表面上删除了这一行,下次读取的时候,这行数据又会出现

//左滑之后会出现的字

  • (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    return @"删除";
    }

//点击了删除 会调用这个方法

  • (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
    // 从数据源中删除
    [_data removeObjectAtIndex:indexPath.row];
    // 从列表中删除
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }

//退出了编辑模式会走这个方法

  • (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath{

}

![左滑删除](http://upload-images.jianshu.io/upload_images/2623330-26d0a02d4bdfabea.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

- 自定义cell
在cell重用那里使用自定义的cell来创建就行了,记得使用重用。
自定义的cell高度可能不是默认的44,因此,要记得修改cell的高度

  • (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    return 44;
    }
> 本文没有写到使用 xib 的情况 ,因为个人不太喜欢使用xib。虽然用xib会快一些,但是在后期维护方面并不是十分方便,我也建议大家多使用全代码的形式来写项目。
> 本文中还有很多没有提及的东西,有一些是忘记了的,如果有疑问直接留言,我会及时添加和修改,谢谢。

你可能感兴趣的:(iOS - UITableView 详解)