您的位置:首页 > 移动开发 > IOS开发

【读书笔记】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版)》
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: