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. < ...
随机推荐
- Android自己定义控件系列五:自己定义绚丽水波纹效果
尊重原创!转载请注明出处:http://blog.csdn.net/cyp331203/article/details/41114551 今天我们来利用Android自己定义控件实现一个比較有趣的效果 ...
- JAVA进阶-注解
注解元数据分为4部分分别为Target,Documented,Inherited,Retention: Target>指定被注解的注解仅仅能使用在某个类型上;ElementType指定其类型:能 ...
- NET中异常处理的最佳实践
NET中异常处理的最佳实践 本文翻译自CodeProject上的一篇文章,原文地址. 目录 介绍 做最坏的打算 提前检查 不要信任外部数据 可信任的设备:摄像头.鼠标以及键盘 “写操作”同样可能失效 ...
- C#函数式编程-高阶函数
随笔分类 -函数式编程 C#函数式编程之标准高阶函数 2015-01-27 09:20 by y-z-f, 344 阅读, 收藏, 编辑 何为高阶函数 大家可能对这个名词并不熟悉,但是这个名词所表达的 ...
- 判断小数点位数不超过2位的JS代码和在删除确认框里面插JS代码
<script type="text/javascript"> function checkDecimals(){ var decallowed = 2; var re ...
- Java 之关键字 null 使用总结
1.null的使用 Java中,null是一个关键字,用来标识一个不确定的对象.因此可以将null赋给引用类型变量,但不可以将null赋给基本类型变量.比如我们在定义一个变量的时候我们通过会这样做:X ...
- printf详解
用了这么多年c了,今天想编个小程序练练手,忽然发现对于printf这个函数并不甚了解.上网查了查,下面是对printf()的详解: 函数原型: #include <stdio.h> int ...
- solr主从复制
solr主从复制 最近的开发工作涉及到两个模块“任务”和“日周报”.关系是日周报消费任务,因为用户在写日周报的时候,需要按一定的规则筛选当前用户的任务,作为日周报的一部分提交.整个项目采用类似于Orc ...
- C#中IList<T>与List<T>的区别
首先IList 泛型接口是 ICollection 泛型接口的子代,并且是所有泛型列表的基接口.它仅仅是所有泛型类型的接口,并没有太多方法可以方便实用,如果仅仅是作为集合数据的承载体,确实,IList ...
- Function.prototype.toString
语法:fn.toString(indentation) 改方法返回当前函数源代码的字符串,而且还可对此字符串进行操作,比如: function num(){ }; var str = num.toSt ...