iOS-代码逻辑参考(不错)

1
2
3
4
5
6
7
8
9
10
#import "TDFatherUIViewcontroller.h"

@interface TDMarketPayChoosePlaceViewController : TDFatherUIViewcontroller

@property (nonatomic,copy)NSString *goodsID;
@property (nonatomic,copy)NSString *pointsID;

@property(nonatomic,copy) void (^tdMarketPayChoosePlaceViewControllerBlock)(NSString *address,NSString *deliveryPointID);

@end

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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#import "TDMarketPayChoosePlaceViewController.h"
#import "TDMarketPayPlaceTableViewCell.h"
#import "TDMarketDataUtil.h"
#import "TDDeleveryPoints.h"

@interface TDMarketPayChoosePlaceViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)TDTableView *tableView;
@property (strong, nonatomic) NSArray *houseArray;
@property(nonatomic,strong)UIView* noNetWorkView;
@property(nonatomic,strong)UIView* noDataView;
@property (nonatomic,strong)TDDeleveryPoints *model;
@property(nonatomic,strong)NSIndexPath *lastIndexPath;
@property (nonatomic,copy)NSString *address;
@property (nonatomic,copy)NSString *deliveryPointID;
@end

@implementation TDMarketPayChoosePlaceViewController
{
BOOL networking;
}

- (void)viewDidLoad
{
[super viewDidLoad];

self.title = @"选择提货地址";
self.noDataView = [TDMainNoDataHelper addNoDataViewWithTextString:@"暂无数据"];
self.houseArray = @[];

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStyleDone target:self action:@selector(saveAddress)];

[self setupTableView];
[self requestDeliveryPointsData];
}

#pragma mark - 保存并退出
- (void)saveAddress
{
if (self.tdMarketPayChoosePlaceViewControllerBlock)
{
self.tdMarketPayChoosePlaceViewControllerBlock(self.address,self.deliveryPointID);
}

[self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 创建表格视图
- (void)setupTableView
{
TDTableView *tableView = [[TDTableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64)];
tableView.delegate = self;
tableView.dataSource = self;
tableView.estimatedRowHeight = 44;
tableView.showsVerticalScrollIndicator = NO;
tableView.rowHeight = UITableViewAutomaticDimension;
[tableView registerNib:[UINib nibWithNibName:NSStringFromClass([TDMarketPayPlaceTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([TDMarketPayPlaceTableViewCell class])];

[self.view addSubview:tableView];

self.tableView = tableView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.model.deliveryPoints.count +1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

if (section == 0)
{
return 44;
}else{
return 32;
}
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if (section == 0) {
UIView *view = [UIView new];

UILabel *headsectionLabel = [UILabel new];
headsectionLabel.textColor = [UIColor colorWithHexString:@"4A4A4A"];
headsectionLabel.font = [UIFont systemFontOfSize:12];
headsectionLabel.frame = CGRectMake(12, 0, [UIScreen mainScreen].bounds.size.width-32, 44);
headsectionLabel.numberOfLines = 0;
headsectionLabel.text = @"本商品的配送方式为指定提货点自提,请在以下选项中选择一个最方便您自提的地点:";
[view addSubview:headsectionLabel];

return view;

}else{

UIView *headview = [UIView new];
headview.backgroundColor = [UIColor colorWithHexString:@"F5F5F5"];

UILabel *headsectionLabel = [UILabel new];
headsectionLabel.textColor = [UIColor colorWithHexString:@"888888"];
headsectionLabel.font = [UIFont systemFontOfSize:16];
headsectionLabel.frame = CGRectMake(12, 0, [UIScreen mainScreen].bounds.size.width-12, 32);
[headview addSubview:headsectionLabel];

TDDeleveryPointsModel *pModel = self.model.deliveryPoints[section - 1];
headsectionLabel.text = pModel.communityName;

return headview;
}

}

#pragma mark - TableView Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0) {
return 0;
}

TDDeleveryPointsModel *pModel = self.model.deliveryPoints[section - 1];
return pModel.deliveryPointList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *homeID = @"TDMarketPayPlaceTableViewCell";
TDMarketPayPlaceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:homeID];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (cell == nil) {
cell = [[TDMarketPayPlaceTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:homeID];
}

TDDeleveryPointsModel *pModel = self.model.deliveryPoints[indexPath.section - 1];
TDDeleveryPointsListModel *listMode = pModel.deliveryPointList[indexPath.row];

cell.model = listMode;
cell.pointsModel = pModel;

return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
TDMarketPayPlaceTableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.model.hasChecked = @1;
self.address = cell.model.address;
self.deliveryPointID = [cell.model.deliveryPointID stringValue];

if (self.lastIndexPath.section == indexPath.section)
{
// 针对同一个section
TDDeleveryPointsModel *pModel = self.model.deliveryPoints[indexPath.section-1];
NSInteger rowCount = pModel.deliveryPointList.count;

for (NSInteger i = 0; i < rowCount; i++ )
{
if (indexPath.row != i)
{
TDDeleveryPointsListModel *listMode = pModel.deliveryPointList[i];
listMode.hasChecked = @0;
}
}
}
else{
// 针对的是不同的section
NSInteger sectionCount = self.model.deliveryPoints.count;
for (NSInteger i = 0; i < sectionCount; i++ )
{
NSInteger sectionIndex = indexPath.section-1;

if (sectionIndex != i)
{
NSLog(@"[i=]%ld---[indexPath]%@",(long)i,indexPath);
TDDeleveryPointsModel *pModel = self.model.deliveryPoints[i];
NSInteger rowCount = pModel.deliveryPointList.count;

for (NSInteger i = 0; i < rowCount; i++ )
{
TDDeleveryPointsListModel *listMode = pModel.deliveryPointList[i];
listMode.hasChecked = @0;
}

}
}
}

self.lastIndexPath = indexPath;
[self.tableView reloadData];
}

- (void)requestDeliveryPointsData
{
// 判断有没有网络,如果没有加载无网络视图
__weak typeof(self) weakself = self;
if (![TDMainNoNetworkHelper checkNetwork]) {
[MBProgressHUD hideHUD];
if (!self.noNetWorkView) {
self.noNetWorkView = [TDMainNoNetworkHelper addNoNetworkViewWithActionBlock:^{
if ([TDMainNoNetworkHelper checkNetwork]) {
[weakself requestDeliveryPointsData];
}
}];
self.noNetWorkView.frame=CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}
if (![self.view.subviews containsObject:self.noNetWorkView]) {
[self.view addSubview:self.noNetWorkView];
}
return;
}else{
[self.noNetWorkView removeFromSuperview];
}


NSString *goodsID = @"106";
if (self.goodsID.length >0) {
goodsID = self.goodsID;
}
[TDMarketDataUtil requestDeliveryPointGoodsID:goodsID
callback:^(NSError *error, id response)
{
[TDProgress dismissProgress];

TDResponse *value = [TDResponse mj_objectWithKeyValues:response];
if (value.rcode ==10000)
{
NSDictionary *dict = [response dictionaryForKey:@"data"];
self.model = [TDDeleveryPoints mj_objectWithKeyValues:dict];


// 替服务器处理一下默认值
if (self.model.deliveryPoints.count >0)
{
if (self.pointsID.length >0)
{
// 传入默认值
for (TDDeleveryPointsModel *pModel in self.model.deliveryPoints )
{
for (TDDeleveryPointsListModel *listMode in pModel.deliveryPointList )
{
if ([[listMode.deliveryPointID stringValue] isEqualToString:self.pointsID])
{
listMode.hasChecked = @1;
NSInteger row = [pModel.deliveryPointList indexOfObject:listMode];
NSInteger section = [self.model.deliveryPoints indexOfObject:pModel];
NSIndexPath *index = [NSIndexPath indexPathForRow:row inSection:section+1];

self.lastIndexPath = index;
self.address = listMode.address;
self.deliveryPointID = self.pointsID;
}
}
}
}else{
// 手动指定默认值
TDDeleveryPointsModel *pModel = self.model.deliveryPoints[0];
if (pModel.deliveryPointList.count >0)
{
TDDeleveryPointsListModel *listMode = pModel.deliveryPointList[0];
listMode.hasChecked = @1;
NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:1];
self.lastIndexPath = index;
self.address = listMode.address;
self.deliveryPointID = [listMode.deliveryPointID stringValue];
}
}

}

[self.tableView reloadData];
}else{
[TDProgress showMessage:value.rmsg];
}
}];

}

// 去掉UItableview的section的headerview黏性
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat sectionHeaderHeight;
if (isIPhone6p_6sp) {
sectionHeaderHeight = 66;
}else{
sectionHeaderHeight = 52;
}

if (scrollView == self.tableView) {

if (scrollView.contentOffset.y<=sectionHeaderHeight && scrollView.contentOffset.y>=0) {
scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
} else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
}
}
}

@end