实例方法

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. POJ 2492 并查集应用的扩展

    A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28651 Accepted: 9331 Descri ...

  2. IS2009制作Oracle 静默安装包(二) 感谢空白先生特许授权

    原文:IS2009制作Oracle 静默安装包(二) 感谢空白先生特许授权 上一篇: IS2009制作Oracle 静默安装包(一)感谢空白先生特许授权本文经原作者特许授权于海洋女神发布,转载请务必注 ...

  3. 在 VS2013的ASPNET站点开发中用 xheditor v1.1.13 作为HTML编辑器

    要用vs2013开发一个博客站点,.net   framework  4,须要一个HTML编辑器作为写文章的工具.经多方试用,排除了dotnettextbox.kceditor.认为xheditor ...

  4. sql点滴37—mysql中的错误Data too long for column '' at row 1

    原文:sql点滴37-mysql中的错误Data too long for column '' at row 1   1.MYSQL服务 我的电脑——(右键)管理——服务与应用程序——服务——MYSQ ...

  5. 编程算法 - 二叉树的深度 代码(C)

    二叉树的深度 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 输入一棵二叉树的根节点, 求该树的深度. 依次选择最深的左右子树, 然后递归加1. ...

  6. SpecFlow - Cucumber for .NET

    SpecFlow使用入门 SpecFlow是一个BDD工具,在这里对BDD不多赘述,你可以阅读一下微软2010年十二月的一篇文章,此外如果你想要更多了解SpecFlow,可以参考我的另一篇翻译(当然, ...

  7. android数据库升级的措辞

    在基类table增加upgrade操作: public abstract class DbBaseTable { private static final String TAG = "DbB ...

  8. js 实现自动换行

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. MSSQL2008数据库备份还原和数据恢复

    原文:MSSQL2008数据库备份还原和数据恢复   序言 一直想写一篇关于数据库备份与恢复的文章,但基于能力的有限对数据库认知的有限怕不足以准确的表达,最后思考很久还是决定把自己的一些理解写出来供大 ...

  10. HP quality center 9.0 邮件设置

    [转载]HP quality center 9.0 邮件设置 (2010-09-20 10:28:03) 转载▼ 标签: 转载   原文地址:HP quality center 9.0 邮件设置作者: ...