iOS开发之初识UITableView

初识
android

在iOS中,要实现表格数据展现,最经常使用的作法就是使用UITableView,UITableView继承自UIScrollView,所以支持垂直滚动,并且性能极佳。她有两种样式:UITableViewStylePlain和UITableViewStyleGrouped,前者其实就是android中的ListView或者RecyclerView,然后者样式在android中是须要经过xml建立布局的,总的说来这玩意功能比较全。缓存

使用
性能优化

一、设置数据源:要用UITableVIew显示数据,就要给她设置一个数据源,UITableView会向数据源查询一共有多少行数据以及每一行显示什么数据等,没有设置数据源的UITableView只是个空壳,凡是遵照UITableViewDataSource协议的OC对象,均可以是UITableView的数据源。布局

二、显示数据的过程:性能

/**
 *  告诉tableView一共有多少组数据
 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
/**
 *  告诉tableView第section组有多少行
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
/**
 *  告诉tableView第indexPath行显示怎样的cell
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
/**
 *  告诉tableView第section组的头部标题
 */
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
/**
 *  告诉tableView第section组的尾部标题
 */
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section


固然,遵照UITableViewDataSource协议后,必须实现的方法只有两个:优化

@required

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

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

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


三、Cell的重用原理:当滚动列表时,部分UITableViewCell会移出窗口,UITableView会将窗口外的UITableViewCell放入一个对象池中,等待重用。当UITableView要求dataSource返回UITableViewCell时,dataSource会先查看这个对象池,若是池中有未使用的UITableViewCell,dataSource会用新的数据配置这个UITableViewCell,而后返回给UITableView,从新显示到窗口中,从而避免建立新对象。
ui

四、还有一个很是重要的问题:有时候须要自定义UITableViewCell(用一个子类继承UITableViewCell),并且每一行用的不必定是同一种UITableViewCell,因此一个UITableView可能拥有不一样类型的UITableViewCell,对象池中也会有不少不一样类型的UITableViewCell,那么UITableView在重用UITableViewCell时可能会获得错误类型的UITableViewCell。spa

解决方案:UITableViewCell有个NSString *reuseIdentifier属性,能够在初始化UITableViewCell的时候传入一个特定的字符串标识来设置reuseIdentifier(通常用UITableViewCell的类名)。当UITableView要求dataSource返回UITableViewCell时,先经过一个字符串标识到对象池中查找对应类型的UITableViewCell对象,若是有,就重用,若是没有,就传入这个字符串标识来初始化一个UITableViewCell对象。code


UITableView性能优化orm

 cell的循环利用方式1

/**
 *  何时调用:每当有一个cell进入视野范围内就会调用
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 0.重用标识
    // 被static修饰的局部变量:只会初始化一次,在整个程序运行过程当中,只有一分内存
    static NSString *ID = @"cell";
    // 1.先根据cell的标识去缓存池中查找可循环利用的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    // 2.若是cell为nil(缓存池找不到对应的cell)
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    // 3.覆盖数据
    cell.textLabel.text = [NSString stringWithFormat:@"testdata - %zd", indexPath.row];
    return cell;
}

cell的循环利用方式2 (定义一个全局变量)

// 定义重用标识
NSString *ID = @"cell";

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1.去缓存池中查找cell(这时候获得的cell一定不为nil)
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    // 2.覆盖数据
    cell.textLabel.text = [NSString stringWithFormat:@"testdata - %zd", indexPath.row];
    //在数据源方法中返回cell
    return cell;
}

cell的循环利用方式3

、在storyboard中设置UITableView的Dynamic Prototypes Cell

、设置cell的重用标识

、在代码中利用重用标识获取cell

// 0.重用标识
// 被static修饰的局部变量:只会初始化一次,在整个程序运行过程当中,只有一分内存
static NSString *ID = @"cell";
// 1.先根据cell的标识去缓存池中查找可循环利用的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 2.覆盖数据
cell.textLabel.text = [NSString stringWithFormat:@"cell - %zd", indexPath.row];
return cell;


UITableView的常见设置

// 分割线颜色
self.tableView.separatorColor = [UIColor redColor];
// 隐藏分割线
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
// tableView有数据的时候才须要分割线
// 开发小技巧:快速取消分割线
 self.tableView.tableFooterView = [[UIView alloc] init];


UITableViewCell的常见设置

// 取消选中的样式(经常使用) 让当前 cell 按下无反应
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// 设置选中的背景色
UIView *selectedBackgroundView = [[UIView alloc] init];
selectedBackgroundView.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = selectedBackgroundView;
// 设置默认的背景色
cell.backgroundColor = [UIColor blueColor];
// 设置默认的背景色
UIView *backgroundView = [[UIView alloc] init];
backgroundView.backgroundColor = [UIColor greenColor];
cell.backgroundView = backgroundView;
// backgroundView的优先级 > backgroundColor
// 设置指示器
// cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.accessoryView = [[UISwitch alloc] init];
相关文章
相关标签/搜索