实例方法

dequeueReusableCellWithIdentifier:
初始化一个指定重用标识符的UITableCell对象

两个协议


UITableViewDataSource

tableView:numberOfRowsInSection: (required)
行数在指定分区中
tableView:cellForRowAtIndexPath: (required)
每个Cell显示的具体内容
numberOfSectionsInTableView:
分区的个数
tableView:titleForHeaderInSection:
分区的标题
tableView:canEditRowAtIndexPath:
能否编辑

UITableViewDelegate

tableView:didSelectRowAtIndexPath:
点击cell时执行的委托方法

简单的Demo


效果图




点击细节按钮将显示一个AlertView。如果想显示细节信息,亦可以用Master-Detail模板进行创建项目,里面提供好了很多现成的方法,更加方便和快捷.

初始准备

实现协议,增加成员
@interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>
{
NSArray *_redFlowers;
NSArray *_blueFlowers;
}

预定义数字

#define kSectionCount 2
#define kRedSection 0
#define kBlueSection 1

初始化成员

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_redFlowers = @[@"Gerbera", @"Peony", @"Rose", @"poppy"];
_blueFlowers = @[@"Hyacinth", @"Hydrangea", @"Sea Holly", @"Phlox", @"Iris"];
}

实现委托方法

分区及分区标题,分区中的行数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return kSectionCount;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (section) {
case kRedSection:
return [_redFlowers count];
case kBlueSection:
return [_blueFlowers count];
default:
return 0;
}
} - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
switch (section) {
case kRedSection:
return @"Red";
case kBlueSection:
return @"Blue";
default:
return @"Unknown";
}
}

显示的Cell信息

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"flowerCell"]; switch (indexPath.section) {
case kRedSection:
cell.textLabel.text = _redFlowers[indexPath.row];
break;
case kBlueSection:
cell.textLabel.text = _blueFlowers[indexPath.row];
break;
default:
cell.textLabel.text = @"Unknown";
} UIImage *flowerImage;
flowerImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@%@", cell.textLabel.text, @".png"]];
cell.imageView.image = flowerImage; return cell;
}

这里重用标识符可以是自定义的Cell,也可以是在xib中设置好的系统带的样式,然后在属性标识板中给它设置标识符。

图片是自己个人加入到项目中的。

然后实现选择时弹出AlertView的方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIAlertView *showSelection;
NSString *messageString; switch (indexPath.section) {
case kRedSection:
messageString = [NSString stringWithFormat:@"You chose the red flower - %@", _redFlowers[indexPath.row]];
break;
case kBlueSection:
messageString = [NSString stringWithFormat:@"You chose the blue flower - %@", _blueFlowers[indexPath.row]];
break;
default:
messageString = @"Unknown";
break;
} showSelection = [[UIAlertView alloc] initWithTitle:@"Flower Selected"
message:messageString
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[showSelection show];
}

UITableView的常用方法与示例的更多相关文章

  1. C#/WPF/WinForm/.NET程序代码实现软件程序开机自动启动的两种常用方法的示例与源码下载带详细注释-源码代码-注册表方式-启动目录快捷方式

    C#/WPF/WinForm/.NET程序代码实现软件程序开机自动启动的两种常用方法的示例与源码下载带详细注释-源码代码-注册表方式-启动目录快捷方式 C#实现自动启动的方法-两种方法 源码下载地址: ...

  2. Spring JDBC常用方法详细示例

    Spring JDBC使用简单,代码简洁明了,非常适合快速开发的小型项目.下面对开发中常用的增删改查等方法逐一示例说明使用方法 1 环境准备 启动MySQL, 创建一个名为test的数据库 创建Mav ...

  3. Date和Calendar时间操作常用方法及示例

    package test; import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date; /** ...

  4. UITableView的常用方法

    一.UITableView的代理方法 #pragma mark 每一行的高度 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtI ...

  5. Mockito常用方法及示例

    Mockit是一个开源mock框架,官网:http://mockito.org/,源码:https://github.com/mockito/mockito 要使用Mockit,首先需要在我们工程中引 ...

  6. python random模块(获取随机数)的常用方法及示例

    random.randomrandom.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0 random.uniformrandom.uniform(a, b),用 ...

  7. pandas 常用方法使用示例

    from pandas import DataFrame import numpy as np import pandas as pd t={ , , np.nan, , np.nan, ], &qu ...

  8. php操作redis常用方法代码示例

     redis 的连接 描述:实例连接到一个Redis. 参数:host: string,port: int 返回值:BOOL 成功返回:TRUE;失败返回:FALSE $redis = new Red ...

  9. ibatis

    ibatis学习笔记(一)>>>>>>>sqlMapConfig.xml文件详解 1.sqlMapConfig.xml配置文件详解: Xml代码 1. < ...

随机推荐

  1. IOS中 init和initialize

    一.init和initialize 1.方法类型 1> init属于对象方法,-开头 2> initialize属于类方法,+开头 2.调用时刻 1> init:每个对象初始化的时候 ...

  2. RESTful API的设计原则

    好RESTful API的设计原则   说在前面,这篇文章是无意中发现的,因为感觉写的很好,所以翻译了一下.由于英文水平有限,难免有出错的地方,请看官理解一下.翻译和校正文章花了我大约2周的业余时间, ...

  3. php——SoapClient访问webservice

    原文:php--SoapClient访问webservice 通过SoapClient调用天气预报 <?phpheader ( "Content-Type: text/html; ch ...

  4. C语言生成2000w行数据

    最近一直抽空学习shell,脚本语言看多了多多少少有些蛋疼不适,所以捡起以前遇到的一个C语言的问题看看. 原先应该是在C++吧关注的一个帖子,楼主为了测试数据库性能需要如下形式的数据要求: 字符串长度 ...

  5. MySQL之自定义函数

    引言 MySQL本身提供了内置函数,这些函数的存在给我们日常的开发和数据操作带来了很大的便利,比如我前面提到过的聚合函数SUM().AVG()以及日期时间函数等等,可是我们总会出现其他的需求:我们需要 ...

  6. 智能的API、云服务和SOA测试解决方案——Parasoft SOAtest

    依赖Parasoft测试解决方案的机构,不仅有小企业,政府机构,还有世界500强集团.Parasoft公司推出的Parasoft SOAtest,提供了API.云服务和SOA最全面的测试解决方案.此次 ...

  7. HDInsight-Hadoop现实(两)传感器数据分析

    HDInsight-Hadoop现实(两)传感器数据分析 简要 现在,含传感器非常个人和商用设备收集来自物理世界的信息.例如.大多数手机都有 GPS.健身器材可以跟踪的步骤,你去数,恒温控制器可以监视 ...

  8. [代码收藏]设为首页和加入收藏的JavaScript代码(兼容多浏览器)

    其实不少非IE内核浏览器都仍不支持通过代码将网页设为主页和加入收藏的功能,因此说是兼容,其实只是一个try,catch后的提醒而已. 加入收藏: /* * author : 2010-12-27 11 ...

  9. MySQL5.7 安装过程中出现 attempting to start service 过不去

    MySQL5.7 安装过程中出现 attempting to start service 过不去. 1,机制打开服务,把MySql服务名启动(我的是MySqlAliyun) 启动失败:提示1067错误 ...

  10. OWC11生成统计图案例

    (1)饼状图:----通过修改参数生成不同的走势图, string strCategory = "优良率" + '\t' + "合格率" + '\t' + &q ...