1.目前首页界面的功能都实现了,但是在 标题的北京那个按钮的搜索功能没有实现;
2.下面3张图 是功能流程图。
反向传值实现的功能。
思路步骤: 1.图1功能已经实现。
2.点击北京按钮 (按钮需要在自己的.m里面关联,里面不用些代码)跳转到一个新的CityListTableViewController页面,同事按住按钮 与页面关联(segue)跳转 下面是跳转方法;
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"category"]) {
CategoryDetialTableViewController * ctvc = segue.destinationViewController;
ctvc.arr = sender;
//城市列表页面跳转
}else if([segue.identifier isEqualToString:@"cityList"]){
CityListTableViewController *cityVc = segue.destinati
onViewController;
cityVc.delegate = self;
}
<<CityListTableViewController.m>>
城市列表页面
3.跳转完成后, 就需要在城市列表页面中对
数据进行展示了, 同事需要拖出一个搜索控件, 关联自己的delegate到当前tableviewController
#import "CityListTableViewController.h"
#import "HomeTableViewController.h"
@interface CityListTableViewController ()<UISearchDisplayDelegate,UISearchBarDelegate>//第一个协议是实现搜索框数据改变的方法在里面,第二个是取消按钮的相关协议,实现方法在里面
@property (nonatomic,strong)NSMutableArray *citys;
@end
@implementationCityListTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle]pathForResource:@"citys" ofType:@"plist"];
//添加数据源数组;
self.citys = [NSMutableArray arrayWithContentsOfFile:path];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.citys.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
if (tableView.tag == 1) {
UITableViewCell *cell= [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
cell.textLabel.text = self.citys[indexPath.row];
return cell;
}else{
UITableViewCell *cell= [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
cell.textLabel.text = self.citys[indexPath.row];
cell.detailTextLabel.text = @"搜索数据";
return cell;
}
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString*)searchString{
NSMutableArray *newCitys= [NSMutableArray array];
for (NSString *city in self.citys) {
if ([city rangeOfString:searchString].length>0) {
[newCitys addObject:city];
}
}
//在遍历完了之后在返回给数据源数组;
self.citys = newCitys;
//还有一个情况就是空的情况
if (!searchString||[searchString isEqualToString:@""]) {
NSString *path = [[NSBundle mainBundle]pathForResource:@"citys" ofType:@"plist"];
NSMutableArray *arr =[NSMutableArray arrayWithContentsOfFile:path];
self.citys = arr;
}
//刷新数据源
[self.tableView reloadData];
return YES;
}
- (void)searchBarCancelButtonClicked:(UISearchBar*)searchBar{
//如果点取消,应该是数据源数组重置一下;
//数据源重新加载
NSString *path = [[NSBundle mainBundle]pathForResource:@"citys" ofType:@"plist"];
self.citys = [NSMutableArray arrayWithContentsOfFile:path];
[self.tableView reloadData];
}
4.以上代码是搜索列表的所有功能的实现。 现在的问题就剩: 当我点击城市列表的对应的Cell时候,将对应的数值反向传值到首页界面,并
将按钮的显示信息更换, 同时更换首页的数据源显示信息: (也就是改变首页的 解析字典的判定,)
5.跳转方法里面实现的内容
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
HomeTableViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"HomeTableViewController"];
NSString *cityNames = self.citys[indexPath.row];
[vc backCityName:cityNames];
[self.navigationController pushViewController:vc animated:YES];
} 跳转方法实现内容的解释: 在跳转的时候创建A页面,跳转过去,同时将对应的数据传递给一个字符串类型; 同时在A类的.H中声明
方法供B类调用 这样A类才能得到传递回去的城市名称, 才能更改按钮的名称, 以及首页的数据;
这里要知道反向传值的流程
1.在B类的.h中声明一个A类型的weak属性delegate
2.在A类中创建完B类的时候给B类型对象的delegate属性赋值
3.在A类的.h中声明方法供B类调用 如果需要传递回来参数那该方法声明时需要带参数
4.在A类中使用此方法;
在viewWillAppear里面写最后一个; 写完这些,恭喜你,搜索功能完成了;
<<HomeTableViewController.m>>
主页页面
第一次加载的时候显示3张个性化, 第二次加载就直接不显示3张个性化了
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
int runCount = [ud integerForKey:@"runCount"];
runCount ++ ;
[ud setInteger:runCount forKey:@"runCount"];
[ud synchronize];
//声明一个轻量级技术方法;
//声明一个数字记录, 每次记录的时候需要把之前的数字取出来,如果数字没有值就证明是第1次
//每次取值后都应该 让计数+1;
//通过方法将记录在ud当中 键值为runCount的对应计数属性 取出来, 并同步
//简单计数方法实现。
//如果runCount为1,证明系统是第一次进来, 第一次进来就要显示个性化界面
if (runCount == 1) {
//只有ViewController才有storyBord,当前self指的是AppDelegate.
//先拿到根才可以拿到storyBord
UIViewController*appView = self.window.rootViewController;
ViewController *vc =[appView.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
self.window.rootViewController =vc;