UITableView常用方法、属性总结

2024-06-17 02:58

本文主要是介绍UITableView常用方法、属性总结,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

2、 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分区的底部标题

 -(NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView;
UITableView右边的索引栏的内容

 -(BOOL)tableView:(UITableView*)tableView
canEditRowAtIndexPath:(NSIndexPath *)indexPath;
某一行是否可以编辑(删除)
 -(BOOL)tableView:(UITableView*)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
某一行是否可以移动来进行重新排序

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

3、 UITableViewDelegate
 - (void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath
选中了UITableView的某一行

 - (UIView )tableView:(UITableView )tableView viewForHeaderInSection:(NSInteger)section第section分区头部显示的视图

 - (UIView )tableView:(UITableView )tableView
viewForFooterInSection:(NSInteger)section
第section分区尾部显示的视图

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

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

自定义UITableViewCell
 一般有两种方式:
1 用一个xib文件来表述UITableViewCell的内容 在这设置字符串标识,以便重用
2 通过代码往UITableViewCell的contentView中添加子视图,在 初始化方法(比如init、initWithStyle:reuseIdentifier:) 中添加子控件,在layoutSubviews方法中分配子控件的位置和大小

(2)UITableViewCell的contentView
 contentView下默认有3个子视图,其中的2个是UILabel(通 过UITableViewCell的textLabel和detailTextLabel属性访问),第3 个是UIImageView(通过UITableViewCell的imageView属性访问), UITableViewCell还有一个UITableViewCellStyle属性,用于决定使 用contentView的哪些子视图,以及这些子视图在contentView中的位置
UITableViewCellStyleDefault
UITableViewCellStyleSubtitle
UITableViewCellStyleValue1
UITableViewCellStyleValue2
(3)UITableViewCell对象的重用原理
(1)重用原理:当滚动列表时,部分UITableViewCell会移出窗口 ,UITableView会将窗口外的UITableViewCell放入一个对象池中 ,等待重用。当UITableView要求dataSource返 回UITableViewCell时,dataSource会先查看这个对象池,如果池 中有未使用的UITableViewCell,dataSource会用新的数据配置这 个UITableViewCell,然后返回给UITableView,重新显示到窗 口中,从而避免创建新对象
(2)不 同类型的UITableViewCell的重用
解决方案:UITableViewCell有个NSString *reuseIdentifier属性,可 以在初始化UITableViewCell的时候传入一个特定的字符串标识来设 置reuseIdentifier(一般用UITableViewCell的类名)。当UITableView 要求dataSource返回UITableViewCell时,先通过一个字符串标识到对象池 中查找对应类型的UITableViewCell对象,如果有,就重用,如果没有,就传 入这个字符串标识来初始化一个UITableViewCell对象
5、重要的典型代码
重用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;
}

UITableView的编辑模式
 UITableView有个editing属性,设置为YES时,可以进入编辑模式。在编 辑模式下,可以管理表格中的行,比如改变行的排列顺序、增加行、删除行 ,但不能修改行的内容
 多种方式开启编辑模式
 @property(nonatomic,getter=isEditing)BOOLediting
 -(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
取消选中某一行,让被选中行的高亮颜色消失(带动画效果)
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 dataSource 数据源
l @property(nonatomic,assign) id delegate 代理
l @property(nonatomic,getter=isEditing) BOOL editing 是否为编辑模式
l @property(nonatomic) UITableViewCellSeparatorStyle separatorStyle 设置分隔线的样式
l @property(nonatomic,retain) UIColor *separatorColor 设置分隔线的颜色
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
 是UIViewController的子类,UITableViewController默认扮演了3种角色:视 图控制器、UITableView的数据源和代理
 UITableViewController的view是个UITablView,由UITableViewController 负责设置和显示这个对象。UITableViewController对象被创建后,会将这 个UITableView对象的dataSource和delegate指向UITableViewController自己

这篇关于UITableView常用方法、属性总结的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1068304

相关文章

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

JavaSE正则表达式用法总结大全

《JavaSE正则表达式用法总结大全》正则表达式就是由一些特定的字符组成,代表的是一个规则,:本文主要介绍JavaSE正则表达式用法的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录常用的正则表达式匹配符正则表China编程达式常用的类Pattern类Matcher类PatternSynta

一文详解Git中分支本地和远程删除的方法

《一文详解Git中分支本地和远程删除的方法》在使用Git进行版本控制的过程中,我们会创建多个分支来进行不同功能的开发,这就容易涉及到如何正确地删除本地分支和远程分支,下面我们就来看看相关的实现方法吧... 目录技术背景实现步骤删除本地分支删除远程www.chinasem.cn分支同步删除信息到其他机器示例步骤

python常用的正则表达式及作用

《python常用的正则表达式及作用》正则表达式是处理字符串的强大工具,Python通过re模块提供正则表达式支持,本文给大家介绍python常用的正则表达式及作用详解,感兴趣的朋友跟随小编一起看看吧... 目录python常用正则表达式及作用基本匹配模式常用正则表达式示例常用量词边界匹配分组和捕获常用re

python删除xml中的w:ascii属性的步骤

《python删除xml中的w:ascii属性的步骤》使用xml.etree.ElementTree删除WordXML中w:ascii属性,需注册命名空间并定位rFonts元素,通过del操作删除属... 可以使用python的XML.etree.ElementTree模块通过以下步骤删除XML中的w:as

在Golang中实现定时任务的几种高效方法

《在Golang中实现定时任务的几种高效方法》本文将详细介绍在Golang中实现定时任务的几种高效方法,包括time包中的Ticker和Timer、第三方库cron的使用,以及基于channel和go... 目录背景介绍目的和范围预期读者文档结构概述术语表核心概念与联系故事引入核心概念解释核心概念之间的关系

在Linux终端中统计非二进制文件行数的实现方法

《在Linux终端中统计非二进制文件行数的实现方法》在Linux系统中,有时需要统计非二进制文件(如CSV、TXT文件)的行数,而不希望手动打开文件进行查看,例如,在处理大型日志文件、数据文件时,了解... 目录在linux终端中统计非二进制文件的行数技术背景实现步骤1. 使用wc命令2. 使用grep命令

Python中Tensorflow无法调用GPU问题的解决方法

《Python中Tensorflow无法调用GPU问题的解决方法》文章详解如何解决TensorFlow在Windows无法识别GPU的问题,需降级至2.10版本,安装匹配CUDA11.2和cuDNN... 当用以下代码查看GPU数量时,gpuspython返回的是一个空列表,说明tensorflow没有找到

XML重复查询一条Sql语句的解决方法

《XML重复查询一条Sql语句的解决方法》文章分析了XML重复查询与日志失效问题,指出因DTO缺少@Data注解导致日志无法格式化、空指针风险及参数穿透,进而引发性能灾难,解决方案为在Controll... 目录一、核心问题:从SQL重复执行到日志失效二、根因剖析:DTO断裂引发的级联故障三、解决方案:修复

C++ 检测文件大小和文件传输的方法示例详解

《C++检测文件大小和文件传输的方法示例详解》文章介绍了在C/C++中获取文件大小的三种方法,推荐使用stat()函数,并详细说明了如何设计一次性发送压缩包的结构体及传输流程,包含CRC校验和自动解... 目录检测文件的大小✅ 方法一:使用 stat() 函数(推荐)✅ 用法示例:✅ 方法二:使用 fsee