将DataSource分离并构建更轻量的UIViewController

来源:互联网 发布:软件项目管理 pdf下载 编辑:程序博客网 时间:2024/04/30 07:25

在objccn.io中看到一篇文章,构建更轻量的View Controllers,在此自己实践一下加深理解。

 

 

新疆项目,learn--tableview,类前缀为LT,开始我们的实验。

 

首先需要在StoryBoard中拖拽一个UITableView,在头文件中申明tableView变量并建立连接:

/

 

 

 

新建ArrayDataSource类,作为TableView的DataSource。目的是将DataSource从原本的ViewController中分离出来:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//
//  ArrayDataSource.h
//  objc.io example project (issue #1)
//
 
#import<foundation foundation.h="">
 
 
typedef void(^TableViewCellConfigureBlock)(id cell, id item);
 
 
@interfaceArrayDataSource : NSObject <uitableviewdatasource>
 
- (id)initWithItems:(NSArray *)anItems
     cellIdentifier:(NSString *)aCellIdentifier
 configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock;
 
- (id)itemAtIndexPath:(NSIndexPath *)indexPath;
 
@end
</uitableviewdatasource></foundation>

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//
//  ArrayDataSource.h
//  objc.io example project (issue #1)
//
 
#importArrayDataSource.h
 
 
@interfaceArrayDataSource ()
 
@property(nonatomic, strong) NSArray *items;
@property(nonatomic, copy) NSString *cellIdentifier;
@property(nonatomic, copy) TableViewCellConfigureBlock configureCellBlock;
 
@end
 
 
@implementationArrayDataSource
 
- (id)init
{
    returnnil;
}
 
- (id)initWithItems:(NSArray *)anItems
     cellIdentifier:(NSString *)aCellIdentifier
 configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock
{
    self = [superinit];
    if(self) {
        self.items = anItems;
        self.cellIdentifier = aCellIdentifier;
        self.configureCellBlock = [aConfigureCellBlock copy];
    }
    returnself;
}
 
- (id)itemAtIndexPath:(NSIndexPath *)indexPath
{
    returnself.items[(NSUInteger) indexPath.row];
}
 
 
#pragma mark UITableViewDataSource
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    returnself.items.count;
}
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier
                                                            forIndexPath:indexPath];
    id item = [self itemAtIndexPath:indexPath];
    self.configureCellBlock(cell, item);
    returncell;
}
 
@end

可以看得出来,这个DataSource的管理类接受三个变量进行初始化,分别是:

 

1.anItems,存储表格数据的对象,是一个NSArray,里面存储封装好的对象,我们并不知道它是什么类型的,所以在使用的时候用id取出其中的元素。

2.cellIdentifier,单元格的标示符,用来指定TableView使用的单元格,是单元格的唯一标识,在创建和设计Cell的时候可以指定。

3.configureCellBlock,一个用来设置每个单元格的block,因为具体的item格式我们并不知道,所以我们也就不知道该如何初始化一个cell里面的数据,需要用block进行设置,因为这个block的目的是为了将item的数据应用到cell上,所以block接受两个参数,cell和item。

 

接下来在添加一个LTMyCell类,作为自定义的单元格类。在xib中添加两个label用来显示数据:

/

 

 

 

将xib中的两个label与.h头文件建立连接,连接后的头文件如下:

 

?
1
2
3
4
+ (UINib *)nib;
 
@property (weak, nonatomic) IBOutlet UILabel *photoTitleLabel;
@property (weak, nonatomic) IBOutlet UILabel *photoDateLabel;


 

修改.m文件,实现相关方法如下:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
+ (UINib *)nib
{
    return[UINib nibWithNibName:@PhotoCellbundle:nil];
}
 
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    [supersetHighlighted:highlighted animated:animated];
    if(highlighted) {
        self.photoTitleLabel.shadowColor = [UIColor darkGrayColor];
        self.photoTitleLabel.shadowOffset = CGSizeMake(3,3);
    }else {
        self.photoTitleLabel.shadowColor = nil;
    }
}


 

 

接着,新建LTPhoto的封装类,我们需要把用来展示的数据进行分装:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
//  LTPhoto.h
//  learn-tableview
//
//  Created by why on 8/11/14.
//  Copyright (c) 2014 why. All rights reserved.
//
 
#import<foundation foundation.h="">
 
@interfaceLTPhoto : NSObject <nscoding>
 
@property(nonatomic, copy) NSString* name;
@property(nonatomic, strong) NSDate* creationDate;
 
@end
 
 
 
//
//  LTPhoto.m
//  learn-tableview
//
//  Created by why on 8/11/14.
//  Copyright (c) 2014 why. All rights reserved.
//
 
#importLTPhoto.h
 
 
static NSString * const IdentifierKey = @identifier;
static NSString * const NameKey = @name;
static NSString * const CreationDateKey = @creationDate;
static NSString * const RatingKey = @rating;
 
 
@implementationLTPhoto
 
 
- (void)encodeWithCoder:(NSCoder*)coder
{
    [coder encodeObject:self.name forKey:NameKey];
    [coder encodeObject:self.creationDate forKey:CreationDateKey];
}
 
- (BOOL)requiresSecureCoding
{
    returnYES;
}
 
- (id)initWithCoder:(NSCoder*)coder
{
    self = [superinit];
    if(self) {
        self.name = [coder decodeObjectOfClass:[NSStringclass] forKey:NameKey];
        self.creationDate = [coder decodeObjectOfClass:[NSDateclass] forKey:CreationDateKey];
    }
    returnself;
}
 
@end
 
</nscoding></foundation>


 

在写完了LTPhoto这个封装对象之后,我们可以对原来的MyCell进行Category扩展。新建一个Category:

/


具体代码如下:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#importLTMyCell.h
 
@class LTPhoto;
 
@interfaceLTMyCell (ConfigureForPhoto)
- (void)configureForPhoto:(LTPhoto *)photo;
 
@end
 
 
//
//  LTMyCell+ConfigureForPhoto.m
//  learn-tableview
//
//  Created by why on 8/11/14.
//  Copyright (c) 2014 why. All rights reserved.
//
 
#importLTMyCell+ConfigureForPhoto.h
#importLTPhoto.h
 
 
@implementationLTMyCell (ConfigureForPhoto)
 
- (void)configureForPhoto:(LTPhoto *)photo
{
    self.photoTitleLabel.text = photo.name;
    self.photoDateLabel.text = [self.dateFormatter stringFromDate:photo.creationDate];
}
 
- (NSDateFormatter *)dateFormatter
{
    staticNSDateFormatter *dateFormatter;
    if(!dateFormatter) {
        dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.timeStyle = NSDateFormatterMediumStyle;
        dateFormatter.dateStyle = NSDateFormatterMediumStyle;
    }
    returndateFormatter;
}
 
@end


 

 

 

接下来就是在ViewController中指定TableView的DataSource。修改m文件代码如下:

 

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//
//  LTViewController.m
//  learn-tableview
//
//  Created by why on 8/11/14.
//  Copyright (c) 2014 why. All rights reserved.
//
 
#importLTViewController.h
#importArrayDataSource.h
#importLTMyCell.h
#importLTMyCell+ConfigureForPhoto.h
#importLTPhoto.h
 
 
static NSString * const PhotoCellIdentifier = @LTMyCell;
 
 
 
@interfaceLTViewController ()<uitableviewdelegate>
 
@property(nonatomic, strong) ArrayDataSource *photosArrayDataSource;
 
 
@end
 
@implementationLTViewController
 
- (void)viewDidLoad
{
    [superviewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self setupTableView];
 
}
 
 
- (void)setupTableView
{
     
    TableViewCellConfigureBlock configureCell = ^(LTMyCell *cell, LTPhoto *photo) {
        [cell configureForPhoto:photo];
    };
     
    NSMutableArray *photos = [[NSMutableArray alloc] init];
    for(int i = 0; i < 10; i++) {
        LTPhoto *photo = [[LTPhoto alloc] init];
        photo.name =@Hello;
        photo.creationDate = [NSDate date];
        [photos addObject:photo];
    }
     
    self.photosArrayDataSource = [[ArrayDataSource alloc] initWithItems:photos
                                                         cellIdentifier:PhotoCellIdentifier
                                                     configureCellBlock:configureCell];
     
    _tableVIew.dataSource = self.photosArrayDataSource;
     
    [_tableVIew registerNib:[LTMyCell nib] forCellReuseIdentifier:PhotoCellIdentifier];
     
}
 
#pragma mark UITableViewDelegate
 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@Click!);
}
 
 
 
 
- (void)didReceiveMemoryWarning
{
    [superdidReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
@end
</uitableviewdelegate>

这样就实现了基本的DataSource分离。

 

0 0
原创粉丝点击