美文网首页
UITableView基础

UITableView基础

作者: Coder_Fsh_Messi | 来源:发表于2015-11-16 23:18 被阅读128次

什么是UITableView

  • 在众多移动应用中,能看到各式各样的列表数据
  • 在iOS中,要实现展示列表数据,最常用的做法就是使用UITableView
  • UITableView继承自UIScrollView,因此支持垂直滚动,而且性能极佳

UITableView的两种格式

  • UITableViewStylePlain


  • UISTableViewStyleGrouped


如何创建UITableView

  • 来点简单粗暴的,直接在Xcode里面的storyboar里面拖出来,也可以根据需求拉个UITableViewController


  • 通过代码
UITableView *myTableView = [UITableView alloc] init];

如何展示数据

  • UITableView需要一个数据源(dataSource)方法来显示数据
  • UITableView会向dataSource查询一共有几组几行以及每一行显示什么数据
  • 没有设置数据源的UITableView都只是空壳子
  • 凡是遵守UITableViewDataSource的OC对象,都可以是UITableView的数据源

tableView展示数据的过程

// 多少组数据
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

// 每一组有多少行数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

// 每一行显示什么内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

  • tableView的常用设置
// 设置每一行cell的高度
self.tableView.rowHeight = 100;
// 设置每一组头部的高度
self.tableView.sectionHeaderHeight = 50;
// 设置每一组尾部的高度
self.tableView.sectionFooterHeight = 50;
// 设置分割线颜色
self.tableView.separatorColor = [UIColor redColor];
// 设置分割线样式
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
// 设置表头控件
self.tableView.tableHeaderView = [[UISwitch alloc] init];
// 设置表尾控件
self.tableView.tableFooterView = [UIButton buttonWithType:UIButtonTypeContactAdd];
// 设置右边索引文字的颜色
self.tableView.sectionIndexColor = [UIColor redColor];
// 设置右边索引文字的背景色
self.tableView.sectionIndexBackgroundColor = [UIColor blackColor];

cell的简介

  • UITableView的每一行都是一个UITableViewCell
  • UITableViewCell内部有个默认的子视图:contentView,contentView是UITableViewCell所显示内容的父视图,可以显示一些辅助指示视图,通过UITableViewCell的accessoryType来显示:
typedef NS_ENUM(NSInteger, UITableViewCellAccessoryType) {
    UITableViewCellAccessoryNone,                                                      // don't show any accessory view
    UITableViewCellAccessoryDisclosureIndicator,                                       // regular chevron. doesn't track
    UITableViewCellAccessoryDetailDisclosureButton __TVOS_PROHIBITED,                 // info button w/ chevron. tracks
    UITableViewCellAccessoryCheckmark,                                                 // checkmark. doesn't track
    UITableViewCellAccessoryDetailButton NS_ENUM_AVAILABLE_IOS(7_0)  __TVOS_PROHIBITED // info button. tracks
};
  • contentView下默认有3个子视图
  • 其中2个是UILabel(通过UITableViewCell的textLabel属性和detailLabel属性访问)
  • 第3个是UIImageView(UITableViewcell.imageView);
  • UITableViewCell还有一个UITableViewCellStyle属性,用于决定使用ContentView的哪些子视图,以及子视图在ContentView中的位置
  • 如下:


cell的重用原理

  • iOS设备的内存很有限,如果用UITableView展示成千上万条数据就得创建成千上万个UITableView对象的话,那将会耗尽iOS设备的内存.要解决这个问题,需要重用UITableView对象

  • 重用原理:


  • 当滚动列表时,部分UITableViewCell会移出窗口,UITableView会将窗口外的UITableViewCell放到一个对象池里,等待重用.当UITableView要求dataSource返回UITableViewCell时,dataSource会查看这个对象池,如果池中有未使用的UITableViewCell,dataSource会用新的数据配置这个UITableViewCell,然后返回给UITableView,重新显示到窗口中,从而避免重复创建对象.

  • 有一个灰常严重的问题:

  • 有时候我们自定义一个UITableViewCell,每一行用的不一定是同一种的UITableViewCell,所以一个UITableView可以拥有不同种类的UITableViewCell,对象池中也会有很多不同类型的UITableViewCell,那么UITableView在重用UITableViewCell时可能会得到错误类型的UITableViewCell

  • 有一个很NB的解决方法(其实也不是很牛逼.入门了的人都会知道><)

  • UITableViewCell有一个NSString *reuseIdentifier属性,可以在初始化UITableViewCell的时候传入一个特定的字符串标示来设置reuseIdentifier(一般用UITableViewCell的类名).当UITableView要求dataSource返回UITableViewCell时,先通过一个字符串标识到对象池中查找对应类型的UITableViewCell,如果有,就重用.如果没有,就传入这个字符串标识来初始化一个UITableViewCell对象

  • 感觉用天朝的语言来描述好绕..直接写个代码好乏

  • 传统写法

/**
 *  每当有一个cell要进入视野范围内,就会调用一次
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // cell的标识
    static NSString *ID = @"wine";
    
    // 先去缓存池中查找可循环利用的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    // 如果缓存池中没有可循环利用的cell
    if (!cell) {
        // 自己创建一个cell
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]
   // 设置cell的属性
     ...
    return cell;
    }
  • 新写法 (注册cell)
- (void)viewDidLoad {
    [super viewDidLoad];

    // 注册某个重用标识 对应的 Cell类型
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1.先去缓存池中查找可循环利用的cell,
   //  如果没有的话,[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID]方法会我们创建

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    // 2.设置数据
    ....

    return cell;
}

相关文章

网友评论

      本文标题:UITableView基础

      本文链接:https://www.haomeiwen.com/subject/mmnhhttx.html