国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

【讀書筆記】iOS-簡單的數據驅動程序

2019-11-14 18:28:05
字體:
來源:轉載
供稿:網友

一,效果圖。

 

 

二,,工程文件如下圖所示:

 

 

三,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版)》


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 金秀| 扎囊县| 兴化市| 尉氏县| 金堂县| 曲沃县| 江津市| 巴青县| 革吉县| 耒阳市| 龙口市| 万年县| 梨树县| 林西县| 行唐县| 库车县| 政和县| 闸北区| 文化| 襄汾县| 长兴县| 定边县| 桐柏县| 海晏县| 湘西| 工布江达县| 龙井市| 宁都县| 虞城县| 阿荣旗| 师宗县| 涟源市| 浦江县| 礼泉县| 福鼎市| 邻水| 临潭县| 呼伦贝尔市| 邵武市| 楚雄市| 呼图壁县|