iOS_21团购_拼音搜索

iOS_21团购_拼音搜索,第1张

概述最终效果图: 关键代码: 搜索结果控制器: //// SearchResultController.m// 帅哥_团购//// Created by beyond on 14-8-15.// Copyright (c) 2014年 com.beyond. All rights reserved.// 当搜索框searchBar里面的文字change的时候,会创建本控制器,展示

最终效果图:



关键代码:

搜索结果控制器:

////  SearchResultController.m//  帅哥_团购////  Created by beyond on 14-8-15.//  copyright (c) 2014年 com.beyond. All rights reserved.//  当搜索框searchbar里面的文字change的时候,会创建本控制器,展示搜索结果列表,本控制器只有唯一一个成员变量,那就是从CityLocationController控制器的searchbar textDIDChange:方法中传递过来的搜索文本#import "SearchResultController.h"#import "PinYin4Objc.h"#import "MetaDataTool.h"#import "City.h"@interface SearchResultController (){    // 数组,放着所有符合搜索条件的 城市对象    NSMutableArray *_resultCityArr;}@end@implementation SearchResultController- (voID)vIEwDIDLoad{    [super vIEwDIDLoad];    self.vIEw.backgroundcolor = [UIcolor whitecolor];    // 数组初始化,放着所有符合搜索条件的 城市对象    _resultCityArr = [NSMutableArray array];}// 唯一一个成员,从CityLocationController控制器的searchbar textDIDChange:方法中传递过来的搜索文本-(voID)setSearchText:(Nsstring *)searchText{    _searchText = searchText;        // 1.清除数组之前存放的搜索结果    [_resultCityArr removeAllObjects];        // 2.筛选城市    // 拼音输出格式    HanyuPinyinOutputFormat *fmt = [[HanyuPinyinOutputFormat alloc] init];    // 全大写    fmt.caseType = CaseTypeUppercase;    // 无音调    fmt.toneType = ToneTypeWithoutTone;    // V字处理方式    fmt.vCharType = VCharTypeWithUUnicode;    // 下面是三种条件,关键代码!    NSDictionary *citIEsDict = [MetaDataTool sharedMetaDataTool].allCitIEsDict;    [citIEsDict enumerateKeysAndobjectsUsingBlock:^(Nsstring *key,City *city,BOol *stop) {        // SHI#JIA#ZHUANG        // 1.拼音字符串,用#连接一个个拼音        Nsstring *pinyinStr = [PinyinHelper toHanyuPinyinStringWithNsstring:city.name withHanyuPinyinOutputFormat:fmt withNsstring:@"#"];                // 2.拼音首字母        NSArray *wordsArr = [pinyinStr componentsSeparatedByString:@"#"];        // 用于所有首字母拼接 如BJ        NSMutableString *pinyinInitial = [NSMutableString string];        for (Nsstring *word in wordsArr) {            [pinyinInitial appendString:[word substringToIndex:1]];        }                // 去掉所有的#  如beijing        pinyinStr = [pinyinStr stringByReplacingOccurrencesOfString:@"#" withString:@""];                // 3.城市名 city.name 中包含了搜索条件 如北京        // 拼音 pinyinStr 中包含了搜索条件  如BEIJING        // 拼音首字母 pinyinInitial 中包含了搜索条件 如BJ        if (([city.name rangeOfString:searchText].length != 0) ||            ([pinyinStr rangeOfString:searchText.uppercaseString].length != 0)||            ([pinyinInitial rangeOfString:searchText.uppercaseString].length != 0))        {            // 来到这,说明城市名中包含了搜索条件,添加到成员变量(对象数组),以便为tableVIEw提供数据源            [_resultCityArr addobject:city];        }    }];        // 3.刷新表格    [self.tableVIEw reloadData];    }#pragma mark - 数据源方法// 符合搜索条件的结果 有多少行- (NSInteger)tableVIEw:(UItableVIEw *)tableVIEw numberOfRowsInSection:(NSInteger)section{    return _resultCityArr.count;}// 每一行的独一无二内容- (UItableVIEwCell *)tableVIEw:(UItableVIEw *)tableVIEw cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static Nsstring *CellIDentifIEr = @"SearchResultCell";    UItableVIEwCell *cell = [tableVIEw dequeueReusableCellWithIDentifIEr:CellIDentifIEr];        if (cell == nil) {        cell = [[UItableVIEwCell alloc] initWithStyle:UItableVIEwCellStyleDefault reuseIDentifIEr:CellIDentifIEr];    }    // 取出本行对应的 城市对象    City *city = _resultCityArr[indexPath.row];    cell.textLabel.text = city.name;        return cell;}// 友好提示,共有多少个符合条件的结果- (Nsstring *)tableVIEw:(UItableVIEw *)tableVIEw TitleForheaderInSection:(NSInteger)section{    return [Nsstring stringWithFormat:@"共%d个搜索结果",_resultCityArr.count];}#pragma mark - 代理方法// 选中了符合搜索条件的所有搜索结果中的某一行- (voID)tableVIEw:(UItableVIEw *)tableVIEw dIDSelectRowAtIndexPath:(NSIndexPath *)indexPath{    // 取出本行对应的 城市对象    City *city = _resultCityArr[indexPath.row];    // 设置工具类的当前城市,其内部会拦截setter *** 作,更新最近访问的城市数组    [MetaDataTool sharedMetaDataTool].currentCity = city;}@end

点击 dock下面的倒数第2个定位按钮,

d出控制器CityLocationController

////  CityLocationController.m//  帅哥_团购////  Created by beyond on 14-8-14.//  copyright (c) 2014年 com.beyond. All rights reserved.//  点击dock下面的倒数第2个定位按钮,d出的用Popover包装的城市选择控制器,其上面是一个搜索框,下面是一个tableVIEw(按城市的拼音分的组),当搜索框文字改变时候,(懒加载)创建一个搜索结果控制器,并且显示搜索界面在遮罩上面#import "CityLocationController.h"// 蒙板#import "CoverOntableVIEw.h"// 元数据工具#import "MetaDataTool.h"// 数据模型---分组#import "Section.h"// 数据模型---城市#import "City.h"// 数据模型---行政区#import "district.h"//  当搜索框searchbar里面的文字change的时候,展示搜索结果列表#import "SearchResultController.h"// 上面的searchbar高度#define kSearchbarH 44@interface CityLocationController ()<UItableVIEwDataSource,UItableVIEwDelegate,UISearchbarDelegate>{    // 从pList中加载的数组,共23个成员,每个成员是个字典,每个字典有两对KV,一对是name-->A,另一对是citIEs--->数组(数组中的成员是字典,一个字典对应一个城市,该字典又有三对KV,分别是:name--->北京,hot---->1,districts--->数组(该数组对应的又是一个字典,代表一个区,字典中有两对KV,分别是:name--->朝阳区,neighbours--->数组(该数组的成员是string.....)))    NSMutableArray *_sections; // 所有的城市组信息    // vIEw上方是UISearchbar,下方是UItableVIEw    UISearchbar *_searchbar;    UItableVIEw *_tableVIEw;    // UItableVIEw上面有一层蒙板,遮盖    CoverOntableVIEw *_cover;    // 搜索结果控制器    SearchResultController *_searchResultCtrl;}@end@implementation CityLocationController- (voID)vIEwDIDLoad{    [super vIEwDIDLoad];        // 1.添加上方的搜索框UISearchbar    [self addSearchbar];        // 2.添加下方的tableVIEw    [self addtableVIEw];        // 3.使用工具类,加载城市数组数据    [self loadCitIEsMetaData];    }// 1.添加搜索框UISearchbar- (voID)addSearchbar{    _searchbar = [[UISearchbar alloc] init];    _searchbar.autoresizingMask = UIVIEwautoresizingFlexibleWIDth;    _searchbar.frame = CGRectMake(0,self.vIEw.frame.size.wIDth,kSearchbarH);    // 监听searchbar的获得焦点,失去焦点,字符变化等事件    _searchbar.delegate = self;    _searchbar.placeholder = @"请输入城市名或拼音";    //    search.tintcolor 渐变色    //    search.barStyle 样式    [self.vIEw addSubvIEw:_searchbar];}// 2.添加下方的tableVIEw- (voID)addtableVIEw{    _tableVIEw = [[UItableVIEw alloc] init];    CGfloat tableVIEwH = self.vIEw.frame.size.height - kSearchbarH;    _tableVIEw.frame = CGRectMake(0,kSearchbarH,tableVIEwH);    _tableVIEw.dataSource = self;    _tableVIEw.delegate = self;    // 重要~因为本控制器是在Popover控制器里面,Popover又设置了内容SIZE只有320,480    _tableVIEw.autoresizingMask = UIVIEwautoresizingFlexibleWIDth | UIVIEwautoresizingFlexibleHeight;    [self.vIEw addSubvIEw:_tableVIEw];}// 3.使用工具类,加载城市数组数据- (voID)loadCitIEsMetaData{    // 从pList中加载的数组,neighbours--->数组(该数组的成员是string.....)))    _sections = [NSMutableArray array];    NSArray *sections = [MetaDataTool sharedMetaDataTool].allSectionsArr;    // 将工具类返回的section数组赋值给成员变量,以供tableVIEw的数据源使用    [_sections addobjectsFromArray:sections];}#pragma mark - 数据源方法#pragma mark - 数据源方法// 共有多少分组(23个字母组)- (NSInteger)numberOfSectionsIntableVIEw:(UItableVIEw *)tableVIEw{    return _sections.count;}// 每一组有多少行(多少个城市就有多少行)- (NSInteger)tableVIEw:(UItableVIEw *)tableVIEw numberOfRowsInSection:(NSInteger)section{    // 第几组    Section *s = _sections[section];    // 第几组的城市数组的个数    return s.citIEs.count;}// 每一行的cell独一无二的内容- (UItableVIEwCell *)tableVIEw:(UItableVIEw *)tableVIEw cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static Nsstring *CellIDentifIEr = @"CityListCell";    UItableVIEwCell *cell = [tableVIEw dequeueReusableCellWithIDentifIEr:CellIDentifIEr];        if (cell == nil) {        cell = [[UItableVIEwCell alloc] initWithStyle:UItableVIEwCellStyleDefault reuseIDentifIEr:CellIDentifIEr];    }    // 第几组    Section *s = _sections[indexPath.section];    // 第几行(城市)    City *city = s.citIEs[indexPath.row];    // 城市名    cell.textLabel.text = city.name;    return cell;}// 每一组的headerTitle- (Nsstring *)tableVIEw:(UItableVIEw *)tableVIEw TitleForheaderInSection:(NSInteger)section{    // 第几组    Section *s = _sections[section];    // 组名,如ABCD    return s.name;}// 表格右侧的分组索引标题- (NSArray *)sectionIndexTitlesFortableVIEw:(UItableVIEw *)tableVIEw{    // 重要~取出_sections数组中每一组的键为name的值(如ABCD...),并且将这些值全放到一个新的数组中,返回的这个新数组,就是分组索引的标题    return [_sections valueForKeyPath:@"name"];}// 点击某一行时,设置当前城市- (voID)tableVIEw:(UItableVIEw *)tableVIEw dIDSelectRowAtIndexPath:(NSIndexPath *)indexPath{    // 第几组    Section *s = _sections[indexPath.section];    // 哪一个城市    City *city = s.citIEs[indexPath.row];    // 设置工具类的当前城市,更新最近访问的城市数组    [MetaDataTool sharedMetaDataTool].currentCity = city;}#pragma mark - 搜索框代理方法// 搜索框开始编辑(开始聚焦,取得焦点)- (voID)searchbarTextDIDBeginEditing:(UISearchbar *)searchbar{    // 1.动画效果,显示其右边的 取消按钮    [searchbar setShowsCancelbutton:YES animated:YES];        // 2.动画显示遮盖(蒙板),并在内部绑定了一个,tap手势监听器    if (_cover == nil) {        _cover = [CoverOntableVIEw coverWithTarget:self action:@selector(coverClick)];    }    // 3.必须让cover完全覆盖在tableVIEw上面    _cover.frame = _tableVIEw.frame;    [self.vIEw addSubvIEw:_cover];    // 4.开始全透明(看不见)    _cover.Alpha = 0.0;    [UIVIEw animateWithDuration:0.3 animations:^{        // 让cover变成黑色        [_cover Alphareset];    }];}// 监听 遮盖 被tap点击,移除遮盖,隐藏取消按钮,退出键盘- (voID)coverClick{    // 1.动画完成后,移除遮盖    [UIVIEw animateWithDuration:0.3 animations:^{        _cover.Alpha = 0.0;    } completion:^(BOol finished) {        [_cover removeFromSupervIEw];    }];    // 2.隐藏_searchbar最右边的取消按钮    [_searchbar setShowsCancelbutton:NO animated:YES];        // 3.让_searchbar取消第一响应者,即退出键盘    [_searchbar resignFirstResponder];    // [self.vIEw endEditing:YES];}// 当点击了 搜索框的键盘上面取消键时(即_searchbar失去了焦点)- (voID)searchbarTextDIDEndEditing:(UISearchbar *)searchbar{    [self coverClick];}// 当点击了 搜索框的右边的取消按钮时- (voID)searchbarCancelbuttonClicked:(UISearchbar *)searchbar{    [self coverClick];}#pragma mark 监听搜索框的文字改变- (voID)searchbar:(UISearchbar *)searchbar textDIDChange:(Nsstring *)searchText{    // 如果搜索框里面没有文字,隐藏搜索结果控制器的vIEw    if (searchText.length == 0) {        [_searchResultCtrl.vIEw removeFromSupervIEw];    } else {        // 懒加载,创建并显示搜索界面,frame与遮罩相同        if (_searchResultCtrl == nil) {            _searchResultCtrl = [[SearchResultController alloc] init];            _searchResultCtrl.vIEw.frame = _cover.frame;            _searchResultCtrl.vIEw.autoresizingMask = _cover.autoresizingMask;            // 如果另一个控制器的vIEw要展示在本控制器的vIEw里,官方建议是让另一个控制器成为本控制器的子控制器            [self addChildVIEwController:_searchResultCtrl];        }        // 传递搜索框里面的文本给 搜索结果控制器        _searchResultCtrl.searchText = searchText;        [self.vIEw addSubvIEw:_searchResultCtrl.vIEw];    }}@end

Dock下方的Location按钮

////  DockItemLocation.m//  帅哥_团购////  Created by beyond on 14-8-13.//  copyright (c) 2014年 com.beyond. All rights reserved.//  dock下方倒数第2个按钮【定位】,继承自DockItem,点击本按钮,负责创建一个封装了控制器CityLocationController的Popover控制器,本按钮同时还负责监听屏幕的横竖屏切换通知,同时还负责监听CityLocationController控制器里面的某一行被选中时,发出的CityDIDChanged通知,目的是能够让本按钮掌控Popover控制器的位置,以及其出现和隐藏#import "DockItemLocation.h"// 点击dock上面的locationBtn,d出的Popover封装的控制器,其上方是搜索栏,下方是tableVIEw#import "CityLocationController.h"// 工具类中currentCity获取当前城市#import "MetaDataTool.h"#import "City.h"// 按钮上面是图片,下面是文字,这是图片在高度上的比例#define kImageHeightRatioInBtn 0.5@interface DockItemLocation()<UIPopoverControllerDelegate>{    //popover控制器,创建出来之后,show方法显示,因此不可以是局部变量,必须用成员变量记住,否则方法btnClick调用完毕就销毁了,还如何 显示捏?    UIPopoverController *_popoverCtrl;}@end@implementation DockItemLocation- (ID)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        // 1.调用父类的方法,设置内部的图片        [self setIcon:@"ic_district.png" selectedIcon:@"ic_district_hl.png"];                // 2.自动伸缩        self.autoresizingMask = UIVIEwautoresizingFlexibletopmargin;                // 3.设置默认的文字        [self setTitle:@"定位中" forState:UIControlStatenormal];        self.TitleLabel.Font = [UIFont systemFontOfSize:16];        self.TitleLabel.textAlignment = NSTextAlignmentCenter;        [self setTitlecolor:[UIcolor whitecolor] forState:UIControlStateDisabled];        [self setTitlecolor:[UIcolor graycolor] forState:UIControlStatenormal];                // 4.设置图片属性        self.imageVIEw.contentMode = UIVIEwContentModeCenter;                // 5.监听点击【Location定位】,d出一个Popover控制器        [self addTarget:self action:@selector(locationBtnOnDockClicked) forControlEvents:UIControlEventtouchDown];        // 6.添加监听城市改变的通知,当接收到其他其他东东(如工具类里面的setterCurrentCity方法中)发出的kCityChangeNote通知,就会调用下面的方法        [[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(cityDIDChanged) name:kCityChangeNote object:nil];    }    return self;}// 6.添加监听城市改变的通知,就会调用下面的方法- (voID)cityDIDChanged{    // 0.先从工具类中,获取当前选中城市    City *city = [MetaDataTool sharedMetaDataTool].currentCity;        // 1.更改本按钮 显示的城市名称    [self setTitle:city.name forState:UIControlStatenormal];        // 2.关闭popover(代码关闭popover不会触发代理方法)    [_popoverCtrl dismisspopoverAnimated:YES];        // 3.更改本按钮 变为enable    self.enabled = YES;        // 4.设置图标    [self setIcon:@"ic_district.png" selectedIcon:@"ic_district_hl.png"];}// 5.监听点击【Location定位】,d出一个Popover控制器- (voID)locationBtnOnDockClicked{    // 禁用,只可点击一次    self.enabled = NO;    // 点击dock上面的locationBtn,下方是tableVIEw    CityLocationController *cityVC = [[CityLocationController alloc] init];        // 唯一一个不是继承自UIVIEwController的控制器,它继承自NSObject    //popover控制器,还如何 显示捏?    _popoverCtrl = [[UIPopoverController alloc] initWithContentVIEwController:cityVC];    // 设置这个Popover控制器的显示的大小    _popoverCtrl.popoverContentSize = CGSizeMake(320,480);    // 代理,监听Popover控制器的XX事件    _popoverCtrl.delegate = self;    // 因为其他方法也要显示,_popoverCtrl,所以抽取成自定义方法    [self showPopoverCtrl];        // 因为屏幕旋转时,d出的popover的指向的位置就不对了,所以有必要注册监听屏幕旋转的通知    // 先移除监听器,保证健壮性    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrIEntationDIDChangeNotification object:nil];    // 再添加一个监听器,一旦设备出现UIDeviceOrIEntationDIDChangeNotification,就会调用observer的selector方法    [[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(screenDIDRotated) name:UIDeviceOrIEntationDIDChangeNotification object:nil];}// 5-1,因为侦听到屏幕旋转了,也要再次显示_popoverCtrl,所以抽取成自定义方法- (voID)showPopoverCtrl{    // 显示到哪里? 如果目标vIEw是self自己,则rect使用bounds,因为bounds的原点才是相对于自己    // 如果目标vIEw是self.superVIEw,则rect使用frame,因为frame的原点才是相对于父控件    [_popoverCtrl presentPopoverFromrect:self.bounds inVIEw:self permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];}// 5-2,再添加一个监听器,就会调用observer的selector方法- (voID)screenDIDRotated{    if (_popoverCtrl.popoverVisible) {        // 1.    关闭之前位置上面的_popoverCtrl        [_popoverCtrl dismisspopoverAnimated:NO];                // 2.    0.5秒后创建新的位置上的_popoverCtrl        [self performSelector:@selector(showPopoverCtrl) withObject:nil afterDelay:0.5];    }}#pragma mark - popOver代理方法- (voID)popoverControllerDIDdismisspopover:(UIPopoverController *)popoverController{    // 消失前,让定位按钮恢复可以点击状态    self.enabled = YES;        // 消失前,即popover被销毁的时候,移除注册的监听器(通知)    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrIEntationDIDChangeNotification object:nil];}#pragma mark - 销毁时,移除当前对控制器对屏幕的监听,防止野指针- (voID)dealloc{    [[NSNotificationCenter defaultCenter] removeObserver:self];}#pragma mark - 覆写调整图片和文字在按钮中的Frame- (CGRect)imageRectForContentRect:(CGRect)contentRect{    CGfloat btnW = contentRect.size.wIDth;    CGfloat imgH = contentRect.size.height * kImageHeightRatioInBtn;    return CGRectMake(0,btnW,imgH);}- (CGRect)TitleRectForContentRect:(CGRect)contentRect{    CGfloat btnW = contentRect.size.wIDth;    CGfloat textH = contentRect.size.height * (1 - kImageHeightRatioInBtn);    // 文字在下面,图片在上面    CGfloat textY = contentRect.size.height - textH;    return CGRectMake(0,textY,textH);}#pragma mark - Json转成PList- (voID)Json2pList{    // Json文件读取成为数组    Nsstring *filePath = @"/Users/beyond/Desktop/citIEs.Json";    NSData *data = [NSData dataWithContentsOffile:filePath];    // options:    // NSJsONReadingMutableContainers  返回可变容器,NSMutableDictionary或NSMutableArray    // NSJsONReadingallowFragments:允许JsON字符串最外层既不是NSArray也不是NSDictionary,但必须是有效的JsON Fragment。例如使用这个选项可以解析 @“123” 这样的字符串。    // NSJsONReadingMutableLeaves:返回的JsON对象中字符串的值为NSMutableString    NSArray *arr = [NSJsONSerialization JsONObjectWithData:data options:NSJsONReadingMutableContainers error:nil];    // 数组写到文件后,就是pList    [arr writetofile:@"/Users/beyond/Desktop/citIEs.pList" atomically:YES];}@end





用到的工具类

@interface MetaDataTool : NSObjectsingleton_interface(MetaDataTool)// Readonly只可读,NSArray,不允许外部随便增删改// 所有的城市分组数组,数组中的元素是section对象@property (nonatomic,strong,Readonly) NSArray *allSectionsArr;// 所有城市字典,Key是城市名,Value是城市对象@property (nonatomic,Readonly) NSMutableDictionary *allCitIEsDict;// 当前选中的城市,当点击了控制器下方的tableVIEw的某一行时,会设置当前城市,拦截setter *** 作,更新最近访问的城市数组@property (nonatomic,strong) City *currentCity; // 当前选中的城市@end////  MetaDataTool.m//  帅哥_团购////  Created by beyond on 14-8-14.//  copyright (c) 2014年 com.beyond. All rights reserved.//  元数据管理类// 1.城市数据// 2.下属分区数据// 3.分类数据#import "MetaDataTool.h"// 一个分组模型#import "Section.h"#import "City.h"//#import "TGcategory.h"//#import "TGOrder.h"// 沙盒里面放的是所有曾经访问过的城市名字#define kfilePath [NSSearchPathForDirectorIEsInDomains(NSdocumentDirectory,NSUserDomainMask,YES)[0] stringByAppendingPathComponent:@"visitedCitynamesArr.data"]@interface MetaDataTool (){    // 数组,存储曾经访问过城市的名称    NSMutableArray *_visitedCitynamesArr;    // 访问过的组section    Section *_visitedSection; // 最近访问的城市组数组}@end@implementation MetaDataToolsingleton_implementation(MetaDataTool)- (ID)init{    if (self = [super init]) {        // 初始化项目中的所有元数据                // 1.初始化城市数据        [self loadCitIEsData];            }    return self;}// 1.初始化城市数据- (voID)loadCitIEsData{    // 所有城市对象组成的字典,Value是城市对象    _allCitIEsDict = [NSMutableDictionary dictionary];    // 临时变量,存放所有的section    NSMutableArray *tempSectionsArr = [NSMutableArray array];        // 1.创建一个热门城市分组    Section *hotSection = [[Section alloc] init];    // 组名是 热门    hotSection.name = @"热门";    // 分组的成员citIEs,初始化    hotSection.citIEs = [NSMutableArray array];    // 为了将热门这一组加在分组数组的最前面,准备了一个临时的section数组    [tempSectionsArr addobject:hotSection];        // 2.添加A-Z分组,到临时section数组后面    // 加载pList数据    NSArray *sectionsArr = [NSArray arrayWithContentsOffile:[[NSBundle mainBundle] pathForResource:@"CitIEs.pList" ofType:nil]];    for (NSDictionary *dict in sectionsArr) {        // 创建城市分组对象模型        Section *section = [[Section alloc] init];        // 调用分类方法,将字典转成模型        [section setValuesWithDict:dict];        // 将所有的section对象,加到临时的section对象数组的后面        [tempSectionsArr addobject:section];                // 遍历每一组的所有城市,找出热门的加到hotSection里面        for (City *city in section.citIEs) {            if (city.hot) {                // 如果是热门城市                [hotSection.citIEs addobject:city];            }            // 并且将所有的城市对象,以城市名作为键,存入字典中            [_allCitIEsDict setobject:city forKey:city.name];        }    }        // 3.从沙盒中读取之前访问过的城市名称    _visitedCitynamesArr = [NSKeyedUnarchiver unarchiveObjectWithfile:kfilePath];    // 如果是首次使用,则沙盒中返回的是空数组,需要懒加载,创建一个数组    if (_visitedCitynamesArr == nil) {        _visitedCitynamesArr = [NSMutableArray array];    }        // 4.创建并添加一个section,最近访问城市组(section)    _visitedSection = [[Section alloc] init];    _visitedSection.name = @"最近访问";    _visitedSection.citIEs = [NSMutableArray array];        // 5.遍历沙盒中取出来的城市名组成的数组,转成一个个城市对象    for (Nsstring *name in _visitedCitynamesArr) {        // 根据城市名,从对象字典中取出城市对象,并添加到最近访问城市组(section)中的城市对象数组        City *city = _allCitIEsDict[name];        [_visitedSection.citIEs addobject:city];    }    // 6.如果最近访问城市组(section)中的城市对象数组中有城市,那么就可以将最近访问组插入到sections最前面    if (_visitedSection.citIEs.count) {        [tempSectionsArr insertObject:_visitedSection atIndex:0];    }        // 将所有的section组成的数组赋值给成员变量供调用者访问    _allSectionsArr = tempSectionsArr;}// 当点击了控制器下方的tableVIEw的某一行时,更新最近访问的城市数组,发出CityDIDChanged通知给Dock上的定位按钮,让它隐藏popoverCtroller- (voID)setCurrentCity:(City *)currentCity{    _currentCity = currentCity;    // 1.先从最近访问的城市名数组中,移除该的城市名    [_visitedCitynamesArr removeObject:currentCity.name];    // 2.再将新的城市名插入到数组的最前面(最近访问的在最前)    [_visitedCitynamesArr insertObject:currentCity.name atIndex:0];    // 3.同时,要将新的城市对象,放到_visitedSection的城市对象数组的最前面    [_visitedSection.citIEs removeObject:currentCity];    [_visitedSection.citIEs insertObject:currentCity atIndex:0];    // 4.归档最近访问的城市名组成的数组,以便下次再解档    [NSKeyedArchiver archiveRootObject:_visitedCitynamesArr tofile:kfilePath];    // 5.每一次点击,拦截setter当前城市之后,都要发出通知,做什么用???    [[NSNotificationCenter defaultCenter] postNotificationname:kCityChangeNote object:nil];}@end
总结

以上是内存溢出为你收集整理的iOS_21团购_拼音搜索全部内容,希望文章能够帮你解决iOS_21团购_拼音搜索所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1088692.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存