UITableView大总结(上)
1.UITableView继承UIScrollView.

步骤一:创建UITableView。UITableView样式为组
步骤二:设置UITableView的数据源方法。
步骤三:实现UITableView的数据源方法,此方法会自动调用。
- 返回有多少组返回一组有多少行返回每一行显示的UITableViewCell(继承UIView),initWithStyle使用这个方法调用。注意UITableView的数据源的方法普遍都是以tableView开头。
步骤四:用数组管理数据。
步骤五:每个数组中都是一个字典,key:(header,footer,cityes).
3.扩展性好:指的是需求改了,代码不需要怎么改。
4.创建模型的时候,自定义一个工厂方法(类方法)接口给外界调用。
工厂方法好处:简化对象的实例化,快速创建对象。
5.UITableViewCell
UITableViewCellStyleDefault
UITableViewCellStyleValue1
UITableViewCellStyleSubtitle
UITableViewCellStyleValue2
6.UITableViewCell设置右边辅助视图accessoryType;
UITableViewCellAccessoryDisclosureIndicator
UITableViewCellAccessoryDetailDisclosureButton
UITableViewCellAccessoryCheckmark

8.中文字前面不要加\,会把\后面的中文转义,正确描述:图书/音像
9.UIAlertView
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"产品信息展示" message:@"哈哈哈哈哈" delegate:nil cancelButtonTitle:@"ABC" otherButtonTitles:@"123",@"456", nil];
// 弹出UIAlertView
[alert show];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"产品信息展示" message:@"哈哈哈哈哈" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
// 弹出UIAlertView
[alert show];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"产品信息展示" message:p.name delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
// 弹出UIAlertView
[alert show];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"产品信息展示" message:p.name delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
// 弹出UIAlertView
[alert show];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"产品信息展示" message:p.name delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
alert.alertViewStyle = UIAlertViewStyleSecureTextInput;
// 弹出UIAlertView
10.监听UIAlertView的事件.
步骤一:设置UIAlertView的代理
步骤二:遵守UIAlertView的协议
步骤三:实现UIAlertView的按钮点击协议方法。
11.按钮方法内部实现
步骤一: 取出文本框文字
注意UIAlertView中的文本框的角标是根据UIAlertView从上到下第几个文本框决定的。最上面的文本框角标为0.
步骤二: 修改模型数据
步骤三: 刷新表格
12.UITableView中的reloadData,会重新整个表格。
13.-(void)reloadRowsAtIndexPaths:(NSArray*)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
// 指定刷新indexPaths数组里的行数。
注意indexPaths里存储的是NSIndexPath对象
14.UITableView的性能优化
UITableView默认只会加载出现在屏幕上面的cell,没当有一个cell移除屏幕,就会存储到缓存池里找。
性能优化步骤:
步骤一:定义cell的标识(不需要每次都创建cell标识,因此需要使用static,static标识只会在第一次创建,以后都不会创建了。)
步骤二:从缓存池里取cell

UITableViewDataSource
@required
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
第section分区一共有多少行
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
创建第section分区第row行的UITableViewCell对象(indexPath包含了section和row)
@optional
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
一共有多少个分区
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
第section分区的头部标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
第section分区的底部标题
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
某一行是否可以编辑(删除)
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
某一行是否可以移动来进行重新排序
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;
UITableView右边的索引栏的内容
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
选中了UITableView的某一行
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
某一行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
第section分区头部的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
第section分区尾部的高度
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
第section分区头部显示的视图
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
第section分区尾部显示的视图
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
设置每一行的等级缩进(数字越小,等级越高)
UITableViewCell

UITableView的每一行都是一个UITableViewCell,通过dataSource的
tableView:cellForRowAtIndexPath:方法来初始化每一行
UITableViewCell是UIView的子类,内部有个默认的子视图:contentView。contentView是UITableViewCell所显示内容的父视图,并负责显示一些辅助指示视图。辅助指示视图的作用是显示一个表示动作的图标,可以通过设置UITableViewCell的accessoryType来显示,默认是UITableViewCellAccessoryNone(不显示辅助指示视图),其他值如下:
UITableViewCellAccessoryDisclosureIndicator
UITableViewCellAccessoryDetailDisclosureButton
UITableViewCell的contentView
个是UILabel(通过UITableViewCell的textLabel和detailTextLabel属性访问),第3个是UIImageView(通过UITableViewCell的imageView属性访问)
UITableViewCell还有一个UITableViewCellStyle属性:用于决定使用contentView的哪些子视图,以及这些子视图在contentView中的位置
UITableViewCell对象的重用原理

iOS设备的内存有限,如果用UITableView显示成千上万条数据,就需要成千上万个UITableViewCell对象的话,那将会耗尽iOS设备的内存。要解决该问题,需要重用UITableViewCell对象
重用原理:当滚动列表时,部分UITableViewCell会移出窗口,UITableView会将窗口外的UITableViewCell放入一个对象池中,等待重用。当UITableView要求dataSource返回UITableViewCell时,dataSource会先查看这个对象池,如果池中有未使用的UITableViewCell,dataSource会用新的数据配置这个UITableViewCell,然后返回给UITableView,重新显示到窗口中,从而避免创建新对象
还有一个非常重要的问题:有时候需要自定义UITableViewCell(用一个子类继承UITableViewCell),而且每一行用的不一定是同一种UITableViewCell(如短信聊天布局),所以一个UITableView可能拥有不同类型的UITableViewCell,对象池中也会有很多不同类型的UITableViewCell,那么UITableView在重用UITableViewCell时可能会得到错误类型的UITableViewCell
解决方案:UITableViewCell有个NSString *reuseIdentifier属性,可以在初始化UITableViewCell的时候传入一个特定的字符串标识来设置reuseIdentifier(一般用UITableViewCell的类名)。当UITableView要求dataSource返回UITableViewCell时,先通过一个字符串标识到对象池中查找对应类型的UITableViewCell对象,如果有,就重用,如果没有,就传入这个字符串标识来初始化一个UITableViewCell对象
重用UITableViewCell对象
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"UITableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
}
cell.textLabel.text = [NSString stringWithFormat:@"Text %i", indexPath.row];
return cell;
}
UITableViewCell的常用属性
设置背景
backgroundView
设置被选中时的背景视图
selectedBackgroundView
selectionStyle属性可设置UITableViewCell被选中时的背景颜色:
UITableViewCellSelectionStyleNone 没有颜色
UITableViewCellSelectionStyleBlue 蓝色(默认)
UITableViewCellSelectionStyleGray 灰色
自定义UITableViewCell
一般有两种方式:
用一个xib文件来描述UITableViewCell的内容
通过代码往UITableViewCell的contentView中添加子视图,在初始化方法(比如init、initWithStyle:reuseIdentifier:)中添加子控件,在layoutSubviews方法中分配子控件的位置和大小
UITableView的编辑模式
UITableView有个editing属性,设置为YES时,可以进入编辑模式。在编辑模式下,可以管理表格中的行,比如改变行的排列顺序、增加行、删除行,但不能修改行的内容
多种方式开启编辑模式
@property(nonatomic,getter=isEditing) BOOL editing
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
删除UITableView的行
首先要开启编辑模式
实现UITableViewDataSource的如下方法:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// 如果UITableView提交的是删除指令
if (editingStyle == UITableViewCellEditingStyleDelete) {
// 删除真实数据
// [self.data removeObjectAtIndex:indexPath.row];
// 删除UITableView中的某一行(带动画效果)
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
// 如果不考虑动画效果,也可以直接[tableView reload];
}
}
移动UITableView的行
首先要开启编辑模式
实现UITableViewDataSource的如下方法(如果没有实现此方法,将无法换行)
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
int from = sourceIndexPath.row;
int to = destinationIndexPath.row;
if (from == to) return;
// 交换数据
// [self.data exchangeObjectAtIndex:from withObjectAtIndex:to];
}
选中UITableView的行
当某行被选中时会调用此方法(UITableViewDelegate的方法)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//取消选中某一行,让被选中行的高亮颜色消失(带动画效果)
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
UITableView常用方法
- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
初始化一个UITableView,并且设置表格样式
- (void)reloadData 重新访问数据源,刷新界面
- (NSInteger)numberOfSections 分区的个数
- (NSInteger)numberOfRowsInSection:(NSInteger)section
第section分区的行数
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
通过indexPath找到对应的UITableViewCell对象
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
是否要开启编辑模式
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated
取消选中某一行,让被选中行的高亮颜色消失(带动画效果)
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier
通过identifier在(缓存)池中找到对应的UITableViewCell对象
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
移除indexPaths范围内的所有行
@property(nonatomic,readonly) UITableViewStyle style 表格样式
@property(nonatomic,assign) id dataSource
数据源
@property(nonatomic,assign) id delegate 代理
@property(nonatomic,getter=isEditing) BOOL editing 是否为编辑模式
@property(nonatomic) UITableViewCellSeparatorStyle separatorStyle
设置分隔线的样式
@property(nonatomic,retain) UIColor *separatorColor
设置分隔线的颜色
@property(nonatomic,retain) UIView *tableHeaderView
表头显示的视图
@property(nonatomic,retain) UIView *tableFooterView
表尾显示的视图
@property(nonatomic) BOOL allowsSelection
是否允许选中行
@property(nonatomic) BOOL allowsSelectionDuringEditing
是否允许在编辑模式下选中行
@property(nonatomic) BOOL allowsMultipleSelection
是否允许选中多行
@property(nonatomic) BOOL allowsMultipleSelectionDuringEditing
是否允许在编辑模式下选中多行
UITableViewController
是UIViewController的子类,UITableViewController默认扮演了3种角色:视图控制器、UITableView的数据源和代理
UITableViewController的view是个UITablView,由UITableViewController负责设置和显示这个对象。UITableViewController对象被创建后,会将这个UITableView对象的dataSource和delegate指向UITableViewController自己

一、UITableView
1.数据展示的条件
1> UITableView的所有数据都是由数据源(dataSource)提供的,所以要想在UITableView展示数据,必须设置UITableView的dataSource数据源对象
2> 要想当UITableView的dataSource对象,必须遵守UITableViewDataSource协议,实现相应的数据源方法
3> 当UITableView想要展示数据的时候,就会给数据源发送消息(调用数据源方法),UITableView会根据方法返回值决定展示怎样的数据
2.数据展示的过程
1> 先调用数据源的
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
得知一共有多少组
2> 然后调用数据源的
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
得知第section组一共有多少行
3> 然后调用数据源的
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
得知第indexPath.section组 第indexPath.row 行显示怎样的cell(显示什么内容)
3.常见数据源方法
1> 一共有多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
2> 第section组一共有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
3> 第indexPath.section组 第indexPath.row行显示怎样的cell(显示什么内容)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
4> 第section组显示怎样的头部标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
5> 第section组显示怎样的尾部标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
4.tableView刷新数据的方式
1> 修改模型数据
2> 刷新表格
* reloadData 整体刷新(每一行都会刷新)
* - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
局部刷新
5.性能优化
1> 定义一个循环利用标识
static NSString *ID = @"C1";
2> 从缓存池中取出可循环利用的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
3> 如果缓存池中没有可循环利用的cell
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
4> 覆盖cell上面的数据
UITableView大总结(上)的更多相关文章
- UITableView或UIScrollVIew上的UIButton的高亮效果
UITableView或UIScrollVIew上的UIButton的高亮效果 原文地址:http://www.jianshu.com/p/b4331f06bd34 最近做项目的时候发现,UIScro ...
- 解决PHP大文件上传问题
PHP大文件上传问题 今天负责创业计划大赛的老师问我作品上报系统上传不了大文件,我当时纳闷了,做的时候没限制上传文件的大小阿,怎么会传不了呢,自己亲自体验了番,果然不 行,想了好一会儿才有点眉目 ...
- 使用commons-fileupload包进行大文件上传注意事项
项目中使用 commons-fileupload-1.2.1.jar 进行大文件上传. 测试了一把,效果很不错. 总结如下: 必须设置好上传文件的最大阀值 final long MAX_SIZE = ...
- 因用了NeatUpload大文件上传控件而导致Nonfile portion > 4194304 bytes错误的解决方法
今天遇到一个问题,就是“NeatUpload大文件上传控件而导致Nonfile portion > 4194304 bytes错误”,百度后发现了一个解决方法,跟大家分享下: NeatUploa ...
- ASP.NET 大文件上传的简单处理
在 ASP.NET 开发的过程中,文件上传往往使用自带的 FileUpload 控件,可是用过的人都知道,这个控件的局限性十分大,最大的问题就在于上传大文件时让开发者尤为的头疼,而且,上传时无法方便的 ...
- 【原创】用JAVA实现大文件上传及显示进度信息
用JAVA实现大文件上传及显示进度信息 ---解析HTTP MultiPart协议 (本文提供全部源码下载,请访问 https://github.com/grayprince/UploadBigFil ...
- BootStrap Progressbar 实现大文件上传的进度条
1.首先实现大文件上传,如果是几兆或者几十兆的文件就用基本的上传方式就可以了,但是如果是大文件上传的话最好是用分片上传的方式.我这里主要是使用在客户端进行分片读取到服务器段,然后保存,到了服务器段读取 ...
- 【解决ViewPager在大屏上滑动不流畅】 设置ViewPager滑动翻页距离
在项目中做了一个ViewPager+Fragment滑动翻页的效果,在模拟器和小米手机上测试也比较正常.但是换到4.7以上屏幕测试的时候发现老是滑动失效. 因为系统默认的滑动策略是当用户滑动超过半屏之 ...
- 使用NeatUpload控件实现ASP.NET大文件上传
使用NeatUpload控件实现ASP.NET大文件上传 一般10M以下的文件上传通过设置Web.Config,再用VS自带的FileUpload控件就可以了,但是如果要上传100M甚至1G的文件就不 ...
随机推荐
- Mysql各种引擎原理实战对比
1)存储引擎概述: (2)MySQL各大存储引擎: (3)InnoDB和MyIsam使用及其原理对比: (4)InnoDB和MyIsam引擎原理: (5)剩余引擎的使用DEMO(主要是Mrg_Myis ...
- Spring boot集成swagger2
一.Swagger2是什么? Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件. Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格 ...
- jsp&servlet初体验——用户登录功能实现
数据库准备-创建db_login数据库 t_user表 1.创建web工程 2.创建用户model user.java package com.gxy.model; public class U ...
- python--ftp服务器(pyftpdlib)
# -*- coding: utf-8 -*-# @Time : 2018/4/11 16:47# @Author : liuxiaobing# @File : test2.py# @Software ...
- solr服务器搭建
百度百科定义:Solr是一个独立的企业级搜索应用服务器,它对外提供类似于Web-service的API接口.用户可以通过http请求,向搜索引擎服务器提交一定格式的XML文件,生成索引:也可以通过Ht ...
- 背包DP入门小笔记01背包
FJUT OJ 2347 http://59.77.139.92/Problem.jsp?pid=2347 采药 TimeLimit:1000MS MemoryLimit:128MB 64-bit ...
- CSDN博客投票活动开始了
自己坚持写博客,一方面是为了将自己对知识点的理解做一个总结,另一方面也是因为自己看到了很多无私奉献分享自己知识的小伙伴们,因此自己也想像他们那样尽自己微薄之力把自己对某一知识点的理解分享给大家,或许算 ...
- 安卓7.1 新特性Shortcut
介绍 Shortcut 是谷歌在API25提出来的 类似苹果3D touch 但是没有压力感应.在安卓中完全就是长按. 来看下效果吧: 是不是很赞? 那么请随本文一起学习吧 更新 新建项目 在你项目下 ...
- J2EE进阶(十六)Hibernate 中getHibernateTemplate()方法使用
J2EE进阶(十六)Hibernate 中getHibernateTemplate()方法使用 spring 中获得由spring所配置的hibernate的操作对象,然后利用此对象进行,保存,修 ...
- 高通开发笔记---Yangtze worknote
点击打开链接 1. repo init -u git://review.sonyericsson.net/platform/manifest -b volatile-jb-mr1-yangtze 2. ...