PlaceAroundTableView.m
7.3 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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//
// PlaceAroundTableView.m
// CMDataCollectTool
//
// Created by PC on 15/8/7.
// Copyright (c) 2015年 autonavi. All rights reserved.
//
#import "PlaceAroundTableView.h"
#define kMoreButtonTitle @"更多..."
@interface PlaceAroundTableView()
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) AMapPOI *selectedPoi;
@property (nonatomic, assign) BOOL isFromMoreButton;
@property (nonatomic, strong) UIButton *moreButton;
@end
@implementation PlaceAroundTableView
#pragma mark - Interface
- (AMapPOI *)selectedTableViewCellPoi
{
return self.selectedPoi;
}
#pragma mark - AMapSearchDelegate
- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response
{
if (self.isFromMoreButton == YES)
{
self.isFromMoreButton = NO;
}
else
{
[self.searchPoiArray removeAllObjects];
[self.moreButton setTitle:kMoreButtonTitle forState:UIControlStateNormal];
self.moreButton.enabled = YES;
self.moreButton.backgroundColor = [UIColor whiteColor];
}
if (response.pois.count == 0)
{
[self.moreButton setTitle:@"没有数据了.." forState:UIControlStateNormal];
self.moreButton.enabled = NO;
self.moreButton.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.4];
self.selectedIndexPath = nil;
[self.tableView reloadData];
return;
}
[response.pois enumerateObjectsUsingBlock:^(AMapPOI *obj, NSUInteger idx, BOOL *stop) {
[self.searchPoiArray addObject:obj];
}];
[[NSNotificationCenter defaultCenter]postNotificationName:@"noti_sendButton" object:nil];
self.selectedIndexPath = nil;
[self.tableView reloadData];
}
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
{
if (response.regeocode != nil)
{
self.currentAddress = response.regeocode.formattedAddress;
self.cityAddress = response.regeocode.addressComponent;
NSIndexPath *reloadIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView reloadRowsAtIndexPaths:@[reloadIndexPath] withRowAnimation:UITableViewRowAnimationNone];
}
}
#pragma mark - UITableView Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType= UITableViewCellAccessoryCheckmark;
self.selectedIndexPath = indexPath;
[tableView reloadData];
if (indexPath.section == 0)
{
self.selectedPoi = nil;
[self.delegate didPositionCellTapped];
return ;
}
self.selectedPoi = self.searchPoiArray[indexPath.row];
if ([self.delegate respondsToSelector:@selector(didTableViewSelectedChanged:)])
{
[self.delegate didTableViewSelectedChanged:self.selectedPoi];
}
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
}
#pragma mark - UITableView Datasource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *reusedIndentifier = @"reusedIndentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedIndentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reusedIndentifier];
}
if (indexPath.section == 0)
{
cell.textLabel.text = @"[位置]";
// 如果没有地址,就设置成"未知"
if([CommonFun isBlankOrNullString:self.currentAddress]){
self.currentAddress = @"未知";
}
cell.detailTextLabel.text = self.currentAddress;
}
else
{
AMapPOI *poi = self.searchPoiArray[indexPath.row];
cell.textLabel.text = poi.name;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@%@%@%@",poi.province,poi.city,poi.district,poi.address];
cell.detailTextLabel.textColor = UIColorFromRGB(0x999999, 1.0);
}
if (self.selectedIndexPath && self.selectedIndexPath.section == indexPath.section && self.selectedIndexPath.row == indexPath.row)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else if (!self.selectedIndexPath && indexPath.row == 0)
{
self.selectedIndexPath = indexPath;
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)
{
return 1;
}
else
{
return self.searchPoiArray.count;
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
#pragma mark - Handle Action
- (void)actionMoreButtonTapped
{
// 防止快速连续点两次
if (self.isFromMoreButton == YES)
{
return;
}
if ([self.delegate respondsToSelector:@selector(didLoadMorePOIButtonTapped)])
{
self.isFromMoreButton = YES;
[self.delegate didLoadMorePOIButtonTapped];
}
}
#pragma mark - Initialization
- (NSMutableArray *)searchPoiArray
{
if (_searchPoiArray == nil)
{
_searchPoiArray = [[NSMutableArray alloc] init];
}
return _searchPoiArray;
}
- (void)initTableViewFooter
{
#define kMoreButtonMargin 20
UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 60)];
UIButton *moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
moreBtn.frame = footer.bounds;
[moreBtn setTitle:kMoreButtonTitle forState:UIControlStateNormal];
[moreBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[moreBtn setTitleColor:[[UIColor grayColor] colorWithAlphaComponent:0.4] forState:UIControlStateHighlighted];
moreBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
moreBtn.titleEdgeInsets = UIEdgeInsetsMake(0, kMoreButtonMargin, 0, 0);
[moreBtn addTarget:self action:@selector(actionMoreButtonTapped) forControlEvents:UIControlEventTouchUpInside];
self.moreButton = moreBtn;
[footer addSubview:moreBtn];
UIView *upLineView = [[UIView alloc] initWithFrame:CGRectMake(kMoreButtonMargin, 3, ScreenWidth - kMoreButtonMargin, 0.5)];
upLineView.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.4];
[footer addSubview:upLineView];
self.tableView.tableFooterView = footer;
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.isFromMoreButton = NO;
[self addSubview:self.tableView];
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self);
}];
[self initTableViewFooter];
}
return self;
}
@end