IOS 大众点评 搜索界面实现

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类中使用此方法;

 

计算机生成了可选文字:CityListTableViewController *cityVc cityVc. de legate self; segue. destinationViewControtter;

 

 

计算机生成了可选文字:HomeTab1eViewController (self. storyboard instant iateV iewCont ro lerWithIdent HomeTableViewCont rot ler" self. citys (indexPath. row) ; NSSt ring *cityNames — backCityName : cityNamesl ; self. navigationController pushViewController: animated : YES

 

计算机生成了可选文字:) backCityName: (NSSt ring cityName( void self. btnTitle setTitle:cityName

 

 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的对应计数属性 取出来,  并同步

    //简单计数方法实现。

   

    //如果runCount1,证明系统是第一次进来,  第一次进来就要显示个性化界面

    if (runCount == 1) {

        //只有ViewController才有storyBord,当前self指的是AppDelegate.

        //先拿到根才可以拿到storyBord

        UIViewController*appView = self.window.rootViewController;

        ViewController *vc =[appView.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];

        self.window.rootViewController =vc;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值