[Reading Notes] iOS-simple data driver, Reading Notes ios-
I ,.
2. Shows the project file:
3. DataModel. h
# Import <Foundation/Foundation. h>
@ Interface DataModel: NSObject
{
NSArray * myData;
}
-(NSString *) getNameAtIndex :( int) index;
-(Int) getRowCount;
@ End
DataModel. m
// Database file
# Import "DataModel. h"
@ Implementation DataModel
-(Id) init
{
If (self = [super init]) {
MyData = [[NSArray alloc] initWithObjects: @ "first", @ "second", @ "three", @ "four", nil];
}
Return self;
}
// Display the data in the array
-(NSString *) getNameAtIndex :( int) index
{
Return (NSString *) [myData objectAtIndex: index];
}
// Display the number of rows
-(Int) getRowCount
{
Return (int) [myData count];
}
@ End
Iv. 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.
// Initialize data
[Self initData];
// Initialization Interface
[Self addBackgroundView];
}
# Pragma-mark-functions
// Initialize data
-(Void) initData
{
Model = [[DataModel alloc] init];
}
// Initialization Interface
-(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
Reference: iOS database application advanced programming (version 2nd)