iOS学习笔记(5)——显示简单的TableView
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的更多相关文章
- iOS学习笔记之Reachability简单使用
写在前面 在学习异步图片下载的Demo过程中,由于需要实时检测网路状态,因此用到了苹果提供的Reachability库.Reachability的功能包括:检测目标网络是否可用.检测当前网络的链接方式 ...
- Spring MVC 学习笔记10 —— 实现简单的用户管理(4.3)用户登录显示全局异常信息
</pre>Spring MVC 学习笔记10 -- 实现简单的用户管理(4.3)用户登录--显示全局异常信息<p></p><p></p>& ...
- Spring MVC 学习笔记9 —— 实现简单的用户管理(4)用户登录显示局部异常信息
Spring MVC 学习笔记9 -- 实现简单的用户管理(4.2)用户登录--显示局部异常信息 第二部分:显示局部异常信息,而不是500错误页 1. 写一个方法,把UserException传进来. ...
- [置顶] iOS学习笔记47——图片异步加载之EGOImageLoading
上次在<iOS学习笔记46——图片异步加载之SDWebImage>中介绍过一个开源的图片异步加载库,今天来介绍另外一个功能类似的EGOImageLoading,看名字知道,之前的一篇学习笔 ...
- iOS学习笔记之UITableViewController&UITableView
iOS学习笔记之UITableViewController&UITableView 写在前面 上个月末到现在一直都在忙实验室的事情,与导师讨论之后,发现目前在实验室完成的工作还不足以写成毕业论 ...
- iOS学习笔记-自己动手写RESideMenu
代码地址如下:http://www.demodashi.com/demo/11683.html 很多app都实现了类似RESideMenu的效果,RESideMenu是Github上面一个stars数 ...
- iOS学习笔记20-地图(二)MapKit框架
一.地图开发介绍 从iOS6.0开始地图数据不再由谷歌驱动,而是改用自家地图,当然在国内它的数据是由高德地图提供的. 在iOS中进行地图开发主要有三种方式: 利用MapKit框架进行地图开发,利用这种 ...
- iOS学习笔记-精华整理
iOS学习笔记总结整理 一.内存管理情况 1- autorelease,当用户的代码在持续运行时,自动释放池是不会被销毁的,这段时间内用户可以安全地使用自动释放的对象.当用户的代码运行告一段 落,开始 ...
- iOS学习笔记10-UIView动画
上次学习了iOS学习笔记09-核心动画CoreAnimation,这次继续学习动画,上次使用的CoreAnimation很多人感觉使用起来很繁琐,有没有更加方便的动画效果实现呢?答案是有的,那就是UI ...
- iOS学习笔记总结整理
来源:http://mobile.51cto.com/iphone-386851_all.htm 学习IOS开发这对于一个初学者来说,是一件非常挠头的事情.其实学习IOS开发无外乎平时的积累与总结.下 ...
随机推荐
- react-native 组件的导入、导出
一.前言背景: 学习react native的关键在于组件,依靠组件的拼接达到想要的效果,由此可见,组件就像一块块功能各异的零件,最终搭建出我们想要的效果. 今天我们就从组件的导入.导出开始 下面是我 ...
- torque
torque - 必应词典 美[tɔrk]英[tɔː(r)k] n.(使机器等旋转的)转矩 网络扭矩:扭力:力矩 变形过去分词:torqued:现在分词:torquing:第三人称单数:torques ...
- Linux confluence5.8.10 wiki安装
选择首先需要java环境 其次需要mysql mysql安装请参考: http://www.cnblogs.com/syuf/p/7818710.html 安装好mysql之后,创建一个库 # mys ...
- 怎么给php下拉框默认选中
比如说一个个人信息的编辑界面,从php界面传来了性别的值是0, 而html上有0 1 两个option 怎么能把0的那个option加上属性selected 用的是模板, 不要用原生的嵌在html中的 ...
- YUI前端优化之javascript,css篇
三.JavaScript和CSS篇 JavaScript和CSS也是我们页面中经常用到的内容,对它们的优化也提高网站性能的重要方面:CSS:把样式表置于顶部避免使用CSS表达式(Expression) ...
- 分布式理论系列(二)一致性算法:2PC 到 3PC 到 Paxos 到 Raft 到 Zab
分布式理论系列(二)一致性算法:2PC 到 3PC 到 Paxos 到 Raft 到 Zab 本文介绍一致性算法: 2PC 到 3PC 到 Paxos 到 Raft 到 Zab 两类一致性算法(操作原 ...
- 一起做RGB-D SLAM(8) (关于调试与补充内容)
“一起做”系列完结后,我收到不少同学给我的反馈.他们提了一些在程序编译/运行过程中的问题.我把它们汇总起来,组成了这个“补充篇”.你也可以看成是一个Q&A. Q: OpenCV的版本?A: 我 ...
- WPF 绑定备忘单
Part I – Common Examples Basic Binding {Binding} Bind to current DataContext. {Binding Name} Bi ...
- 20169221 2016——2017《网络攻防》SQL注入
准备知识 1.SQL语言 结构化查询语言(Structured Query Language)简称SQL:是一种特殊目的的编程语言,是一种数据库查询和程序设计语言,用于存取数据以及查询.更新和管理关系 ...
- Java之RandomAccessFile小结
今天跟大家分享一下javase中的关于I/O的操作: 有时我们需要在文件的末尾追加一些内容,在这时用RandomAccessFile就很好. 这个类有两个构造方法: RandomAccessFile( ...