《iPad开发从入门到精通》——6.3节站站查询

    xiaoxiao2024-01-01  139

    本节书摘来自异步社区《iPad开发从入门到精通》一书中的第6章,第6.3节站站查询,作者 杨春泽,更多章节内容可以访问云栖社区“异步社区”公众号查看

    6.3 站站查询iPad开发从入门到精通本模块的功能是提供站站查询功能,只需输入起始站和目的站的名称,就可以快速查询到符合要求的公交线路了。在本节的内容中,将详细讲解站站查询模块的具体实现流程。

    6.3.1 站站查询主视图站站查询主视图CBus_StatToStatView.xib的UI界面如图6-6所示,在上方显示搜索表单,下方列表显示了30条线路。

    实现文件CBus_StatToStatViewController.h的代码如下所示。

    #import <UIKit/UIKit.h> #define kBeginStationComponent 0 #define kEndStationComponent 1 enum EStationType{   EBeginStationType,   EEndStationType,   ENoneStationType }; @interface CBus_StatToStatViewController : UIViewController <UITableViewDelegate, UITableViewDataSource,           UISearchDisplayDelegate, UISearchBarDelegate, UIPickerView Delegate, UIPickerViewDataSource>{   UITableView    *busStatToStatTableView;   UISearchBar    *currentSearchBar;   UISearchBar    *beginSearchBar;   UISearchBar    *endSearchBar;     UIPickerView  *stationPickView;   NSInteger     currentBeginIndex;   NSInteger     currentEndIndex;     NSInteger     stationType;     NSMutableArray  *beginFilteredListContent;   NSMutableArray  *endFilteredListContent;   NSString    *ifSelectedPickerString;   NSString    *ifSelectedPickEndString;   BOOL       isSearchBegin;   BOOL       isSearchEndBegin;   BOOL       isJumpToStat;   UIBarButtonItem  *returnKeyBordBtn; } @property (nonatomic, retain) IBOutlet UITableView    *busStatToStatTableView; @property (nonatomic, retain) IBOutlet UISearchBar    *beginSearchBar; @property (nonatomic, retain) IBOutlet UISearchBar    *endSearchBar; @property (nonatomic, retain) IBOutlet UIPickerView    *stationPickView; @property(nonatomic, retain)      NSMutableArray  *beginFilteredListContent; @property(nonatomic, retain)      NSMutableArray  *endFilteredListContent; @property(nonatomic) BOOL   isJumpToStat; - (void)filterContentForSearchText:(NSString*)searchText; @end 文件CBus_StatToStatViewController.m是文件CBus_StatToStatViewController.h的实现,功能是显示搜索表单,根据用户输入的起始站点和终点站信息,列表显示符合搜索条件的线路。 #import "CBus_StatToStatViewController.h" #import "CBus_LineDetailViewController.h" #import "CBus_StationDetailViewController.h" #import "CDataContainer.h" @implementation CBus_StatToStatViewController @synthesize busStatToStatTableView,beginFilteredListContent,endFilteredListContent; @synthesize beginSearchBar, endSearchBar, stationPickView; @synthesize isJumpToStat; // 界面初始化 - (void)viewDidLoad {   [super viewDidLoad];   returnKeyBordBtn = [[UIBarButtonItem alloc] initWithTitle:@"收起键盘"  style:UIBarButtonItemStylePlain target:self action:@selector(HideKeyBoard:)];   self.navigationItem.rightBarButtonItem = nil;// 输入键盘;   self.beginFilteredListContent = [NSMutableArray arrayWithArray:[CDataContainer Instance].stationNameArray];   self.endFilteredListContent = [NSMutableArray arrayWithArray:[CDataContainer Instance].stationNameArray];   isSearchBegin = NO;   isSearchEndBegin = NO;   isJumpToStat = NO;   ifSelectedPickerString = [[NSString alloc] init];   ifSelectedPickEndString = [[NSString alloc] init]; } #pragma mark - #pragma mark View lifecycle //根据设置的样式显示 - (void)viewWillAppear:(BOOL)animated{   [super viewWillAppear:animated];   [self.busStatToStatTableView reloadData];   NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];   NSInteger styleNum = [userDefault integerForKey:@"styleType"];   switch (styleNum){     case 0:{       [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyle Default;       self.navigationController.navigationBar.barStyle = UIBarStyleDefault;       self.beginSearchBar.barStyle = UIBarStyleDefault;       self.endSearchBar.barStyle = UIBarStyleDefault;       break;     }     case 1:{       [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyle BlackOpaque;       self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;       self.beginSearchBar.barStyle = UIBarStyleBlackOpaque;       self.endSearchBar.barStyle = UIBarStyleBlackOpaque;       break;     }   }   self.navigationItem.rightBarButtonItem = nil;   stationType = ENoneStationType;   [stationPickView reloadAllComponents];   if(isJumpToStat){     CBus_StationDetailViewController *detailViewController = [[CBus_Station DetailViewController alloc] initWithNibName:@"CBus_StationDetailView" bundle:nil];     detailViewController.currentStationName = [[CDataContainer Instance].stationNameArray objectAtIndex:currentBeginIndex];     detailViewController.currentStationIndex = currentBeginIndex+1;     [self.navigationController pushViewController:detailViewController animated:YES];     [detailViewController release];     isJumpToStat = NO;   }   } - (void)viewDidAppear:(BOOL)animated{   [super viewDidAppear:animated]; } - (void)viewWillDisappear:(BOOL)animated{   [super viewWillDisappear:animated]; } - (void)viewDidDisappear:(BOOL)animated{   [super viewDidDisappear:animated];   beginSearchBar.text = @"";   endSearchBar.text = @""; } // 默认图片 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation{ // Return YES for supported orientations   return (interfaceOrientation == UIInterfaceOrientationPortrait); } -(IBAction)HideKeyBoard:(id)sender{   NSLog(@"------HideKeyBoard------");   self.navigationItem.rightBarButtonItem = nil;   beginSearchBar.text = @"";   endSearchBar.text = @"";   stationPickView.hidden = YES;   [currentSearchBar resignFirstResponder]; } #pragma mark - #pragma mark Table view data source - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger) section{   return @"站站查询"; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger) section{   return 30; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{   return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{   return [[CDataContainer Instance].stationNameArray count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {   static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) {     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];   }     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;   cell.selectionStyle = UITableViewCellSelectionStyleGray;   // Configure the cell   cell.textLabel.text = [[CDataContainer Instance].stationNameArray objectAtIndex: indexPath.row];   cell.imageView.image = [UIImage imageNamed:@"bus_table_stat.png"];   return cell; } #pragma mark - #pragma mark Table view delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {   // 创造和推动另一个视图控制器   CBus_StationDetailViewController *detailViewController = [[CBus_StationDetail ViewController alloc] initWithNibName:@"CBus_StationDetailView" bundle:nil];   detailViewController.currentStationName = [[CDataContainer Instance].stationName Array objectAtIndex:indexPath.row];   detailViewController.currentStationIndex = indexPath.row+1;   [self.navigationController pushViewController:detailViewController animated:YES];   [detailViewController release]; } -(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath: (NSIndexPath *)indexPath { } #pragma mark - #pragma mark Picker view data source - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{   return 2; } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger) component{   if (component == kBeginStationComponent){     if ([beginSearchBar.text isEqualToString:@""]){       return [[CDataContainer Instance].stationNameArray count];     }     if (stationType == EBeginStationType){       return[beginFilteredListContent count];     }     else{       return [[CDataContainer Instance].stationNameArray count];     }   }   else if(component == kEndStationComponent){     if ([endSearchBar.text isEqualToString:@""]){       return [[CDataContainer Instance].stationNameArray count];     }     if (stationType == EEndStationType){       return[endFilteredListContent count];     }     else {       return [[CDataContainer Instance].stationNameArray count];     }   }     return 0; } - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{   if (component == kBeginStationComponent){     if ([beginSearchBar.text isEqualToString:@""]){       return [[CDataContainer Instance].stationNameArray objectAtIndex:row];     }     if (stationType == EBeginStationType){       return[beginFilteredListContent objectAtIndex:row];     }     else {       return [[CDataContainer Instance].stationNameArray objectAtIndex:row];     }   }   else if(component == kEndStationComponent){     if ([endSearchBar.text isEqualToString:@""]){       return [[CDataContainer Instance].stationNameArray objectAtIndex:row];     }     if (stationType == EEndStationType){       return[endFilteredListContent objectAtIndex:row];     }     else {       return [[CDataContainer Instance].stationNameArray objectAtIndex:row];     }   }   return nil; } - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent: (NSInteger)component{   if (component == kBeginStationComponent){     currentBeginIndex = row;     if ([beginSearchBar.text isEqualToString:@""]){       isSearchBegin = NO;       ifSelectedPickerString = [[CDataContainer Instance].stationNameArray objectAtIndex:row];       beginSearchBar.text = [[CDataContainer Instance].stationNameArray objectAtIndex:row];     }     else if (stationType == EBeginStationType){       ifSelectedPickerString = [beginFilteredListContent objectAtIndex:row];       beginSearchBar.text = [beginFilteredListContent objectAtIndex:row];     }     else {       ifSelectedPickerString = [[CDataContainer Instance].stationNameArray objectAtIndex:row];       beginSearchBar.text = [[CDataContainer Instance].stationNameArray objectAtIndex:row];     }   }   else if(component == kEndStationComponent){     currentEndIndex = row;     if ([endSearchBar.text isEqualToString:@""]){       isSearchEndBegin = NO;       ifSelectedPickEndString = [[CDataContainer Instance].stationNameArray objectAtIndex:row];       endSearchBar.text = [[CDataContainer Instance].stationNameArray object AtIndex:row];     }     else if (stationType == EEndStationType){       ifSelectedPickEndString = [endFilteredListContent objectAtIndex:row];       endSearchBar.text = [endFilteredListContent objectAtIndex:row];     }     else{       ifSelectedPickEndString = [[CDataContainer Instance].stationNameArray objectAtIndex:row];       endSearchBar.text = [[CDataContainer Instance].stationNameArray object AtIndex:row];     }   } } #pragma mark - #pragma mark SearchBarText delegate - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{   NSLog(@"------searchBarTextDidBeginEditing---");   stationPickView.hidden = NO;   self.navigationItem.rightBarButtonItem = returnKeyBordBtn;   currentSearchBar = searchBar;   if (currentSearchBar == beginSearchBar){     NSLog(@"---------Type------EBeginStationType");     stationType = EBeginStationType;   }   else{     NSLog(@"---------Type------EEndStationType");     stationType = EEndStationType;   } } - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{   NSLog(@"------searchBarTextDidEndEditing---"); } - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{   NSLog(@"--------SearchBar_text Changed--------");   if(searchBar == beginSearchBar)   {     if ([ifSelectedPickerString isEqualToString:searchText]){       return;     }     if (stationType == EBeginStationType){       isSearchBegin = YES;       [self filterContentForSearchText:searchText];       [stationPickView reloadAllComponents];     }   }   else if(searchBar == endSearchBar){     if ([ifSelectedPickEndString isEqualToString:searchText]){       return;     }     if (stationType == EEndStationType){       isSearchEndBegin = YES;       [self filterContentForSearchText:searchText];       [stationPickView reloadAllComponents];     }   } } - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{   NSLog(@"------searchBarSearchButtonClicked---");     CBus_StationDetailViewController *detailViewController = [[CBus_StationDetail ViewController alloc] initWithNibName:@"CBus_StationDetailView" bundle:nil];   if ((![beginSearchBar.text isEqualToString:@""])&&(![endSearchBar.text isEqual ToString:@""])){     NSLog(@"------站站查询------");     if (isSearchBegin){       detailViewController.beginStationName = [beginFilteredListContent objectAtIndex:currentBeginIndex];       detailViewController.beginStationIndex = [[CDataContainer Instance]. stationNameArray indexOfObject:[beginFilteredListContent objectAtIndex:currentBeginIndex]]+1;     }     else {       detailViewController.beginStationName = [[CDataContainer Instance]. stationNameArray objectAtIndex:currentBeginIndex];       detailViewController.beginStationIndex = currentBeginIndex+1;     }         if (isSearchEndBegin){       detailViewController.endStationName = [endFilteredListContent object AtIndex:currentEndIndex];       detailViewController.endStationIndex = [[CDataContainer Instance]. stationNameArray indexOfObject:[endFilteredListContent objectAtIndex:currentEndIndex]]+1;     }     else {       detailViewController.endStationName = [[CDataContainer Instance]. stationNameArray objectAtIndex:currentEndIndex];       detailViewController.endStationIndex = currentEndIndex+1;     }         detailViewController.isStatToStat = YES;   }   else if (stationType == EBeginStationType){     NSLog(@"------始站查询------");     if (isSearchBegin){       detailViewController.currentStationName = [beginFilteredListContent objectAtIndex:currentBeginIndex];       detailViewController.currentStationIndex = [[CDataContainer Instance]. stationNameArray indexOfObject:[beginFilteredListContent objectAtIndex:currentBeginIndex]]+1;     }     else {       detailViewController.currentStationName = [[CDataContainer Instance]. stationNameArray objectAtIndex:currentBeginIndex];       detailViewController.currentStationIndex = currentBeginIndex+1;     }     detailViewController.isStatToStat = NO;   }   else if(stationType == EEndStationType){     NSLog(@"------尾站查询------");     if (isSearchEndBegin){       detailViewController.currentStationName = [endFilteredListContent objectAtIndex:currentEndIndex];       detailViewController.currentStationIndex = [[CDataContainer Instance]. stationNameArray indexOfObject:[endFilteredListContent objectAtIndex:currentEndIndex]]+1;     }     else{       detailViewController.currentStationName = [[CDataContainer Instance]. stationNameArray objectAtIndex:currentEndIndex];       detailViewController.currentStationIndex = currentEndIndex+1;     }     detailViewController.isStatToStat = NO;   }     [currentSearchBar resignFirstResponder];    stationPickView.hidden = YES;   // 通过选定的对象到新视图控制器   [self.navigationController pushViewController:detailViewController animated:YES];   [detailViewController release]; } #pragma mark - #pragma mark Search Methods - (void)filterContentForSearchText:(NSString*)searchText;{   //基于搜索文本和范围更新过滤阵列   //首先清除过滤数组   if (stationType == EBeginStationType){     [self.beginFilteredListContent removeAllObjects];     for (int i = 0; i < [[CDataContainer Instance].stationNameArray count]; i++){       NSString *searchNameInfo = [[CDataContainer Instance].stationNameArray objectAtIndex:i];       NSString *searchPYInfo = [[CDataContainer Instance].stationPYArray objectAtIndex:i];       NSComparisonResult result = [searchNameInfo compare:searchText options: (NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];       NSComparisonResult resultPY = [searchPYInfo compare:searchText options: (NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];       if (result == NSOrderedSame || resultPY == NSOrderedSame){         [self.beginFilteredListContent addObject:searchNameInfo];       }     }   }   else if(stationType == EEndStationType){     [self.endFilteredListContent removeAllObjects];     for (int i = 0; i < [[CDataContainer Instance].stationNameArray count]; i++){       NSString *searchNameInfo = [[CDataContainer Instance].stationNameArray objectAtIndex:i];       NSString *searchPYInfo = [[CDataContainer Instance].stationPYArray objectAtIndex:i];       NSComparisonResult result = [searchNameInfo compare:searchText options: (NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];       NSComparisonResult resultPY = [searchPYInfo compare:searchText options: (NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];       if (result == NSOrderedSame || resultPY == NSOrderedSame){         [self.endFilteredListContent addObject:searchNameInfo];       }     }   } } - (void)dealloc {   [busStatToStatTableView release];   [beginSearchBar release];   [endSearchBar release];   [currentSearchBar release];   [stationPickView release];   [beginFilteredListContent release];   [endFilteredListContent release];   [returnKeyBordBtn release];   [super dealloc]; } @end

    执行效果如图6-7所示。

    6.3.2 站站查询详情视图站站查询详情视图CBus_StatDetailStatView.xib的UI界面如图6-8所示,在上方显示票价、始发站、终点站、首班车时间和末班车时间,在下方列表显示了30条线路。

    其实本模块和上一节中的线路详情模块类似,实现文件CBus_StatDetailStatViewController.m的主要代码如下所示。

    #import "CBus_StatDetailStatViewController.h" #import "CBus_StationDetailViewController.h" #import "CBus_LineDetailLineViewController.h" #import "CDataContainer.h" @implementation CBus_StatDetailStatViewController @synthesize busLineDetailTableView,currentLineName; @synthesize currentLineIndex; @synthesize isStatToStat; // 视图初始化 - (void)viewDidLoad{   [super viewDidLoad];     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBar ButtonSystemItem:UIBarButtonSystemItemAdd                        target:self        action:@selector(AddLineToFavorite)];   [[CDataContainer Instance] GetLineStationFromTableSequence:currentLineIndex]; } #pragma mark - #pragma mark View lifecycle - (void)viewWillAppear:(BOOL)animated {   [super viewWillAppear:animated];   [self.busLineDetailTableView reloadData];   NSLog(@"-----Nav----%@",self.navigationController.viewControllers);   NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];   NSInteger styleNum = [userDefault integerForKey:@"styleType"];   switch (styleNum) {     case 0:{       [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;       self.navigationController.navigationBar.barStyle = UIBarStyleDefault;       self.searchDisplayController.searchBar.barStyle = UIBarStyleDefault;       break;     }     case 1:{       [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyle BlackOpaque;       self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;       self.searchDisplayController.searchBar.barStyle = UIBarStyleBlackOpaque;       break;     }   } } - (void)viewDidDisappear:(BOOL)animated {   [super viewDidDisappear:animated];   isStatToStat = NO; } -(void)AddLineToFavorite{   NSLog(@"-------addLineToFavorite---------%@---%d",currentLineName,currentLineIndex);   for(NSString *lineName in [CDataContainer Instance].favoriteLineNameArray){     if ([lineName isEqualToString:currentLineName]) {       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"收藏"         message:[NSString stringWithFormat:@"%@ 已收藏",currentLineName]         delegate:self    cancelButtonTitle:@"确定"    otherButtonTitles:nil];       [alert show];       [alert release];       return;     }   }   [[CDataContainer Instance] InsertFavoriteInfoToDatabase:0 AddName:currentLine Name AddIndex:currentLineIndex AddNameEnd:nil AddIndexEnd:0];   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"收藏"   message:[NSString stringWithFormat:@"收藏 %@ 成功",currentLineName]   delegate:self   cancelButtonTitle:@"确定"   otherButtonTitles:nil];   [alert show];   [alert release]; } #pragma mark - #pragma mark Table view data source - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{   return currentLineName; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{   return 30; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {   // Return the number of sections   return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {   // Return the number of rows in the section   return [[CDataContainer Instance].sequenceNumArray count]; } // Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell";   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];   if (cell == nil){     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];   }     cell.selectionStyle = UITableViewCellSelectionStyleGray;   cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; // Configure the cell   cell.textLabel.text = [[CDataContainer Instance].stationNameArray objectAtIndex: [[CDataContainer Instance] GetBusLineSequenceByIndex:indexPath.row]-1];   cell.imageView.image = [UIImage imageNamed:@"bus_table_stat.png"];   return cell; } #pragma mark - #pragma mark Table view delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{   // Navigation logic may go here. Create and push another view controller   if (isStatToStat) {     return;   }     CBus_StationDetailViewController *statDetailViewController = [self.navigation Controller.viewControllers objectAtIndex:1];   statDetailViewController.currentStationName = [[CDataContainer Instance].station NameArray objectAtIndex:[[CDataContainer Instance] GetBusLineSequenceByIndex:indexPath.row]-1];   statDetailViewController.currentStationIndex = [[CDataContainer Instance].station NameArray indexOfObject:statDetailViewController.currentStationName]+1;   [self.navigationController popViewControllerAnimated:YES]; } 相关资源:敏捷开发V1.0.pptx
    最新回复(0)