zoukankan      html  css  js  c++  java
  • 【读书笔记】iOS-简单的数据驱动程序

    一,效果图。

     

     

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

     

     

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

  • 相关阅读:
    win7下 iis配置 不能添加默认文档的 解决方案
    经典SQL语句大全
    Sql 行转列问题总结
    jQuery获取Select选择的Text和 Value(转)
    如何让虚拟目录里面的webconfig不继承网站的设置
    SQL2008 用户'sa'登录失败(错误18456)图文解决方法
    ASP.NET实现公历转农历的简单方法
    你的DNN站点慢了么?
    SQLServer2005删除log文件和清空日志的方案
    由于未能创建 Microsoft Visual C# 2008 编译器,因此未能打开项目 "xxx"
  • 原文地址:https://www.cnblogs.com/yang-guang-girl/p/4755588.html
Copyright © 2011-2022 走看看