【ios6.0 自学瞎折腾】(三)表示图填充数据
来源:互联网 发布:每月数据统计分析报表 编辑:程序博客网 时间:2024/06/16 05:25
1.首先拖入TableView;
2.然后再ViewController接口文件中添加UITableViewDelegate,UITableViewDataSource委托协议;
//// BvinViewController.h// TestTableView//// Created by Bvin on 12-12-27.// Copyright (c) 2012年 Bvin. All rights reserved.//#import <UIKit/UIKit.h>@interface BvinViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>@end
3.然后再ViewController的实现文件中,声明一个字符串数组属性,用以填充tableView(其实就相当于android里的listview),并在viewDidLoad;方法里实力化赋值。
{ NSArray *tableData;}- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. tableData = [NSArray arrayWithObjects:@"[1]南昌-Bvin",@"[1]南昌-宝宝",@"[12]B5+",@"[12]bugFix",@"[123]西班牙斗士",@"[23]福建-冰凝",@"[1]小破孩",@"[13]蜗牛哥",@"[1]大卫",@"[1]广州-风",@"[1]北京-甜瓜", nil]; //初始化赋值}
4.实现委托的两个必要方法tableView:numberOfRowsInSection:和tableView:cellForRowAtIndexPath:,前一个是返回item的总量,后面就是获取一个viewHold(用android的话来说),返回一个UITableViewCell对象,其实这个对象就相当于android中的adapter,然后用cell.textLable.text去获取数组里每个元素。
- (NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{ return [tableData count];}-(UITableViewCell*) tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *identifier = @"TestTableViewItem"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; //返回可重用的cell if(cell==nil){ cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; //如果没有可重用的,就新建一个 } cell.textLabel.text = [tableData objectAtIndex:indexPath.row]; //把数据源填充进去 return cell;}
这就像android中adapter的getItemCount()方法和getView()方法。为了高效率android采用的viewHolder方法,其实就是仿照cell的方法,回收重用。
5.然后把TableView与协议和委托链接起来,同样再xib中把TableView拖向File's Owner进行链接,连接好了就可以run一遍了!!!
后语:
由于是从android转过来的,对oc很多不懂,其实再弄这个小demo期间还是除了几个小失误!!
要谨慎,还有oc基础得去充电。
- 【ios6.0 自学瞎折腾】(三)表示图填充数据
- 【ios6.0 自学瞎折腾】(一) Helloworld!
- 【IOS6.0 自学瞎折腾】(四)Xib可视化编程
- 【ios6.0 自学瞎折腾】(二)控件交互和对话框
- 【IOS6.0 自学瞎折腾】(五)应用程序的启动过程和Application生命周期
- IOS6之AutoLayout(三)
- IOS6之AutoLayout(三)
- IOS6之AutoLayout(三)
- 数组大折腾(三).
- python自学(三)链接mysql,查询数据
- oracle自学(三)
- 自学Swift(三)
- VBS自学(三)
- JAVA自学(三)
- (三)整数表示
- BeagleBone折腾记(三):接入wifi
- strut1.0表单数据填充
- 从稀疏表示到低秩表示(三)
- [windows phone自学笔记] 第一篇 HelloWorld!
- Win8应用开发 入门篇(一) Hello world!
- [windows phone自学笔记] 第二篇 Button控件
- Win8应用开发 入门篇(二)富文本和消息框
- 【ios6.0 自学瞎折腾】(二)控件交互和对话框
- 【ios6.0 自学瞎折腾】(三)表示图填充数据
- WebService(一)Hello world!和IIS配置
- AndroidのBitmap之大图片优化
- AndroidのIntent之传递List
- AndroidのActivity之退出返回栈(二)
- AndroidのBitmap之圆形头像
- Myeclipse 8.5安装Python插件PyDev
- AndroidのBitmap之Scaletype
- Androidの网络通信之TDP