-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMPKArrayDataSource.m
More file actions
87 lines (70 loc) · 2.21 KB
/
MPKArrayDataSource.m
File metadata and controls
87 lines (70 loc) · 2.21 KB
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
81
82
83
84
85
86
//
// MPKArrayDataSource.m
//
// Created by Mike Rudoy on 07.08.15.
// Copyright (c) 2015 MPK. All rights reserved.
//
#import "MPKArrayDataSource.h"
@interface MPKArrayDataSource ()
@property (nonatomic, strong) NSArray *items;
@property (nonatomic, copy) NSString *cellIdentifier;
@property (nonatomic, copy) TableViewCellConfigureBlock configureCellBlock;
@property (nonatomic, strong) UILabel *emptyItemsLabel;
@end
@implementation MPKArrayDataSource
- (id)init
{
return nil;
}
- (id)initWithItems:(NSArray *)anItems
cellIdentifier:(NSString *)aCellIdentifier
configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock
{
return [self initWithItems:anItems
cellIdentifier:aCellIdentifier
configureCellBlock:aConfigureCellBlock
emptyItemsLabel:nil];
}
- (id)initWithItems:(NSArray *)anItems
cellIdentifier:(NSString *)aCellIdentifier
configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock
emptyItemsLabel:(UILabel *)label
{
self = [super init];
if (self) {
self.items = anItems;
self.cellIdentifier = aCellIdentifier;
self.configureCellBlock = [aConfigureCellBlock copy];
self.emptyItemsLabel = label;
}
return self;
}
- (id)itemAtIndexPath:(NSIndexPath *)indexPath
{
return self.items[(NSUInteger) indexPath.row];
}
#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (self.items.count == 0 && self.emptyItemsLabel) {
tableView.backgroundView = self.emptyItemsLabel;
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
return 0;
}
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.items.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier
forIndexPath:indexPath];
cell.tag = indexPath.row;
id item = [self itemAtIndexPath:indexPath];
self.configureCellBlock(cell, item);
return cell;
}
@end