iOS开发之UITableView详解

一、UITableView基本介绍

默认的UITableView有2种风格:

  1. UITableViewStylePlain(不分组)
  2. UITableViewStyleGrouped(分组)

UITableView中的数据只有行的概念,没有列的概念,UITableView的每行数据就是一个UITableViewCell。
自带的UITableViewCell的类型选择有:

代码如下:

typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
    UITableViewCellStyleDefault,    // 左侧显示textLabel(不显示detailTextLabel),imageView可选(显示在最左边)
    UITableViewCellStyleValue1,     // 左侧显示textLabel、右侧显示detailTextLabel(默认蓝色),imageView可选(显示在最左边)
    UITableViewCellStyleValue2,     // 左侧依次显示textLabel(默认蓝色)和detailTextLabel,imageView可选(显示在最左边)
    UITableViewCellStyleSubtitle    // 左上方显示textLabel,左下方显示detailTextLabel(默认灰色),imageView可选(显示在最左边)
};

二、UITableViewDataSource数据源

数据源的作用就是告诉UITableView,我该显示什么数据

代码如下:

#pragma mark 常用数据源方法
#pragma mark 返回分组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
#pragma mark 返回每组行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
#pragma mark 返回每行的单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
#pragma mark 返回每组头标题名称
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
#pragma mark 返回每组尾部说明
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

计算分组数 -> 计算每组行数 -> 生成分组索引 -> 生成单元格
注意:cellForRowAtIndexPath只生产当前显示在界面上的单元格

三、UITableViewDelegate代理

代理的作用是告诉UITableView,我该怎么显示和响应

代码如下:

#pragma mark - 常用代理方法
#pragma mark 设置分组头部的内容高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
#pragma mark 设置每行高度(每行高度可以不一样)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
#pragma mark 设置分组尾部的内容高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
#pragma mark 点击了某一行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
#pragma mark 设置分组的头部视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
#pragma mark 设置分组的尾部视图
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;

四、UITableView刷新列表方法

代码如下:

#pragma mark 刷新整个表格
- (void)reloadData;
#pragma mark 刷新指定的行
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
#pragma mark 刷新指定的分组
- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
#pragma mark 删除时刷新指定的行数据
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
#pragma mark 添加时刷新指定的行数据
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

五、UITableViewCell的重用机制

在UITableView内部有一个缓存池,专门用来缓存UITableViewCell,因为UITableView不是一下子显示全部Cell,而是以 所见即所得 的方式,手机上看的见的Cell,才有存在的对象UITableViewCell实例。具体表现如下:

每次显示新的Cell的时候,都是先从缓存池中取出对应的UITableViewCell对象,进行 重新初始化 显示。如果缓存池中没有,才创建新的UITableViewCell对象
每当某个Cell被移出 可见区域 外后,就会被 回收 到缓存池中
所以尽管要展示的数据巨大,但内存中存在的UITableViewCell也是有限的,极大的降低了对内存的需求。

代码如下:

# pragma mark 在tableView:cellForRowAtIndexPath:方法中使用UITableView的重用机制
// 由于此方法调用十分频繁,cell的标示声明成静态变量有利于性能优化
static NSString *cellIdentifier = @"UITableViewCellIdentifierKey1";
// 首先根据标识去缓存池取
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
// 如果缓存池没有找到,则重新创建并放到缓存池中
if(!cell){
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
}

六、系统自带的UITableViewCell

我们基本上很少使用系统自带的UITableViewCell,样式太过于死板了。

七、自定义Cell

基本步骤:
自定义类XXXTableViewCell,继承UITableViewCell
重写-(id)initWithStyle:reuseIdentifier:方法,添加子控件
最好重写layoutSubView方法,设置子控件frame
然后在UITableView的代理方法tableView:cellForRowAtIndexPath:中使用重用机制创建该类XXXTableViewCell,再对cell进行初始化

八、MVC模式

(0)

相关推荐

  • 改变iOS应用中UITableView的背景颜色与背景图片的方法

    改变UITableView的header.footer背景颜色 改变UITableView的header.footer背景颜色,这是个很常见的问题.之前知道的一般做法是,通过实现tableView: viewForHeaderInSection:返回一个自定义的View,里面什么都不填,只设背景颜色.但是今天发现一个更简洁的做法: 对于iOS 6及以后的系统,实现这个新的delegate函数即可: 复制代码 代码如下: - (void)tableView:(UITableView *)table

  • ios8 UITableView设置 setSeparatorInset UIEdgeInsetsZero不起作用的解决办法(去掉15px空白间距)

    但是在ios8中,设置setSeparatorInset:UIEdgeInsetsZero 已经不起作用了.下面是解决办法: 首先在viewDidLoad方法加入以下代码: if(leftTable!.respondsToSelector("setLayoutMargins:")){ leftTable?.layoutMargins=UIEdgeInsetsZero } if(leftTable!.respondsToSelector("setSeparatorInset:&

  • iOS App开发中使用及自定义UITableViewCell的教程

    UITableView用来以表格的形式显示数据.关于UITableView,我们应该注意: (1)UITableView用来显示表格的可见部分,UITableViewCell用来显示表格的一行. (2)UITableView并不负责存储表格中的数据,而是仅仅存储足够的数据使得可以画出当前可见部分. (3)UITableView从UITableViewDelegate协议获取配置信息,从UITableViewDataSource协议获得数据信息. (4)所有的UITableView实现时实际上只有

  • iOS开发之UITableView与UISearchController实现搜索及上拉加载,下拉刷新实例代码

    废话不多说了,直接给大家贴代码了. 具体代码如下所示: #import "ViewController.h" #import "TuanGouModel.h" #import "TuanGouTableViewCell.h" #define kDeviceWidth [UIScreen mainScreen].bounds.size.width #define kDeviceHeight [UIScreen mainScreen].bounds.

  • iOS App中UITableView左滑出现删除按钮及其cell的重用

    UITableView的编辑模式 实现UITableView简单的删除功能(左滑出现删除按钮) 首先UITableView需要进入编辑模式.实现下面的方法,即使什么代码也不写也会进入编辑模式: 复制代码 代码如下: - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)index

  • 详解iOS开发中UITableview cell 顶部空白的多种设置方法

    我知道没人会主动设置这个东西,但是大家一定都遇到过这个问题,下面总结下可能是哪些情况: 1, self.automaticallyAdjustsScrollViewInsets = NO; 这个应该是最常见而且不容易被发现的原因,起因是iOS7在Conttoller中新增了automaticallyAdjustsScrollViewInsets这个属性,当设置为YES时(默认YES),如果视图里面存在唯一一个UIScrollView或其子类View,那么它会自动设置相应的内边距,这样可以让scr

  • iOS程序开发中设置UITableView的全屏分隔线的方法(不画线)

    ableView是app开发中常用到的控件,功能很强大,多用于数据的显示.下面给大家介绍设置UITableView的全屏分隔线的两种方法. 具体详情如下所示: 如图 添加如下代码 sTableView.separatorInset = UIEdgeInsetsZero; sTableView.layoutMargins = UIEdgeInsetsZero; cell.layoutMargins = UIEdgeInsetsZero; 第二种方法如下图 -(void)viewDidLayoutS

  • iOS应用中UITableView左滑自定义选项及批量删除的实现

    实现UITableView左滑自定义选项 当UITableView进入编辑模式,在进行左滑操作的cell的右边,默认会出现Delete按钮,如何自定义左滑出现的按钮呢? 只需要实现UITableView下面的这个代理方法. 复制代码 代码如下: - (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath

  • 详解iOS开发中UItableview控件的数据刷新功能的实现

    实现UItableview控件数据刷新 一.项目文件结构和plist文件 二.实现效果 1.说明:这是一个英雄展示界面,点击选中行,可以修改改行英雄的名称(完成数据刷新的操作). 运行界面: 点击选中行: 修改数据后自动刷新: 三.代码示例 数据模型部分: YYheros.h文件 复制代码 代码如下: // //  YYheros.h //  10-英雄展示(数据刷新) // //  Created by apple on 14-5-29. //  Copyright (c) 2014年 itc

  • 全面解析iOS应用中自定义UITableViewCell的方法

    有时候我们需要自己定义UITableViewCell的风格,其实就是向行中添加子视图.添加子视图的方法主要有两种:使用代码以及从.xib文件加载.当然后一种方法比较直观. 一.基本用法 我们这次要自定义一个Cell,使得它像QQ好友列表的一行一样:左边是一张图片,图片的右边是三行标签: 当然,我们不会搞得这么复杂,只是有点意思就行. 1.运行Xcode 4.2,新建一个Single View Application,名称为Custom Cell: 2.将图片资源导入到工程.为此,我找了14张50

  • 详解iOS App中UITableView的创建与内容刷新

    UITableView几乎是iOS开发中用处最广的一个控件,当然也是要记相当多东西的一个控件. 创建 首先创建一个新的项目,并添加一个MainViewController的Class文件 打开MainViewController.h文件 @interface MainViewController : UIViewController<UITableViewDataSource,UITableViewDelegate> @property (nonatomic, retain) NSArray

  • iOS应用开发中UITableView的分割线的一些设置技巧

    对于ios7,ios8及以上来说,调整UITableView的cell的分割线位置已经是相当不便,因为UITableView内部使用了margin layout. 其实只需要如下这样子就可以实现分割线的控制. 复制代码 代码如下: -(void)tableView:(UITableView )tableView willDisplayCell:(UITableViewCell )cell forRowAtIndexPath:(NSIndexPath *)indexPath {     // 下面

  • iOS开发中UITableview控件的基本使用及性能优化方法

    UITableview控件基本使用 一.一个简单的英雄展示程序 NJHero.h文件代码(字典转模型) 复制代码 代码如下: #import <Foundation/Foundation.h> @interface NJHero : NSObject /**  *  头像  */ @property (nonatomic, copy) NSString *icon; /**  *  名称  */ @property (nonatomic, copy) NSString *name; /**  

随机推荐