(转载)UITableView的详细讲解

NSIndexPath类型是用来获取用户选择的indexPath,在别的函数里面,若需要知道用户选择了哪个cell,用上它可以省事很多。不必再去建全局变量section和row。

NSIndexPath *tableSelection = [self.tableView indexPathForSelectedRow];

1.    UITableView的初始化

UITableView tableview= [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)];

[tableview setDelegate:self];

[tableview setDataSource:self];

[self.view addSubview: tableview];

[tableview release];

(1)在初始化UITableView的时候必须实现UITableView的是,在.h文件中要继承UITableViewDelegate和UITableViewDataSource,并实现3个UITableView数据源方法和设置它的delegate为self,这个是在不直接继承UITableViewController实现的方法。

(2) 直接在XCODE生成项目的时候继承UITableViewController的,它会帮你自动写好UITableView必须要实现的方法。

(3)UITableView继承自UIScrollView。

2.    UITableView的数据源

(1)UITableView是依赖外部资源为新表格单元填上内容的,我们称为数据源,这个数据源可以根据索引路径提供表格单元格,在UITableView中,索引路径是NSIndexPath的对象,可以选择分段或者分行,即是我们编码中的section和row。

(2)UITableView有三个必须实现的核心方法,分别如下:

-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView;

这个方法可以分段显示或者单个列表显示我们的数据。如下,左边为分段显示,右边为单个列表显示:

    

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

这个方法返回每个分段的行数,不同分段返回不同的行数可以用switch来做,如果是单个列表就直接返回单个你想要的函数即可。

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

这个方法是返回我们调用的每一个单元格。通过我们索引的路径的section和row来确定。

3.    UITableView的委托方法

使用委托是为了响应用户的交互动作,比如下拉更新数据和选择某一行单元格,在UITableView中有很大这种方法供我们选择。

(1) 委托方法讲解

//设置Section的数量

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{

return TitleData;

}

//设置每个section显示的Title

- (NSString *)tableView:(UITableView *)tableViewtitleForHeaderInSection:(NSInteger)section{

return @"Andy-清风";

}

//指定有多少个分区(Section),默认为1

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return 2;

}

//指定每个分区中有多少行,默认为1

- (NSInteger)tableView:(UITableView *)tableViewnumberOfRowsInSection:(NSInteger)section{

}

//设置每行调用的cell

-(UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";

UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:

SimpleTableIdentifier];

if (cell == nil) {

cell = [[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefault

reuseIdentifier:SimpleTableIdentifier] autorelease];

}

cell.imageView.image=image;//未选cell时的图片

cell.imageView.highlightedImage=highlightImage;//选中cell后的图片

[email protected]”Andy-清风”;

return cell;

}

//设置让UITableView行缩进

-(NSInteger)tableView:(UITableView *)tableViewindentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{

NSUInteger row = [indexPath row];

return row;

}

//设置cell每行间隔的高度

- (CGFloat)tableView:(UITableView *)tableViewheightForRowAtIndexPath:(NSIndexPath *)indexPath{

return 40;

}

//返回当前所选cell

NSIndexPath *ip = [NSIndexPath indexPathForRow:row inSection:section];

[TopicsTable selectRowAtIndexPath:ip animated:YESscrollPosition:UITableViewScrollPositionNone];

//设置UITableView的style

[tableView setSeparatorStyle:UITableViewCellSelectionStyleNone];

//设置选中Cell的响应事件

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{

[tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失

}

//设置选中的行所执行的动作

-(NSIndexPath *)tableView:(UITableView *)tableViewwillSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

NSUInteger row = [indexPath row];

return indexPath;

}

//设置划动cell是否出现del按钮,可供删除数据里进行处理

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath {

}

//设置删除时编辑状态

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle

forRowAtIndexPath:(NSIndexPath *)indexPath

{

//右侧添加一个索引表

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{

}

(2)  其他

//选中cell时的颜色,在官方文档有如下可以选择

typedef enum {

UITableViewCellSelectionStyleNone,

UITableViewCellSelectionStyleBlue,

UITableViewCellSelectionStyleGray

} UITableViewCellSelectionStyle

//cell右边按钮格式

typedef enum {

UITableViewCellAccessoryNone,                   //don‘t show any accessory view

UITableViewCellAccessoryDisclosureIndicator,    //regular chevron. doesn‘t track

UITableViewCellAccessoryDetailDisclosureButton, //blue button w/ chevron. tracks

UITableViewCellAccessoryCheckmark               //checkmark. doesn‘t track

} UITableViewCellAccessoryType

//是否加换行线

typedef enum {

UITableViewCellSeparatorStyleNone,

UITableViewCellSeparatorStyleSingleLine

} UITableViewCellSeparatorStyle

//改变换行线颜色

tableView.separatorColor= [UIColor blueColor];

4.    UITableViewCell

表中的每一行都代表一个UITableViewCell。可以使用图像、文本还有辅助的图标等来自定义你自己的UITableViewCell。你可以自定义你自己的cell如下模型或者像appstore那样的。

UITableViewCell为每个Cell提供了三个可以选择的属性,如下:

l textLabel:填写文本

l detailTextLable:稍微详细的副标题

l imageView:用来显示你cell的图片,可以通过UIImage来加载。

最后给出一个官方的demo给大家学习下,多实践,不懂的就问下,下节课讲些UITableView应用中实际会出现的问题,比如自定义啊,重用单元格,单元格的数据排序等问题。欢迎大家拍砖。

附上代码:http://www.2cto.com/uploadfile/2011/1130/20111130025401502.zip

时间: 2024-08-09 19:52:41

(转载)UITableView的详细讲解的相关文章

[iOS]数据库第三方框架FMDB详细讲解

[iOS]数据库第三方框架FMDB详细讲解 初识FMDB iOS中原生的SQLite API在进行数据存储的时候,需要使用C语言中的函数,操作比较麻烦.于是,就出现了一系列将SQLite API进行封装的库,例如FMDB.PlausibleDatabase.sqlitepersistentobjects等. FMDB是一款简洁.易用的封装库.因此,在这里推荐使用第三方框架FMDB,它是对libsqlite3框架的封装,用起来的步骤与SQLite使用类似,并且它对于多线程的并发操作进行了处理,所以

Android自定义相机超详细讲解

Android自定义相机超详细讲解 转载请标明出处: http://blog.csdn.net/vinicolor/article/details/49642861: 由于网上关于Android自定义相机的文章写得不是太详细,Google官方的文档又说得不太容易理解,所以今天我来详细讲解一下Android自定义相机. 这篇文章主要写给一些刚刚接触Android的那些看官方API困难以及不太了解Android机制的同学们,所以熟练开发者可以绕道了. 最近在使用Camera类的时候发现居然被弃用了,

PE格式详细讲解3 - 系统篇03

作者:小甲鱼 接着我们来谈谈 IMAGE_OPTIONAL_HEADER 结构,正如名字的意思,这是一个可选映像头,是一个可选的结构. 但是呢,实际上上节课我们讲解的 IMAGE_FILE_HEADER 结构远远不足以来定义 PE 文件的属性. 因此,这些属性在 IMAGE_OPTIONAL_HEADER 结构中进行定义. 因此这两个结构联合起来,才是一个完整的 “PE文件结构” . typedef struct _IMAGE_OPTIONAL_HEADER { // // Standard f

Android中万能的适配器的详细讲解

Android中万能的适配器的详细讲解 在Android开发中,适配器的用处是非常大的,尤其是效率优化方面.除了使用ViewHolder复用View之外,如果存在很多的ListView或者是一个ListView中存在很多的View组件,那对代码的阅读不是很好的.考虑到优化以及共通方面,我封装了ViewHolder类以及将Adapter类封装成共通的了,将对以后的开发带来很大的方便. (1).ViewHolder类的封装如下:ViewHolder类: package com.chengdong.s

jsoup抓取网页+详细讲解

jsoup抓取网页+详细讲解 Java 程序在解析 HTML 文档时,相信大家都接触过 htmlparser 这个开源项目,我曾经在 IBM DW 上发表过两篇关于 htmlparser 的文章,分别是:从 HTML 中攫取你所需的信息和 扩展 HTMLParser 对自定义标签的处理能力.但现在我已经不再使用 htmlparser 了,原因是 htmlparser 很少更新,但最重要的是有了 jsoup . jsoup 是一款 Java 的 HTML 解析器,可直接解析某个 URL 地址.HT

iOS KVC详细讲解

iOS KVC详细讲解 什么是KVC? KVC即NSKeyValueCoding,就是键-值编码的意思.一个非正式的 Protocol,是一种间接访问对象的属性使用字符串来标识属性,而不是通过调用存取方法,直接或通过实例变量访问的机制. 以上就是KVC的理论,面试时,如实回答就可以啦! 使用KVC说明 KVC间接修改对象属性时,会自动判断对象属性的类型,完成相应的转换. KVC按键值路径取值时,如果对象不包含指定的键值,那么就会自动进入对象内部,查找对象属性. KVC可以嵌套按照键值路径取值.

详细讲解Android的网络通信(HttpUrlConnection和HttpClient)

前言,Android的网络通信的方式有两种:使用Socket或者HTTP,今天这一篇我们详细讲解使用HTTP实现的网络通信,HTTP又包括两种方式编程方式: (1)HttpUrlConnection: (2)HttpClient: 好了,我们直接进行讲解,当然之前也会有一部分有关Android网络通信的其他知识,我们也应该了解. 一.获取网络状态的方法 (1)MainActivity.java中的关键代码 1 2 3 4 5 6 7 8 //网络管理类,可以判断是否能上网,以及网络类型     

IOS UITableView NSIndexPath属性讲解

IOS UITableView NSIndexPath属性讲解 查看UITableView的帮助文档我们会注意到UITableView有两个Delegate分别为:dataSource和delegate. dataSource 是UITableViewDataSource类型,主要为UITableView提 供显示用的数据(UITableViewCell),指定UITableViewCell支持的编辑操作类型(insert,delete和 reordering),并根据用户的操作进行相应的数据更

Spark SQL操作详细讲解

一. Spark SQL和SchemaRDD 关于Spark SQL的前生就不再多说了,我们只关注它的操作.但是,首先要搞明白一个问题,那就是究竟什么是SchemaRDD呢?从Spark的Scala API可以知道org.apache.spark.sql.SchemaRDD和class SchemaRDD extends RDD[Row] with SchemaRDDLike,我们可以看到类SchemaRDD继承自抽象类RDD.官方文档的定义是"An RDD of Row objects tha