Skip to content

Commit b411f27

Browse files
macmac
mac
authored and
mac
committed
修复了城市呈列显示布局
修复了城市呈列显示布局
1 parent 61a13ae commit b411f27

File tree

4 files changed

+46
-18
lines changed

4 files changed

+46
-18
lines changed

Diff for: YMCitySelect/YMCitySelect.m

+30-15
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ -(void)ym_setTableView{
151151
_ym_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_ym_searchBar.frame), self.view.ym_width, self.view.ym_height - CGRectGetMaxY(_ym_searchBar.frame))];
152152
_ym_tableView.delegate = self;
153153
_ym_tableView.dataSource = self;
154-
_ym_tableView.tintColor = [UIColor grayColor];
154+
_ym_tableView.tintColor = [UIColor blackColor];
155155
[_ym_tableView registerClass:[YMTableViewCell class] forCellReuseIdentifier:reuseIdentifier];
156156
[self.view addSubview:_ym_tableView];
157157
if([_ym_tableView respondsToSelector:@selector(setSectionIndexColor:)]) {
@@ -286,22 +286,39 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
286286
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
287287
{
288288
YMCityGroupsModel *model = _ym_ctiyGroups[section];
289-
if ([model.title isEqualToString:@"热门"]) {
290-
return @"中国热门城市";
291-
}
292-
if ([model.title isEqualToString:@"最近"]) {
293-
return @"最近选择过的城市";
294-
}
295-
if ([model.title isEqualToString:@"定位"]) {
296-
return @"定位的城市";
297-
}
298289
return model.title;
299290
}
300291

301292
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
302293
return [_ym_ctiyGroups valueForKeyPath:@"title"];
303294
}
304295

296+
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
297+
UIView *ym_view = [[UIView alloc] init];
298+
ym_view.backgroundColor = [UIColor colorWithRed:229/255.0 green:229/255.0 blue:229/255.0 alpha:1.0];
299+
UILabel *ym_label = [[UILabel alloc] init];
300+
ym_label.textAlignment = NSTextAlignmentLeft;
301+
ym_label.textColor = [UIColor blackColor];
302+
ym_label.font = [UIFont systemFontOfSize:16];
303+
YMCityGroupsModel *cityGroupModel = _ym_ctiyGroups[section];
304+
NSString *ym_title = cityGroupModel.title;
305+
if ([cityGroupModel.title isEqualToString:@"热门"]) {
306+
ym_title = @"中国热门城市";
307+
}
308+
if ([cityGroupModel.title isEqualToString:@"最近"]) {
309+
ym_title = @"最近选择过的城市";
310+
}
311+
if ([cityGroupModel.title isEqualToString:@"定位"]) {
312+
ym_title = @"定位的城市";
313+
}
314+
ym_label.text = ym_title;
315+
[ym_label sizeToFit];
316+
ym_label.ym_x = 10;
317+
ym_label.ym_y = 5;
318+
[ym_view addSubview:ym_label];
319+
return ym_view;
320+
}
321+
305322
#pragma mark - 选中cell
306323
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
307324
{
@@ -352,13 +369,11 @@ -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPat
352369
-(CGFloat)ym_setcellHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
353370
CGFloat ym_height = 44;
354371
YMCityGroupsModel *cityGroupModel = _ym_ctiyGroups[indexPath.section];
372+
CGFloat ym_w = ([UIScreen mainScreen].bounds.size.width - 72) / 3;
373+
CGFloat ym_h = ym_w / 3;
355374
if (cityGroupModel.title.length > 1) {
356375
NSInteger count = cityGroupModel.cities.count;
357-
if (count >3) {
358-
ym_height = (count / 3 + (count % 3 == 0?0:1)) * 44 + 30;
359-
}else{
360-
ym_height = (count / 3 + (count % 3 == 0?0:1)) * 44 + 15;
361-
}
376+
ym_height = (count / 3 + (count % 3 == 0?0:1)) * (ym_h + 15) + 15;
362377
}
363378
return ym_height;
364379
}

Diff for: YMCitySelect/YMTableViewCell.m

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSStri
3131
self.selectionStyle = UITableViewCellSelectionStyleBlue;
3232
self.backgroundColor =[UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1.0];
3333
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
34-
ym_w = (self.ym_width - 77) / 3;
34+
ym_w = ([UIScreen mainScreen].bounds.size.width - 72) / 3;
3535
CGFloat ym_h = ym_w / 3;
3636
layout.itemSize = CGSizeMake(ym_w, ym_h);
37-
layout.sectionInset = UIEdgeInsetsMake(0, 8, 0, 8);
37+
layout.sectionInset = UIEdgeInsetsMake(15, 15, 0, 0);
3838
layout.minimumLineSpacing = 15;
39-
_ym_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(5, 15, self.ym_width - 25, self.ym_height) collectionViewLayout:layout];
39+
_ym_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.ym_width - 27, self.ym_height) collectionViewLayout:layout];
4040
[_ym_collectionView registerClass:[YMCollectionViewCell
4141
class] forCellWithReuseIdentifier:identifier];
4242
_ym_collectionView.delegate = self;

Diff for: YMCitySelectExample/YMViewController.m

+13
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,25 @@ - (void)viewDidLoad {
3434
_cityLabel.text = @"北京";
3535
_cityLabel.textAlignment = NSTextAlignmentCenter;
3636
[self.view addSubview:_cityLabel];
37+
38+
UIButton *clearBtn = [[UIButton alloc] init];
39+
[clearBtn setTitle:@"清理缓存" forState:UIControlStateNormal];
40+
[clearBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
41+
[clearBtn sizeToFit];
42+
clearBtn.center = self.view.center;
43+
[self.view addSubview:clearBtn];
44+
[clearBtn addTarget:self action:@selector(clearBtnClick) forControlEvents:UIControlEventTouchUpInside];
3745
}
3846

3947
-(void)cityBtnClick:(UIButton *)btn{
4048
[self presentViewController:[[YMCitySelect alloc] initWithDelegate:self] animated:YES completion:nil];
4149
}
4250

51+
-(void)clearBtnClick{
52+
NSUserDefaults *clear = [NSUserDefaults standardUserDefaults];
53+
[clear removeObjectForKey:@"ym_cityNames"];
54+
}
55+
4356
- (void)didReceiveMemoryWarning {
4457
[super didReceiveMemoryWarning];
4558
// Dispose of any resources that can be recreated.

0 commit comments

Comments
 (0)