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后的图片

cell.text=@”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

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

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

    [iOS]数据库第三方框架FMDB详细讲解 初识FMDB iOS中原生的SQLite API在进行数据存储的时候,需要使用C语言中的函数,操作比较麻烦.于是,就出现了一系列将SQLite API进行封 ...

  2. python编写shell脚本详细讲解

    python编写shell脚本详细讲解 那,python可以做shell脚本吗? 首先介绍一个函数: os.system(command) 这个函数可以调用shell运行命令行command并且返回它 ...

  3. 详细讲解WaterRefreshLoadMoreView的使用

    详细讲解WaterRefreshLoadMoreView的使用 效果图: 加载控件的源码在如下网址中:上拉加载下拉刷新控件WaterRefreshLoadMoreView 使用的源码: // // V ...

  4. 05-- C++ 类的静态成员详细讲解

     C++ 类的静态成员详细讲解    在C++中,静态成员是属于整个类的而不是某个对象,静态成员变量只存储一份供所有对象共用.所以在所有对象中都可以共享它.使用静态成员变量实现多个对象之间的数据共享不 ...

  5. python format函数/print 函数详细讲解(4)

    在python开发过程中,print函数和format函数使用场景特别多,下面分别详细讲解两个函数的用法. 一.print函数 print翻译为中文指打印,在python中能直接输出到控制台,我们可以 ...

  6. head标签详细讲解

    head标签详细讲解 head位于html网页的头部,后前的标签,并以开始以结束的一html标签. Head标签位置如图: head标签示意图 head包含标签 meta,title,link,bas ...

  7. 详细讲解nodejs中使用socket的私聊的方式

    详细讲解nodejs中使用socket的私聊的方式 在上一次我使用nodejs+express+socketio+mysql搭建聊天室,这基本上就是从socket.io的官网上的一份教程式复制学习,然 ...

  8. iOS KVC详细讲解

    iOS KVC详细讲解 什么是KVC? KVC即NSKeyValueCoding,就是键-值编码的意思.一个非正式的 Protocol,是一种间接访问对象的属性使用字符串来标识属性,而不是通过调用存取 ...

  9. Android webservice的用法详细讲解

    Android webservice的用法详细讲解 看到有很多朋友对WebService还不是很了解,在此就详细的讲讲WebService,争取说得明白吧.此文章采用的项目是我毕业设计的webserv ...

随机推荐

  1. ExtJs布局之viewport

    <!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...

  2. poj 2975 Nim 博弈论

    令ans=a1^a2^...^an,如果需要构造出异或值为0的数, 而且由于只能操作一堆石子,所以对于某堆石子ai,现在对于ans^ai,就是除了ai以外其他的石子 的异或值,如果ans^ai< ...

  3. 正则表达式(RegExp)

    正则表达式(RegExp) 如何按一定规则快速查找到需要找寻的内容,js的设计者们给我们提供了一个叫正则表达式(RegExp对象),专门用于处理类似问题. RegExp对象表示正则表达式,它是对字符串 ...

  4. python unittest基本介绍

    python内部自带了一个单元测试的模块,pyUnit也就是我们说的:unittest 1.介绍下unittest的基本使用方法: 1)import unittest 2)定义一个继承自unittes ...

  5. 【hdu3341-Lost's revenge】DP压缩+AC自动机

    题意:给定只含有A.G.C.T的n个模板串,一个文本串,文本串任意两个字母可互换位置,问最多能匹配多少个模板串.注意:匹配同一个模板串匹配了两次,ans+=2:(可重复) 题解: 原本想到一个简单dp ...

  6. 【hdu1573-X问题】拓展欧几里得-同余方程组

    http://acm.hdu.edu.cn/showproblem.php?pid=1573 求小于等于N的正整数中有多少个X满足: X mod a0 = b0 X mod a1 = b1 …… X  ...

  7. YARN学习笔记 ResourceManager部分

    CompositeService 多个service封装,service定义了状态机状态改变的合法情况. 重要的方法是(子类需要实现的):serviceStart,serviceInit,servic ...

  8. Spring学习总结(2)——Spring IOC的前世今生

    前些天,参与了公司内部小组的一次技术交流,主要是针对<IOC与AOP>,本着学而时习之的态度及积极分享的精神,我就结合一个小故事来初浅地剖析一下我眼中的“IOC前世今生”,以方便初学者能更 ...

  9. JavaWeb项目开发案例精粹-第6章报价管理系统-002辅助类及配置文件

    1. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www ...

  10. 一个国外网盘pCloud——支持离线下载

    给大家分享一个国外网盘<支持离线下载> https://my.pcloud.com/#page=register&invite=HiegZ8aBrt7