1. 创建工程

  • 创建一个新的Xcode工程命名为SimpleTableTest。
  • 删除main.storyboard文件和info.plist中有关storyboard的相关属性。
  • 按command+F键创建TableViewController视图控制器(继承自UIViewController)和xib视图文件(此时,系统将默认xib视图文件的File's Owner是TableViewController视图控制器类)。

2. 创建xib文件

  • 选定View视图,并按下option+command+3切换到Attributes inspector,调整模拟器尺寸为4inch(默认是pad的尺寸,调小即可)。
  • 按下option+command+control+3打开Object library,选中tableview并拖到视图View上。
  • 选定Table View视图,按下option+command+6打开Connections inspector,将Outlets中的delegate和dataSource与File's Owner(TableViewController视图控制器)关联。

3. 编写代码

TableViewController.h

 #import <UIKit/UIKit.h>

 @interface TableViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>//遵循tableview的委托协议和数据源协议

 @property (copy, nonatomic) NSArray *tableData;
@end

TableViewController.m

 #import "TableViewController.h"

 @interface TableViewController ()

 @end

 @implementation TableViewController

 - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.tableData = @[@"Bob", @"Fenndy", @"Neo", @"Lucky", @"Lily", @"Lucy", @"Jone", @"Kate", @"Sunny", @"Fenndy", @"Greeny", @"Brown", @"Jack", @"Andy"];
self.tableData = [self.tableData sortedArrayUsingSelector:@selector(compare:)];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.tableData count];
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *Identifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier];
}
cell.textLabel.text = self.tableData[indexPath.row];
return cell;
}
/*
#pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/ @end

AppDelegate.h

 #import <UIKit/UIKit.h>

 @interface AppDelegate : UIResponder <UIApplicationDelegate>

 @property (strong, nonatomic) UIWindow *window;

 @end

AppDelegate.m

 #import "AppDelegate.h"
#import "TableViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.rootViewController = [[TableViewController alloc] initWithNibName:@"TableViewController" bundle:nil];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
} - (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
} - (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
} - (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
} - (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
} - (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
} @end

TableViewController.h

 #import <UIKit/UIKit.h>

 @interface TableViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>//遵循tableview的委托协议和数据源协议

 @property (copy, nonatomic) NSArray *tableData;
@end

TableViewController.m

 #import "TableViewController.h"

 @interface TableViewController ()

 @end

 @implementation TableViewController

 - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.tableData = @[@"Bob", @"Fenndy", @"Neo", @"Lucky", @"Lily", @"Lucy", @"Jone", @"Kate", @"Sunny", @"Fenndy", @"Greeny", @"Brown", @"Jack", @"Andy"];
self.tableData = [self.tableData sortedArrayUsingSelector:@selector(compare:)];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.tableData count];
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *Identifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier];
}
cell.textLabel.text = self.tableData[indexPath.row];
return cell;
}
/*
#pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/ @end

4. 小结

  • 若要实现TableView必须遵循UITableViewDataSource, UITableViewDelegate协议
  • 必须实现

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 两个方法。

  • - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 方法返回一个分区的表视图单元的行数。
  • - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 方法返回一个表视图单元。
  • 表视图的每一行都是一个表视图单元的实例。
  • 对于iOS而言,如果表视图不管表视图单元显示与否,都为表中的每一行分配一个表视图单元,那么就会造成大量的开销。因此,iOS利用重用机制显示表视图单元,当表视图单元滚离屏幕时,他们将放置在一个可重用的单元队列中。如果系统运行较慢,表视图将从队列中删除这些单元,以释放空间。
  • 标识符Identifier的作用:如果,滚动到屏幕上的新行重新使用滚离屏幕的一个单元,iOS系统利用tableview的重用机制就能避免不断创建和释放那些视图的开销。但是,需要表视图给出前面使用过的制定类型的单元,因此使用一个标识符来标明重用单元。
  • TableViewController既是视图的File's Owner,也是TableView的delegate和dataSource,实现tableview的具体方法,并提供tableview所需的数据源。

iOS学习笔记(5)——显示简单的TableView的更多相关文章

  1. iOS学习笔记之Reachability简单使用

    写在前面 在学习异步图片下载的Demo过程中,由于需要实时检测网路状态,因此用到了苹果提供的Reachability库.Reachability的功能包括:检测目标网络是否可用.检测当前网络的链接方式 ...

  2. Spring MVC 学习笔记10 —— 实现简单的用户管理(4.3)用户登录显示全局异常信息

    </pre>Spring MVC 学习笔记10 -- 实现简单的用户管理(4.3)用户登录--显示全局异常信息<p></p><p></p>& ...

  3. Spring MVC 学习笔记9 —— 实现简单的用户管理(4)用户登录显示局部异常信息

    Spring MVC 学习笔记9 -- 实现简单的用户管理(4.2)用户登录--显示局部异常信息 第二部分:显示局部异常信息,而不是500错误页 1. 写一个方法,把UserException传进来. ...

  4. [置顶] iOS学习笔记47——图片异步加载之EGOImageLoading

    上次在<iOS学习笔记46——图片异步加载之SDWebImage>中介绍过一个开源的图片异步加载库,今天来介绍另外一个功能类似的EGOImageLoading,看名字知道,之前的一篇学习笔 ...

  5. iOS学习笔记之UITableViewController&UITableView

    iOS学习笔记之UITableViewController&UITableView 写在前面 上个月末到现在一直都在忙实验室的事情,与导师讨论之后,发现目前在实验室完成的工作还不足以写成毕业论 ...

  6. iOS学习笔记-自己动手写RESideMenu

    代码地址如下:http://www.demodashi.com/demo/11683.html 很多app都实现了类似RESideMenu的效果,RESideMenu是Github上面一个stars数 ...

  7. iOS学习笔记20-地图(二)MapKit框架

    一.地图开发介绍 从iOS6.0开始地图数据不再由谷歌驱动,而是改用自家地图,当然在国内它的数据是由高德地图提供的. 在iOS中进行地图开发主要有三种方式: 利用MapKit框架进行地图开发,利用这种 ...

  8. iOS学习笔记-精华整理

    iOS学习笔记总结整理 一.内存管理情况 1- autorelease,当用户的代码在持续运行时,自动释放池是不会被销毁的,这段时间内用户可以安全地使用自动释放的对象.当用户的代码运行告一段 落,开始 ...

  9. iOS学习笔记10-UIView动画

    上次学习了iOS学习笔记09-核心动画CoreAnimation,这次继续学习动画,上次使用的CoreAnimation很多人感觉使用起来很繁琐,有没有更加方便的动画效果实现呢?答案是有的,那就是UI ...

  10. iOS学习笔记总结整理

    来源:http://mobile.51cto.com/iphone-386851_all.htm 学习IOS开发这对于一个初学者来说,是一件非常挠头的事情.其实学习IOS开发无外乎平时的积累与总结.下 ...

随机推荐

  1. bootstraptable为行中的按钮添加事件

  2. 监测IIS上网站的连接数

    1.运行,输入,perfmon.msc 2.性能监视器>右侧区域>右键 添加计数器 3.在“可用计数器” 区域,选择Web Service,展开Web Service,选择Current ...

  3. HDU 5120 Intersection (圆的面积交)

    题意:给定两个圆环,求两个圆环的面积交. 析:很容易知道,圆环面积交就是,大圆与大圆面积交 - 大圆和小圆面积交 - 小圆和大圆面积交 + 小圆和小圆面积交. 代码如下: #pragma commen ...

  4. 移动lob类型索引到指定表空间

    WWWNEWSAD 为表名 USERS 为原表空间 CONTENT 为lob类型的字段 DATA_INDEX_WXZJ 指定的表空间 ALTER TABLE WWWNEWSAD MOVE TABLES ...

  5. Thrift结构分析及增加取客户端IP功能实现

    目录 目录 1 1. 前言 1 2. 示例Service 1 3. 网络部分类图 2 4. 线程模式 3 4.1. IO线程 3 4.2. 工作线程 4 4.2.1. 工作线程类图 4 4.2.2.  ...

  6. loadrunner - Run time Settings 的详细说明

    本文主要讲解一下run-time settings(如图1所示)里各设置项的具体含义(注:标红色的选项卡是比较值得关注的,可重点看一下): 图1 1.General / Run Logic 选项卡   ...

  7. 使用MATLAB一键制作mif文件

    本文档主要讲解实现一个16384(2^14)点的14位正弦波数据mif格式文件的生成,使用此文件,我们便可以在FPGA上基于直接数字合成(DDS)原理生成标准的正弦波,即实现信号发生器的功能.关于DD ...

  8. 51nod1459迷宫问题—(迪杰斯特拉)

    1459 迷宫游戏  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 你来到一个迷宫前.该迷宫由若干个房间组成,每个房间都有一个得分,第一次进入这个房间, ...

  9. 四则运算生成器(java) 蔡苑菲,陆海燕

    github地址:https://github.com/Nancy0611/Myapp.git 一.项目相关要求 使用 -n 参数控制生成题目的个数,例如 Myapp.exe -n 10 将生成10个 ...

  10. 安装Greenplum-perfmon-web监控软件遇到的问题及解决

    环境 Product Version Pivotal Greenplum (GPDB) 4.3.x Pivotal Greenplum Command Center (GPCC)   Others   ...