//转载请注明出处--本文永久链接:http://www.cnblogs.com/ChenYilong/p/3496969.html

技术博客http://www.cnblogs.com/ChenYilong/   新浪微博http://weibo.com/luohanchenyilong  


UITableView (PDF)

UITableView.pdf
1.6 MB 

 

l iOS中显示数据列表最常用的一个控件,支持垂直滚动


   

UITableView的两种内置样式
UITableViewStylePlain UITableViewStyleGrouped

  
数据源(dataSource)和代理(delegate)
l UITableView需要一个数据源(dataSource)来显示数据 ,UITableView会向数据源查询一共有多少行数据以及每一行显 示什么数据等。没有设置数据源的UITableView只是个空壳。凡 是遵守UITableViewDataSource协议的OC对象,都可以 是UITableView的数据源
l  通常都要为UITableView设置代理对象(delegate),以便 在UITableView触发一下事件时做出相应的处理,比如选中了某 一行。凡是遵守了UITableViewDelegate协议的OC对象,都可 以是UITableView的代理对象 
l  一般会让控制器充当UITableView的dataSource和delegate 
 
 

//转载请注明出处--本文永久链接:http://www.cnblogs.com/ChenYilong/p/3496969.html

 
 
  UITableViewDataSource

l @required
u -(NSInteger)tableView:(UITableView*)tableView
numberOfRowsInSection:(NSInteger)section;
第section分区一共有多少行
u -(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
创建第section分区第row行的UITableViewCell对象(indexPath包含 了section和row)
l @optional
u -(NSInteger)numberOfSectionsInTableView:(UITableView
*)tableView; 一共有多少个分区
u -(NSString*)tableView:(UITableView*)tableView
titleForHeaderInSection:(NSInteger)section; 第section分区的头部标题

  UITableViewDataSource
u -(NSString*)tableView:(UITableView*)tableView titleForFooterInSection:(NSInteger)section;
第section分区的底部标题
u -(BOOL)tableView:(UITableView*)tableView
canEditRowAtIndexPath:(NSIndexPath *)indexPath;
某一行是否可以编辑(删除)
u -(BOOL)tableView:(UITableView*)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
某一行是否可以移动来进行重新排序
u -(NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView;
UITableView右边的索引栏的内容

  UITableViewDelegate
l - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
选中了UITableView的某一行
l - (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath 某一行的高度
l - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
第section分区头部的高度
l - (CGFloat)tableView:(UITableView *)tableView
heightForFooterInSection:(NSInteger)section 第section分区尾部的高度

  UITableViewDelegate
l - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
第section分区头部显示的视图
l - (UIView *)tableView:(UITableView *)tableView
viewForFooterInSection:(NSInteger)section
第section分区尾部显示的视图
l - (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
设置每一行的等级缩进(数字越小,等级越高)

  UITableViewCell
l  UITableView的每一行都是一个UITableViewCell,通过dataSource 的tableView:cellForRowAtIndexPath:方法来初始化每一行 
l  UITableViewCell是UIView的子类,内部有个默认的子视图:contentView 。contentView是UITableViewCell所显示内容的父视图,并负责显示一些 辅助指示视图。辅助指示视图的作用是显示一个表示动作的图标,可以通过设 置UITableViewCell的accessoryType来显示,默认 是UITableViewCellAccessoryNone(不显示辅助指示视图),其他值如下: 
u  UITableViewCellAccessoryDisclosureIndicator  
u  UITableViewCellAccessoryDetailDisclosureButton   
u  UITableViewCellAccessoryCheckmark 

//转载请注明出处--本文永久链接:http://www.cnblogs.com/ChenYilong/p/3496969.html

 

UITableViewCellcontentView
l contentView下默认有3个子视图,其中的2个是UILabel(通 过UITableViewCell的textLabel和detailTextLabel属性访问),第3 个是UIImageView(通过UITableViewCell的imageView属性访问)
l UITableViewCell还有一个UITableViewCellStyle属性,用于决定使 用contentView的哪些子视图,以及这些子视图在contentView中的位置
   UITableViewCellStyleDefault
   UITableViewCellStyleSubtitle
   UITableViewCellStyleValue1
   UITableViewCellStyleValue2

 


 
UITableViewCell对象的重用原理
l iOS设备的内存有限,如果用UITableView显示成千上万条数据,就需 要成千上万个UITableViewCell对象的话,那将会耗尽iOS设备的 内存。要解决该问题,需要重用UITableViewCell对象
l 重用原理:当滚动列表时,部分UITableViewCell会移出窗口 ,UITableView会将窗口外的UITableViewCell放入一个对象池中 ,等待重用。当UITableView要求dataSource返 回UITableViewCell时,dataSource会先查看这个对象池,如果池 中有未使用的UITableViewCell,dataSource会用新的数据配置这 个UITableViewCell,然后返回给UITableView,重新显示到窗 口中,从而避免创建新对象

UITableViewCell对象的重用原理
l  还有一个非常重要的问题:有时候需要自定义UITableViewCell(用一个子类 继承UITableViewCell),而且每一行用的不一定是同一 种UITableViewCell(如短信聊天布局),所以一个UITableView可能拥有不 同类型的UITableViewCell,对象池中也会有很多不同类型 的UITableViewCell,那么UITableView在重用UITableViewCell时可能 会得到错误类型的UITableViewCell 
l  解决方案:UITableViewCell有个NSString *reuseIdentifier属性,可 以在初始化UITableViewCell的时候传入一个特定的字符串标识来设 置reuseIdentifier(一般用UITableViewCell的类名)。当UITableView 要求dataSource返回UITableViewCell时,先通过一个字符串标识到对象池 中查找对应类型的UITableViewCell对象,如果有,就重用,如果没有,就传 入这个字符串标识来初始化一个UITableViewCell对象 


一个UITableView中不同类型的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;
}

//转载请注明出处--本文永久链接:http://www.cnblogs.com/ChenYilong/p/3496969.html


UITableViewCell的常用属性
l 设置背景
u backgroundView
l 设置被选中时的背景视图
u selectedBackgroundView
l selectionStyle属性可设置UITableViewCell被选中时的背景颜色: u UITableViewCellSelectionStyleNone 没有颜色
u UITableViewCellSelectionStyleBlue 蓝色(默认)
u UITableViewCellSelectionStyleGray 灰色

自定义UITableViewCell 

l 一般有两种方式:
1 用一个xib文件来表述UITableViewCell的内容 在这设置字符串标识,以便重用
2 通过代码往UITableViewCell的contentView中添加子视图,在 初始化方法(比如initinitWithStyle:reuseIdentifier:) 中添加子控件,在layoutSubviews方法中分配子控件的位置和大小
 
UITableView的编辑模式
l UITableView有个editing属性,设置为YES时,可以进入编辑模式。在编 辑模式下,可以管理表格中的行,比如改变行的排列顺序、增加行、删除行 ,但不能修改行的内容
l 多种方式开启编辑模式
u @property(nonatomic,getter=isEditing)BOOLediting
u -(void)setEditing:(BOOL)editinganimated:(BOOL)animated
 
删除UITableView的行
l 首先要开启编辑模式
l 实现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的行
l 首先要开启编辑模式
l 实现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的行
l 当某行被选中时会调用此方法(UITableViewDelegate的方法)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//取消选中某一行,让被选中行的高亮颜色消失(带动画效果) [tableView deselectRowAtIndexPath:indexPath
 animated:YES];
}

 
UITableView常用方法
l - (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style 初始化一个UITableView,并且设置表格样式
l  - (void)reloadData 重新访问数据源,刷新界面 
l  - (NSInteger)numberOfSections 分区的个数 
l  - (NSInteger)numberOfRowsInSection:(NSInteger)section 
第section分区的行数 
l - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
通过indexPath找到对应的UITableViewCell对象
l - (void)setEditing:(BOOL)editing animated:(BOOL)animated
是否要开启编辑模式
l - (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated :(BOOL)animated
取消选中某一行,让被选中行的高亮颜色消失(带动画效果)
UITableView常用方法
l - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier
通过identifier在(缓存)池中找到对应的UITableViewCell对象
l - (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
移除indexPaths范围内的所有行
l  @property(nonatomic,readonly) UITableViewStyle style 表格样式 
l  @property(nonatomic,assign) id <UITableViewDataSource> dataSource 数据源 
l  @property(nonatomic,assign) id <UITableViewDelegate> delegate 代理 
l  @property(nonatomic,getter=isEditingBOOL editing 是否为编辑模式 
l  @property(nonatomic) UITableViewCellSeparatorStyle separatorStyle 设置分隔线的样式 
l @property(nonatomic,retain) UIColor *separatorColor 设置分隔线的颜色

//转载请注明出处--本文永久链接:http://www.cnblogs.com/ChenYilong/p/3496969.html

UITableView常用方法
l @property(nonatomic,retain) UIView *tableHeaderView 表头显示的视图
l @property(nonatomic,retain) UIView *tableFooterView 表尾显示的视图
l @property(nonatomic) BOOL allowsSelection
是否允许选中行
l @property(nonatomic) BOOL allowsSelectionDuringEditing 是否允许在编辑模式下选中行
l @property(nonatomic) BOOL allowsMultipleSelection 是否允许选中多行
l @property(nonatomic) BOOL allowsMultipleSelectionDuringEditing 是否允许在编辑模式下选中多行

 UITableViewController
l 是UIViewController的子类,UITableViewController默认扮演了3种角色:视 图控制器、UITableView的数据源和代理
l UITableViewController的view是个UITablView,由UITableViewController 负责设置和显示这个对象。UITableViewController对象被创建后,会将这 个UITableView对象的dataSource和delegate指向UITableViewController自己
 

例子1-城市信息

例子2-淘宝商品列表

例子3-数据删除


例子4-编辑模式


例子5-九宫格

例子6-天猫列表

基本控件文档-UITableView---iOS-Apple苹果官方文档翻译的更多相关文章

  1. 基本控件文档-UITextField属性---iOS-Apple苹果官方文档翻译

    本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址 //转载请注明出处--本文永久链接:http://www.cnblogs.com/Ch ...

  2. 基本控件文档-UIView属性---iOS-Apple苹果官方文档翻译

    本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址 //转载请注明出处--本文永久链接:http://www.cnblogs.com/Ch ...

  3. 基本控件文档-UILabel属性---iOS-Apple苹果官方文档翻译

    本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址   //转载请注明出处--本文永久链接:http://www.cnblogs.com/ ...

  4. 基本控件文档-UISwitch属性---iOS-Apple苹果官方文档翻译

    本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址 //转载请注明出处--本文永久链接:http://www.cnblogs.com/Ch ...

  5. 基本控件文档-UISlider属性---iOS-Apple苹果官方文档翻译

    本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址  //转载请注明出处--本文永久链接:http://www.cnblogs.com/C ...

  6. 基本控件文档-UISegment属性----iOS-Apple苹果官方文档翻译

    本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址 //转载请注明出处--本文永久链接:http://www.cnblogs.com/Ch ...

  7. 基本控件文档-UIKit结构图---iOS-Apple苹果官方文档翻译

    本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址  UIKit结构图 //转载请注明出处--本文永久链接:http://www.cnbl ...

  8. 基本控件文档-UIButton属性---iOS-Apple苹果官方文档翻译

    本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址 //转载请注明出处--本文永久链接:http://www.cnblogs.com/Ch ...

  9. 基本控件文档-UILabel属性

    CHENYILONG Blog 基本控件文档-UILabel属性 Fullscreen UILabel属性技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http ...

  10. 基本控件文档-UITextField属性

    CHENYILONG Blog 基本控件文档-UITextField属性 Fullscreen   UITextField属性技术博客http://www.cnblogs.com/ChenYilong ...

随机推荐

  1. 新手必备!11个强大的 Visual Studio 调试技巧

    简介 调试是软件开发周期中很重要的一部分.它具有挑战性,同时也很让人疑惑和烦恼.总的来说,对于稍大一点的程序,调试是不可避免的.最近几年,调试工具的发展让很多调试任务变的越来越简单和省时. 这篇文章总 ...

  2. 使用ASP.NET Identity 实现WebAPI接口的Oauth身份验证

    使用ASP.NET Identity 实现WebAPI接口的Oauth身份验证   目前WEB 前后端分离的开发模式比较流行,之前做过的几个小项目也都是前后分离的模式,后端使用asp.net weba ...

  3. py27使用redis

    1.安装redis pip install redis 转载请注明博客出处:http://www.cnblogs.com/cjh-notes/

  4. 【数据库】同一字段根据不同条件更新的sql语句的写法

    语法: update test set 字段1=case when 条件1 then 值1 when 条件2 then 值2 end 示例: update PMS_ProjectInfo set Pr ...

  5. 【bzoj3110】[Zjoi2013]K大数查询 整体二分+树状数组区间修改

    题目描述 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c.如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数 ...

  6. 【Java】CSVUtils

    package com.msk.ds.logic; import java.io.*; import java.util.List; /** * Created by Administrator on ...

  7. QoS专题-第5期-QoS实现之队列调度与报文丢弃

    QoS实现之队列调度与报文丢弃 前面的专题里面介绍了通过优先级映射或者MQC对报文进行分类和标记,那么分类之后要做什么呢?那就是根据分类结果对报文进行流量控制处理.拥塞管理和拥塞避免就是两种流量控制手 ...

  8. ubuntu adduser

    ubuntu adduser 添加用户 hexan 到目录 /home/hexan 下.$ sudo adduser hexan -home /home/hexan 添加一个普通用户adduser [ ...

  9. 【刷题】BZOJ 5249 [2018多省省队联测]IIIDX

    Description [题目背景] Osu听过没?那是Konano最喜欢的一款音乐游戏,而他的梦想就是有一天自己也能做个独特酷炫的音乐游戏.现在,他在世界知名游戏公司KONMAI内工作,离他的梦想也 ...

  10. 【刷题】BZOJ 4196 [Noi2015]软件包管理器

    Description Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件源下载软件包,同时自动解决所有的依赖( ...