主要是因为父类中会调用其分隔线位置调整,    //初始化数据

//显示数组中数据,//数据库文件,//显示数组中数据,//数据库文件,主要是因为父类中会调用其分隔线位置调整,if (,refresh.attributedTitle =,refresh.attributedTitle =

图片 12

【读书笔记】iOS-简单的数据驱动程序,读书笔记ios-

一,效果图。

 

图片 1

 

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

 

图片 2

 

三,DataModel.h

图片 3

#import <Foundation/Foundation.h>

 

@interface DataModel : NSObject

{

    NSArray *myData;

}

-(NSString *)getNameAtIndex:(int)index;

-(int)getRowCount;

 

@end

 

 

图片 4

 

 DataModel.m

图片 5

//数据库文件

#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

 

图片 6

 

四,ViewController.h

图片 7

#import <UIKit/UIKit.h>

#import “DataModel.h”

 

@interface ViewController : UIViewController

<UITableViewDataSource,UITableViewDelegate>

{

    UITableView *myTableView;

    DataModel *model;

}

@end

 

图片 8

ViewController.m

图片 9

#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
*[email protected]”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

 

图片 10

参考资料:《iOS数据库应用高级编程(第2版)》

一,效果图。 二,,工程文件如下图所示: 三,DataModel.h #import
Foundation/Foundation.h @i…

一,效果图。

iOS 代码自定义cell示例

底色标黄为代码自定义cell重点处,入手从这几点即可。

 

MyCell.h

 

#import

 

@interface MyCell :UITableViewCell

@property(nonatomic,strong)UILabel *ageLabel;

@property(nonatomic,strong)UILabel *nameLabel;

@property(nonatomic,strong)UILabel *additionLabel;

– (instancetype)initWithStyle:(UITableViewCellStyle)style
reuseIdentifier:(NSString *)reuseIdentifier;

@end

MyCell.m

 

 

#import MyCell.h

 

@implementation MyCell

 

– (instancetype)initWithStyle:(UITableViewCellStyle)style
reuseIdentifier:(NSString *)reuseIdentifier

{

if (self = [superinitWithStyle:stylereuseIdentifier:reuseIdentifier])
{

 

// for (UIView *view in self.contentView.subviews) {

// NSLog(@%@,view);

// [view removeFromSuperview];

// }

 

_ageLabel = [[UILabelalloc]init];

_nameLabel = [[UILabelalloc]init];

_additionLabel = [[UILabelalloc]init];

_additionLabel.text
[email protected]点击;

[self.contentViewaddSubview:_ageLabel];

[self.contentViewaddSubview:_nameLabel];

[self.contentViewaddSubview:_additionLabel];

self.backgroundColor = [UIColorclearColor];

}

return self;

}

 

– (void)layoutSubviews

{

[superlayoutSubviews];
//如不调用父类此方法就会导致在使用时出现分隔线位置不正确,主要是因为父类中会调用其分隔线位置调整

self.nameLabel.frame =CGRectMake(0,0,100,44);

self.ageLabel.frame =CGRectMake(100,0,100,44);

self.additionLabel.frame =CGRectMake(0,40,320, 20);

}

@end

控制器.m

 

#import ViewController.h

#import ConcernViewController.h

#import MyCell.h

 

@interface ViewController ()

@property(nonatomic,strong)UITableView *myTableView;

@property(nonatomic,strong)NSMutableArray *myData;

@end

 

@implementation ViewController

 

 

– (NSMutableArray *)myData

{

if (!_myData)
{//如果想修改字典中的值,字典必须为可变字典不然就会出现问题

 

NSMutableDictionary *dict1 =
[NSMutableDictionarydictionaryWithObjects:@[@hua,@12,@NO]forKeys:@[@name,@age,@flag]];

NSMutableDictionary *dict2 =
[NSMutableDictionarydictionaryWithObjects:@[@huhu,@14,@NO]forKeys:@[@name,@age,@flag]];

NSMutableDictionary *dict3 =
[NSMutableDictionarydictionaryWithObjects:@[@huxun,@16,@YES]forKeys:@[@name,@age,@flag]];

 

_myData = [NSMutableArray arrayWithObjects:dict1,dict2,dict3, nil];

}

return_myData;

}

 

– (void)viewDidLoad {

[superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

CGFloat customW =self.view.bounds.size.width;

CGFloat customH =self.view.bounds.size.height;

UITableView *tableview = [[UITableView alloc]
initWithFrame:CGRectMake(10,44, customW -2 *10, customH
-80)style:UITableViewStylePlain];

tableview.delegate =self;

tableview.dataSource =self;

// tableview.separatorStyle = UITableViewCellSeparatorStyleNone;

tableview.separatorColor = [UIColorblueColor];

tableview.separatorInset =UIEdgeInsetsMake(0,20,0, 20);

 

 

self.myTableView = tableview;

[self.viewaddSubview:self.myTableView];

 

 

 

}

– (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 1;

}

 

/**

* 返回每组有多少行

*

*/

– (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section

{

return self.myData.count;

}

 

/**

* 返回cell

*

*/

– (MyCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

staticNSString *ID = @MyCell;

MyCell *cell = [tableViewdequeueReusableCellWithIdentifier:ID]; //
cell的重用

if (cell ==nil) {

cell =
[[MyCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:ID];

}

// cell值的初始化

return cell;

}

 

– (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath;

{

if ([self.myData[indexPath.row][@flag]boolValue]) {

return 44 + 30;

}

return 44;

 

}

 

– (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

}

 

 

– (void)didReceiveMemoryWarning {

[superdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

 

@end

 

代码自定义cell示例
底色标黄为代码自定义cell重点处,入手从这几点即可。 MyCell.h #import
@interface MyCell :UITableViewCell @property(nonatomic,stro…

//

IOS原生态刷新控件实用——UIRefreshControl

//

// ViewController.m

// 代码自定义cell

//

// Created by mac on 15/4/18.

// Copyright (c) 2015年 mac. All rights reserved.

//

 

#import “ViewController.h”

//#import “myTableViewCell.h”

@interface ViewController ()

 

@end

 

@implementation ViewController

 

– (void)viewDidLoad {

[super viewDidLoad];

 

[self addArrayM];

_myTableView = [[UITableView alloc]initWithFrame:[UIScreen
mainScreen].bounds style:UITableViewStylePlain];

_myTableView.delegate = self;

_myTableView.dataSource = self;

_myTableView.backgroundColor = [UIColor whiteColor];

[self.view addSubview:_myTableView];

[_myTableView reloadData];

 

/******内置刷新的常用属性设置******/

_refresh = [[UIRefreshControl alloc] init];

_refresh.tintColor = [UIColor redColor];

_refresh.attributedTitle =[[NSAttributedString
alloc]initWithString:@”智课网正在加载”];

[_refresh addTarget:self action:@selector(pullToRefresh)
forControlEvents:UIControlEventValueChanged];

 

[_myTableView addSubview:_refresh];

 

// UIActivityViewController *act = [[UIActivityViewController
alloc]initw];

// Do any additional setup after loading the view, typically from a nib.

}

-(void)pullToRefresh

{

[self.arrayM addObjectsFromArray:self.arrayM];

[_myTableView reloadData];

[NSTimer scheduledTimerWithTimeInterval:2.0 target:self
selector:@selector(timerStop) userInfo:nil repeats:NO];

}

-(void)timerStop

{

[_refresh endRefreshing];

 

}

-(void)addArrayM

{

NSString *one = @”A”;

NSString *one1 = @”B”;

NSString *one11 = @”C”;

NSString *one111 = @”D”;

NSString *one1111= @”E”;

NSString *one11111= @”F”;

NSString *one111111 = @”G”;

_arrayM = [NSMutableArray
arrayWithObjects:one,one1,one11,one111,one1111,one11111,one111111,
nil];

}

-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section

{

return [_arrayM count];

}

-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *ID = @”cell”;

UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:ID];

if (!cell) {

cell = [[UITableViewCell
alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];

 

 

 

}

cell.imageView.image = [UIImage imageNamed:@”iosshare”];

cell.textLabel.text = [_arrayM objectAtIndex:indexPath.row];

return cell;

}

-(void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

 

NSLog(@”点击我了”);

}

-(CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

 

 

return 80;

}

– (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

 

@end

// //
ViewController.m // 代码自定义cell // // Created by mac on 15/4/18. //
Copyright (c) 2015年 mac. All rights reserve…

 

// ViewController.m

图片 11

// 代码自定义cell

 

//

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

// Created by mac on 15/4/18.

 

// Copyright (c) 2015年 mac. All rights reserved.

图片 12

//

 

 

三,DataModel.h

#import “ViewController.h”

图片 13

//#import “myTableViewCell.h”

#import
<Foundation/Foundation.h>

@interface ViewController ()

 

 

@interface DataModel : NSObject

@end

{

 

    NSArray *myData;

@implementation ViewController

}

 

-(NSString
*)getNameAtIndex:(int)index;

– (void)viewDidLoad {

-(int)getRowCount;

[super viewDidLoad];

 

 

@end

[self addArrayM];

 

_myTableView = [[UITableView alloc]initWithFrame:[UIScreen
mainScreen].bounds style:UITableViewStylePlain];

 

_myTableView.delegate = self;

图片 14

_myTableView.dataSource = self;

 

_myTableView.backgroundColor = [UIColor whiteColor];

 DataModel.m

[self.view addSubview:_myTableView];

图片 15

[_myTableView reloadData];