Table View简单描述:

在iPhone和其他iOS的很多程序中都会看到Table View的出现,除了一般的表格资料展示之外,设置的属性资料往往也用到Table View,Table View主要分为以下两种:

  • Plain:这是普通的列表风格
  • Grouped :这是分块风格。
 
对于UITableView,我們有一些特殊的概念和术语,比如说我们成Table View的一行为Cell,而许多的Cell可以组成Section,每个Section上下又分別有Header和Footer,许多个的Section则组成了整个Table ,当然Table也有Header和Footer,下面看两种图就能明白这几个拗口名词了:
 
 
现在理论知识了解的差不多了。今天先做一个Plain样式的例子,这样加强对Table view的熟练使用。
 

1、新建项目

新建一个Single View Application,命名为TableViewDemo,开发环境是:Xcode 4.3,iPhone 5.1模拟器。
2、Table View放上控件
打开ViewController.xib文件,往ViewController.xib界面上拖拽一个Table View控件到现有的View上,
对齐。
3、连接新添加的TableView和ViewController。
选中新添的TableView控件,打开连接检查器(Connection Inspector), 找到delegate和datasource并点中圆圈拉线连接到左边File's Owner图标上,为什么要把这两个连接File's Owner上呢?这是因为iOS使用的MVC设计模式,View和ViewController之间的对应关系,需要一个桥梁来进行连接的(即,对于一个视图,他如何知道自己的界面的操作应该由谁来响应),这个桥梁就是File's Owner。
4、打开ViewController.h,添加协议和Property (类似与java里的实现接口)
  1. #import <UIKit/UIKit.h>
  2. @interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
  3. @property (strong, nonatomic) NSArray *list;
  4. @end

5、打开.m文件,添加:

  1. @synthesize list = _list;


这是发现有两个警告,提示未完成的实现,这提示的是UITableViewDelegate, UITableViewDataSource这个两个头文件里的协议的方法未实现。待会我们去实现它。

6、建立数据
  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. // Do any additional setup after loading the view, typically from a nib.
  5. NSArray *array = [[NSArray alloc] initWithObjects:@"美国", @"菲律宾",
  6. @"黄岩岛", @"中国", @"泰国", @"越南", @"老挝",
  7. @"日本" , nil];
  8. self.list = array;
  9. }
  10. - (void)viewDidUnload
  11. {
  12. [super viewDidUnload];
  13. // Release any retained subviews of the main view.
  14. self.list = nil;
  15. }

7、生成row

关键的步骤来了,实现tableview添加数据源,返回TableView的行数,返回各行cell实例。
  1. - (UITableViewCell *)tableView:(UITableView *)tableView
  2. cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  3. static NSString *TableSampleIdentifier = @"TableSampleIdentifier";
  4. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
  5. TableSampleIdentifier];
  6. if (cell == nil) {
  7. cell = [[UITableViewCell alloc]
  8. initWithStyle:UITableViewCellStyleDefault
  9. reuseIdentifier:TableSampleIdentifier];
  10. }
  11. NSUInteger row = [indexPath row];
  12. cell.textLabel.text = [self.list objectAtIndex:row];
  13. return cell;
  14. }
上面的第二个方法中,
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: TableSampleIdentifier];
这个语句根据标识符TableSampleIdentifier寻找当前可以重用的UITableViewCell。当某行滑出当前可见区域后,我们重用它所对应的UITableViewCell对象,那么就可以节省内存和资源。
这里UITableViewCellStyleDefault是表示UITableViewCell风格的常数,除此之外,还有其他风格,后面将会用到。
注意参数(NSIndexPath *)indexPath,它将行号row和部分号section合并了,通过[indexPath row];获取行号。之后给cell设置其文本:
cell.textLabel.text = [self.list objectAtIndex: row];
 

8、现在一个简单的TableView就弄好看,运行下看效果

、、
 
9、添加图片。
在项目上add files到项目,提交两张小图片,然后在cell返回之前添加如下代码
  1. NSUInteger row = [indexPath row];
  2. cell.textLabel.text = [self.list objectAtIndex:row];
  3. UIImage *image = [UIImage imageNamed:@"qq"];
  4. cell.imageView.image = image;
  5. UIImage *highLighedImage = [UIImage imageNamed:@"youdao"];
  6. cell.imageView.highlightedImage = highLighedImage;
  7. return cell;

效果如下:

 
 
10、设置行的风格
表示UITableViewCell风格的常量有:

UITableViewCellStyleDefault
UITableViewCellStyleSubtle
UITableViewCellStyleValue1
UITableViewCellStyleValue2

可以自己修改看看效果。可以添加一个detail

cell.detailTextLabel.text =@"打打打打";

return cell;

 
 
11、选择table里的某一行
 
在.m文件@end之前编写  -(void)table  这时会自动提示可以实现的方法,
 
我们选择这个方法

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

选中是做个提示,提示选中了那个信息,代码实现如下:

  1. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  2. NSString *rowString = [self.list objectAtIndex:[indexPath row]];
  3. UIAlertView * alter = [[UIAlertView alloc] initWithTitle:@"选中的行信息" message:rowString delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
  4. [alter show];
  5. }

效果:

 
以上是Plain风格的TableView
 

iOS学习之Table View的简单使用的更多相关文章

  1. ***iOS学习之Table View的简单使用和DEMO示例(共Plain普通+Grouped分组两种)

    Table View简单描述: 在iPhone和其他iOS的很多程序中都会看到Table View的出现,除了一般的表格资料展示之外,设置的属性资料往往也用到Table View,Table View ...

  2. IOS学习笔记3—Objective C—简单的内存管理

    今天简述一下简单的内存管理,在IOS5.0以后Apple增加了ARC机制(Automatic Reference Counting),给开发人员带来了不少的方便,但是为了能更好的理解IOS内存管理机制 ...

  3. iOS学习之UITableView中Cell的操作

    接着iOS学习之Table View的简单使用 这篇,这里主要讲UITableView 中的Cell的操作,包括标记.移动.删除.插入. 为了简单快捷,直接从原来那篇的代码开始,代码下载地址:http ...

  4. Table View Programming Guide for iOS---(一)---About Table Views in iOS Apps

    About Table Views in iOS Apps Table views are versatile user interface objects frequently found in i ...

  5. 在iOS中怎样创建可展开的Table View?(上)

    原文地址 本文作者:gabriel theodoropoulos 原文:How To Create an Expandable Table View in iOS 原文链接 几乎所有的app都有一个共 ...

  6. iOS 利用长按手势移动 Table View Cells

    本文译自:Cookbook: Moving Table View Cells with a Long Press Gesture 目录: 你需要什么? 如何做? 如何将其利用至UICollection ...

  7. 在iOS中怎样创建可展开的Table View?(下)

    接上篇:在iOS中怎样创建可展开的Table View?(上) 展开和合拢 我猜这部分可能是你最期望的了,因为本次教程的目标将会在在部分实现.第一次我们设法让顶层的cell,在它们点击的时候展开或者合 ...

  8. iOS学习笔记(5)——显示简单的TableView

    1. 创建工程 创建一个新的Xcode工程命名为SimpleTableTest. 删除main.storyboard文件和info.plist中有关storyboard的相关属性. 按command+ ...

  9. iOS学习之第二个View使用UITabBarViewController

    前面有一篇博文iOS学习之Tab Bar的使用和视图切换 这是在AppDelegate里使用Tabbar,这样的程序打开就是TabbarView了,有时候我们需要给程序做一些帮助页面,或者登录页面,之 ...

随机推荐

  1. js工具类大全

    /********** 日期处理函数 *********/<script type="text/javascript" src="${springMacroRequ ...

  2. 第六篇 Integration Services:初级工作流管理

    本篇文章是Integration Services系列的第六篇,详细内容请参考原文. 简介在前几篇文章中,我们关注使用增量加载方式加载数据.在本篇文章,我们将关注使用优先约束管理SSIS控制流中的工作 ...

  3. C++内存未释放的情况

    以下例子中,存储了整数123的记亿体空间不能被删除,因为地址丢失了.这些空间已无法再使用. #include <iostream> using namespace std; int mai ...

  4. python判断key是否在字典用in不用has_key

    小测试 in del.py import datetime cur = datetime.datetime.now() num = 1 a_list = {"a":1, " ...

  5. Go 性能分析

    上线一定要用压力测试,才能知道自己的承受度是多少,不然出了问题,就各种排查. http://www.tuicool.com/articles/NVRJrm 通过jmeter压力测试,发现打印请求参数消 ...

  6. Jquery判断离开页面时,通过Ajax更新数据(兼容IE,Chrome,FF浏览器)

    现在很多项目都有客户离开网页时,处理一些业务的需求.所以焦点就聚集在了如何获取页面离开事件. 以下是本人在一个项目中需要记录页面浏览时长的处理办法,测试兼容IE,Chrome,FF浏览器 代码如下: ...

  7. css 滑动按钮样式

    <div class="pub_switch_box"> <input type="checkbox" id="pub_switch ...

  8. node.js学习(1)

    新建便笺 3 node.js学习(1) 1)安装 http://nodejs.org/download/下载. 2)编写一个案例 var http=require("http"); ...

  9. SQL 基本的函数

    select * from emp;

  10. 转:Selenium之CSS Selector定位详解

    CSS selector定位 CSS(Cascading Style Sheets)是一种语言,它被用来描述 HTML 和 XML 文档的样式.  百度输入框: <input name=&quo ...