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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//
// ICRStoreListViewController.m
// XFFruit
//
// Created by Xummer on 4/6/15.
// Copyright (c) 2015 Xummer. All rights reserved.
//
#import "ICRStoreListViewController.h"
#import "ICRStore.h"
#import "IBTUISearchBar.h"
static NSString *StoreListCellIdentifier = @"StoreListCell";
@interface ICRStoreListViewController ()
<
UITableViewDataSource,
UITableViewDelegate,
UISearchBarDelegate,
UISearchDisplayDelegate
>
@property (strong, nonatomic) IBTTableView *m_tableView;
@property (strong, nonatomic) NSArray *m_arrStores;
@property (strong, nonatomic) IBTUISearchBar *m_searchBar;
@property (strong, nonatomic) NSArray *m_searchResult;
@property (strong, nonatomic) UISearchDisplayController *m_searchDisplayCtr;
@end
@implementation ICRStoreListViewController
#pragma mark - Life Cycle
- (instancetype)initWithBHaveToChooseOne:(BOOL)bHaveTo {
self = [super init];
if (!self) {
return nil;
}
self.bNotAutoAddCancelButton = bHaveTo;
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self initTableView];
[self fetchStoreList];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self doHTTPGetStoreList];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Private Method
- (void)initTableView {
CGRect frame = self.view.bounds;
frame.size.height = IBT_SEARCH_BAR_HEIGHT;
self.m_searchBar = [[IBTUISearchBar alloc] initWithFrame:frame];
_m_searchBar.placeholder = [IBTCommon localizableString:@"Search"];
_m_searchBar.sbDelegate = self;
self.m_tableView =
[[IBTTableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
[_m_tableView autoresizingWithStrechFullSize];
[_m_tableView registerClass:[IBTTableViewCell class]
forCellReuseIdentifier:StoreListCellIdentifier];
_m_tableView.tableHeaderView = _m_searchBar;
_m_tableView.dataSource = self;
_m_tableView.delegate = self;
[self.view addSubview:_m_tableView];
self.m_searchDisplayCtr = [[UISearchDisplayController alloc] initWithSearchBar:_m_searchBar contentsController:self];
_m_searchDisplayCtr.delegate = self;
_m_searchDisplayCtr.searchResultsDataSource = self;
_m_searchDisplayCtr.searchResultsDelegate = self;
}
- (void)configureTableView:(UITableView *)tableView {
tableView.frame = self.view.frame;
[tableView registerClass:[IBTTableViewCell class]
forCellReuseIdentifier:StoreListCellIdentifier];
UIView *tableFooterViewToGetRidOfBlankRows = [[UIView alloc] initWithFrame:CGRectZero];
tableFooterViewToGetRidOfBlankRows.backgroundColor = [UIColor clearColor];
tableView.tableFooterView = tableFooterViewToGetRidOfBlankRows;
}
- (void)doHTTPGetStoreList {
__weak typeof(self)weakSelf = self;
void(^succ)(id) = ^(id data) {
__strong __typeof(weakSelf)strongSelf = weakSelf;
[strongSelf fetchStoreList];
};
void(^fail)(id) = ^(id data) {
[IBTLoadingView showTips:data];
};
ICRHTTPController *httpCtrl = [ICRHTTPController sharedController];
[httpCtrl doGetStoreListFromUpdateTime:0 position:0 size:20
success:succ
failure:fail];
}
- (void)fetchStoreList {
ICRDatabaseFetchBlock fetchBlk = ^FMResultSet *(FMDatabase *db) {
NSString * sql = [NSString stringWithFormat:@"SELECT * FROM %@ ORDER BY %@", [ICRStore TableName], @"uuid"];
return [db executeQuery:sql];
};
__weak typeof(self)weakSelf = self;
ICRDatabaseFetchResultsBlock fetchResultsBlk = ^(NSArray *fetchedObjects) {
__strong __typeof(weakSelf)strongSelf = weakSelf;
strongSelf.m_arrStores = fetchedObjects;
[strongSelf.m_tableView reloadData];
};
ICRDataBaseController *dbCtrl = [ICRDataBaseController sharedController];
[dbCtrl runFetchForClass:[ICRStore class]
fetchBlock:fetchBlk
fetchResultsBlock:fetchResultsBlk];
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.m_tableView) {
return [_m_arrStores count];
} else {
return [_m_searchResult count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:StoreListCellIdentifier
forIndexPath:indexPath];
[self configureCell:cell forRowAtIndexPath:indexPath inTableview:tableView];
return cell;
}
- (void)configureCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
inTableview:(UITableView *)tableView
{
ICRStore *store = nil;
if (tableView == _m_tableView) {
store = _m_arrStores[ indexPath.row ];
} else {
store = _m_searchResult[ indexPath.row ];
}
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.textLabel.textColor = [UIColor grayColor];
cell.textLabel.text = store.name;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if ([_m_delegate respondsToSelector:@selector(storeListViewCtrl:didSelectedStore:)]) {
if (tableView == _m_tableView) {
[_m_delegate storeListViewCtrl:self didSelectedStore:_m_arrStores[ indexPath.row ]];
} else
{
[_m_delegate storeListViewCtrl:self didSelectedStore:_m_searchResult[ indexPath.row ]];
}
}
else {
[self disMissSelf];
}
}
#pragma mark UISearchDisplayDelegate
- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView {
[self configureTableView:tableView];
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH[cd] %@", searchString];
self.m_searchResult = [self.m_arrStores filteredArrayUsingPredicate:predicate];
return YES;
}
@end