[Reading Notes] iOS-simple data driver, Reading Notes ios-

Source: Internet
Author: User

[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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.