UITableView的常用方法与示例
实例方法
初始化一个指定重用标识符的UITableCell对象
两个协议
UITableViewDataSource
行数在指定分区中
tableView:cellForRowAtIndexPath: (required)
每个Cell显示的具体内容
numberOfSectionsInTableView:
分区的个数
tableView:titleForHeaderInSection:
分区的标题
tableView:canEditRowAtIndexPath:
能否编辑
UITableViewDelegate
点击cell时执行的委托方法
简单的Demo
效果图
初始准备
@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";
}
}
- (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中设置好的系统带的样式,然后在属性标识板中给它设置标识符。
- (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的常用方法与示例的更多相关文章
- C#/WPF/WinForm/.NET程序代码实现软件程序开机自动启动的两种常用方法的示例与源码下载带详细注释-源码代码-注册表方式-启动目录快捷方式
C#/WPF/WinForm/.NET程序代码实现软件程序开机自动启动的两种常用方法的示例与源码下载带详细注释-源码代码-注册表方式-启动目录快捷方式 C#实现自动启动的方法-两种方法 源码下载地址: ...
- Spring JDBC常用方法详细示例
Spring JDBC使用简单,代码简洁明了,非常适合快速开发的小型项目.下面对开发中常用的增删改查等方法逐一示例说明使用方法 1 环境准备 启动MySQL, 创建一个名为test的数据库 创建Mav ...
- Date和Calendar时间操作常用方法及示例
package test; import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date; /** ...
- UITableView的常用方法
一.UITableView的代理方法 #pragma mark 每一行的高度 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtI ...
- Mockito常用方法及示例
Mockit是一个开源mock框架,官网:http://mockito.org/,源码:https://github.com/mockito/mockito 要使用Mockit,首先需要在我们工程中引 ...
- python random模块(获取随机数)的常用方法及示例
random.randomrandom.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0 random.uniformrandom.uniform(a, b),用 ...
- pandas 常用方法使用示例
from pandas import DataFrame import numpy as np import pandas as pd t={ , , np.nan, , np.nan, ], &qu ...
- php操作redis常用方法代码示例
redis 的连接 描述:实例连接到一个Redis. 参数:host: string,port: int 返回值:BOOL 成功返回:TRUE;失败返回:FALSE $redis = new Red ...
- ibatis
ibatis学习笔记(一)>>>>>>>sqlMapConfig.xml文件详解 1.sqlMapConfig.xml配置文件详解: Xml代码 1. < ...
随机推荐
- java设计模式之七装饰器模式(Decorator)
顾名思义,装饰模式就是给一个对象增加一些新的功能,而且是动态的,要求装饰对象和被装饰对象实现同一个接口,装饰对象持有被装饰对象的实例,关系图如下: Source类是被装饰类,Decorator类是一个 ...
- 【phpMyAdmin】更改配置文件连接到其他server
默认phpMyAdmin安装完毕后对机器的访问mysql,但有时我们需要访问其它server的mysql数据库,所以我们需要配置. 真,phpMyAdmin已经为我们做了配置的选项.可是须要我们进行一 ...
- Swift编程语言学习4.1——周期
Swift它提供了类似 C 流量控制结构语言,它包含运行多个任务的能力for和while周期.选择根据不同的编码分支机构的具体条件来运行if和switch声明,有控制流程跳转到其他代码break和co ...
- 3. SQL Server数据库状态监控 - 可用空间
原文:3. SQL Server数据库状态监控 - 可用空间 数据库用来存放数据,那么肯定需要存储空间,所以对磁盘空间的监视自然就很有必要了. 一. 磁盘可用空间 1. 操作系统命令或脚本.接口或工具 ...
- jQuery小例
jQuery小例子 使用前,请先引用jquery 1,map遍历数组 2,jQuery对象与DOM对象才做元素和互转 3,prevall与nextall 4,jquery版的星星评分控件 5,jq ...
- ComponentName意思
ComponentNameActivity Intent i=new Intent(); i.setComponent(new ComponentName(String packageName,Str ...
- 临时对象与NRV技术
<More Effective C++>中讲到,在C++中真正的临时对象是看不见的,它们不出现在你的源代码中.建立一个没有命名的非堆(non-heap)对象会产生临时对象,这种未命名的对象 ...
- JSON.stringify 方法
浅谈 JSON.stringify 方法 用过 json 的应该都知道,把一个对象通过 stringify 之后提交给后台或者存储在 Storage 里是很常用的手段.但是 IE6-8 下没有 J ...
- SignalR 2.0 系列: SignalR简介
SignalR 2.0 系列: SignalR简介 英文渣水平,大伙凑合着看吧,并不是逐字翻译的…… 这是微软官方SignalR 2.0教程Getting Started with ASP.NET S ...
- 记录OC学习的一点一滴(一)
练习代码: 代码一:(面向过程的OC) // // main.m // Class01Test01 // // Copyright (c) 2014年 levi. All rights reserve ...