界面效果:
因?yàn)楸救瞬](méi)有分離這個(gè)界面,所以暫時(shí)沒(méi)辦法將demo公布出來(lái),只能將將重點(diǎn)的代碼貼出來(lái)
- 首先是自定義熱門城市這種樣式的Cell紧阔,因?yàn)槲矣玫氖莔odel,所以請(qǐng)大家看到明白
-(void)setLocationFirstViewCellWithModel:(NSArray<LocationModel *> *)modeles {
self.backgroundColor = COLOR(240, 240, 240, 1);
self.contentView.backgroundColor = [UIColor clearColor];
self.dataSource = [NSMutableArray arrayWithArray:modeles];
CGFloat screenWigth = [UIScreen mainScreen].bounds.size.width;
//首先,每一行要有3個(gè)Button,最左邊和最右邊的間隔都為15,中間的間隔為15,可以算出每個(gè)Button個(gè)寬度
CGFloat buttonWigth = (screenWigth - (15+ 30 ) - 15*2) / 3.0;
//臨時(shí)變量 - 確定循環(huán)次數(shù)
NSInteger temp_i = 0;
//臨時(shí)變量 - 確定當(dāng)前列
NSInteger temp_k = 0;
//臨時(shí)變量 - 確定共有多少列
NSInteger temp_count_k = ceil([modeles count] / 3.0) - 1;
self.contentView.userInteractionEnabled = YES;
//根據(jù)得到的數(shù)據(jù)確定有多少個(gè)button
for (LocationModel *model in modeles) {
UIButton *button = [self viewWithTag:100 + temp_i];
//創(chuàng)建button
if (![self.contentView viewWithTag:(100 + temp_i ) ]) {
button = [UIButton buttonWithType:UIButtonTypeSystem];
button.layer.cornerRadius = 5;
[button setTitleColor:COLOR(100, 100, 100, 1) forState:UIControlStateNormal];
button.backgroundColor = [UIColor whiteColor];
button.layer.borderColor = [COLOR(220, 220, 220, 1) CGColor];
button.layer.borderWidth = 0.5;
button.tag = temp_i +100;
[button addTarget:self action:@selector(clickLocationButton:) forControlEvents:UIControlEventTouchUpInside];
self.contentView.userInteractionEnabled = YES;
[self addSubview:button];
//button到父視圖Left的距離
CGFloat buttonToViewLeft = 15 + temp_i%3 *(buttonWigth + 15);
//button到父視圖Top的距離
CGFloat buttonToViewTop = 7 + temp_k *(40 + 7);
button.frame = ({
CGRect frame = CGRectMake(buttonToViewLeft,buttonToViewTop, buttonWigth, 40);
frame;
});
}
temp_i = temp_i+1; temp_k = floor(temp_i/ 3.0);
[button setTitle:model.addressName forState:UIControlStateNormal];
}
//判斷是否最后一個(gè)button
if (temp_k == temp_count_k) {
CGRect frame = [self viewWithTag:100 + temp_i - 1].frame;
CGFloat height = CGRectGetMaxY(frame);
[self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
//固定contentView的高度高度
make.height.equalTo(@(height + 7));
}];
}
}
因?yàn)槲矣玫?strong>FDTemplateLayoutCell ,在這里我的cell只要在內(nèi)部將contenview撐起來(lái)就可以實(shí)現(xiàn)自適應(yīng)高度了
- 2.關(guān)于如何實(shí)現(xiàn)排序功能,我在這里使用的是第三方ZYPinYinSearch和ChineseString其他的我沒(méi)用過(guò),反正這2個(gè)用起來(lái)挺方便的
使用ZYPinYinSearch的時(shí)候 要注意必須使用模型來(lái)保存這些地址 地址里必須設(shè)置紅色圈中的標(biāo)志
模型要全部保存到一個(gè)數(shù)組里,就可以進(jìn)行排序了
//獲取索引的首字母
self.sectionArr = [ChineseString IndexArray:self.dataCahce];
//對(duì)原數(shù)據(jù)進(jìn)行排序重新分組
self.dataSource = [ChineseString LetterSortArray:self.dataCahce];//datacache全部保存的是自定義模型
因?yàn)槲疫@里是固定了熱門城市和當(dāng)前位置,故我在索引數(shù)組前增加了2個(gè)標(biāo)記
[self.sectionArr insertObject:@"#" atIndex:0];
[self.sectionArr insertObject:@"*" atIndex:0];
- 3.關(guān)于如何實(shí)現(xiàn)searchBar,這步其實(shí)非常簡(jiǎn)單,在ZYPinYinSearch里有個(gè)代理方法
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
if (controller.searchBar.text.length != 0) {
dispatch_queue_t queue = dispatch_queue_create("com.mmfishing.locationcontroller.searchbar", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
[self.dataSearch removeAllObjects];
NSArray *ary = [NSArray new];
ary = [ZYPinYinSearch searchWithOriginalArray:self.dataCahce
andSearchText:controller.searchBar.text
andSearchByPropertyName:@"addressName"];
[self.dataSearch addObjectsFromArray:ary];
dispatch_async(dispatch_get_main_queue(), ^{
[controller.searchResultsTableView reloadData];
for (UIView* v in self.displayController.searchResultsTableView.subviews) {
if ([v isKindOfClass: [UILabel class]] &&
[[(UILabel*)v text] isEqualToString:@"No Results"]) {
UILabel *label = (UILabel *)v;
label.text = @"沒(méi)有結(jié)果";
break;
}
}
});
});
}
return NO;
}
在這里 我使用異步是因?yàn)檫@庫(kù)在搜索時(shí)會(huì)照成頁(yè)面卡頓,加上后的確是不這么卡了