【读书笔记】iOS-简单的数据驱动程序
2015-08-24 18:36
411 查看
一,效果图。
二,,工程文件如下图所示:
三,DataModel.h
#import <Foundation/Foundation.h>
@interface DataModel : NSObject
{
NSArray *myData;
}
-(NSString *)getNameAtIndex:(int)index;
-(int)getRowCount;
@end
DataModel.m
//数据库文件
#import "DataModel.h"
@implementation DataModel
-(id)init
{
if (self=[super init]) {
myData=[[NSArray alloc]initWithObjects:@"first",@"second",@"three",@"four", nil];
}
return self;
}
//显示数组中数据
-(NSString *)getNameAtIndex:(int)index
{
return (NSString *)[myData objectAtIndex:index];
}
//显示行数
-(int)getRowCount
{
return (int)[myData count];
}
@end
四,ViewController.h
#import <UIKit/UIKit.h>
#import "DataModel.h"
@interface ViewController : UIViewController
<UITableViewDataSource,UITableViewDelegate>
{
UITableView *myTableView;
DataModel *model;
}
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//初始化数据
[self initData];
//初始化界面
[self addBackgroundView];
}
#pragma -mark -functions
//初始化数据
-(void)initData
{
model=[[DataModel alloc]init];
}
//初始化界面
-(void)addBackgroundView
{
myTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 100, 320, 300)];
myTableView.dataSource=self;
myTableView.delegate=self;
[self.view addSubview:myTableView];
}
#pragma -mark -UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [model getRowCount];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 40;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=@"Cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text=[NSString stringWithFormat:@"%@",[model getNameAtIndex:(int)indexPath.row]];
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
参考资料:《iOS数据库应用高级编程(第2版)》
二,,工程文件如下图所示:
三,DataModel.h
#import <Foundation/Foundation.h>
@interface DataModel : NSObject
{
NSArray *myData;
}
-(NSString *)getNameAtIndex:(int)index;
-(int)getRowCount;
@end
DataModel.m
//数据库文件
#import "DataModel.h"
@implementation DataModel
-(id)init
{
if (self=[super init]) {
myData=[[NSArray alloc]initWithObjects:@"first",@"second",@"three",@"four", nil];
}
return self;
}
//显示数组中数据
-(NSString *)getNameAtIndex:(int)index
{
return (NSString *)[myData objectAtIndex:index];
}
//显示行数
-(int)getRowCount
{
return (int)[myData count];
}
@end
四,ViewController.h
#import <UIKit/UIKit.h>
#import "DataModel.h"
@interface ViewController : UIViewController
<UITableViewDataSource,UITableViewDelegate>
{
UITableView *myTableView;
DataModel *model;
}
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//初始化数据
[self initData];
//初始化界面
[self addBackgroundView];
}
#pragma -mark -functions
//初始化数据
-(void)initData
{
model=[[DataModel alloc]init];
}
//初始化界面
-(void)addBackgroundView
{
myTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 100, 320, 300)];
myTableView.dataSource=self;
myTableView.delegate=self;
[self.view addSubview:myTableView];
}
#pragma -mark -UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [model getRowCount];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 40;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=@"Cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text=[NSString stringWithFormat:@"%@",[model getNameAtIndex:(int)indexPath.row]];
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
参考资料:《iOS数据库应用高级编程(第2版)》
相关文章推荐
- 【读书笔记】iOS-简单的数据驱动程序
- IOS RSA加密解密
- IOS7导航条与状态栏的那些事儿
- iOS 深入理解“属性”及其关键字
- iOS 添加粘贴板的功能(复制功能)
- phonegap插件-使用ios原生输入框解决position fixed的显示问题
- iOS程序框架设计之皮肤切换功能 (白天与夜间效果)
- Quartz2d从易到难全解析---利用位图上下文打水印,并保存图片
- iOS--开发文本输入框遇到的一些问题
- iOS7 Navgation的手势滑动返回功能
- iOS开发-Day30 参数传递的五种方法
- ios笔试题大全
- iOS设备中WiFi、蓝牙和飞行模式的开启与关闭
- iOS:(接口适配器3)--iPhone适应不同型号 6/6plus 前
- 008 [iOS图像]一、iOS中的那些图片格式
- Activity随手势滑动退出,及模糊Dialog(类似于IOS的效果)
- iOS自动化常用命令集合
- iOS- NSThread/NSOperation/GCD 三种多线程技术的对比及实现
- IOS集成支付宝回调的坑
- ios开发里的第三方高级框架神器