《iPad开发从入门到精通》——6.4节收藏历史

简介:

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

6.4 收藏历史
iPad开发从入门到精通
为了方便用户,特意提供了收藏历史功能,用户可以将经常用到的信息收藏起来,便于以后查询。主要包括如下3类收藏信息。
收藏线路。
收藏站点。
收藏站站。
CFavoriteView.xib的UI界面如图6-9所示,在上方显示了3个选项卡,在下方列表中显示了具体的收藏信息。

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

#import <UIKit/UIKit.h>
enum ESegCtrlIndex 
{
  EFavorite_Line,
  EFavorite_Stat,
  EFavorite_StatToStat
}segCtrlIndex;
@interface CFavoriteViewController : UIViewController <UITableViewDelegate,UITableView DataSource>{


87a0f4038b0b645711af79cdc08753aab56c2820
  UITableView      *favoriteTableView;

  UINavigationBar    *favNavigationBar;
  UISegmentedControl  *favoriteSegCtrl;

  NSInteger      ESegType;
}
@property (nonatomic, retain) IBOutlet  UITableView      *favoriteTableView;
@property (nonatomic, retain) IBOutlet   UISegmentedControl  *favoriteSegCtrl;
@property (nonatomic, retain) IBOutlet UINavigationBar    *favNavigationBar;
-(IBAction)OnSegmentIndexChanged:(id)sender;
@end
文件CFavoriteViewController.m是文件CFavoriteViewController.h的实现,分别实现了收藏线路、收藏站点和收藏站站的功能,并且还可以删除不需要的收藏信息。具体实现代码如下所示。

#import "CFavoriteViewController.h"
#import "CDataContainer.h"
#import "CBus_LineDetailViewController.h"
#import "CBus_StationDetailViewController.h"
@implementation CFavoriteViewController
@synthesize favoriteTableView,favoriteSegCtrl,favNavigationBar;
// 视图初始化
- (void)viewDidLoad {
  [super viewDidLoad];
  ESegType = EFavorite_Line;
  UIBarButtonItem *editButton = [[UIBarButtonItem alloc]
                  initWithTitle:@"Delete"
                  style:UIBarButtonItemStyleBordered
                  target:self
                  action:@selector(toggleEdit:)];
  self.navigationItem.rightBarButtonItem = editButton;
  [editButton release];  
}
-(IBAction)toggleEdit:(id)sender{
  [self.favoriteTableView setEditing:!self.favoriteTableView.editing animated:YES];

  if (self.favoriteTableView.editing){
    [self.navigationItem.rightBarButtonItem setTitle:@"Done"];
  }
  else{
    [self.navigationItem.rightBarButtonItem setTitle:@"Delete"];
  }
}
#pragma mark -
#pragma mark View lifecycle
- (void)viewWillAppear:(BOOL)animated{
  [super viewWillAppear:animated];
  [favoriteTableView reloadData];  
  NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
  NSInteger styleNum = [userDefault integerForKey:@"styleType"];
  switch (styleNum) {
    case 0:{
      [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
      self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
      self.favoriteSegCtrl.tintColor = [UIColor colorWithRed:0.48  green:0.56 blue:0.66 alpha:1.0];
      self.favNavigationBar.barStyle = UIBarStyleDefault;
      break;
    }
    case 1:{
      [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyle BlackOpaque;
      self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
      self.favoriteSegCtrl.tintColor = [UIColor darkGrayColor];
      self.favNavigationBar.barStyle = UIBarStyleBlackOpaque;
      break;
    }
  }
  [favoriteTableView reloadData];
}
#pragma mark -
#pragma mark Table view data source
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
  if (ESegType == EFavorite_Line){
    return @"收藏线路";
  }
  else if(ESegType == EFavorite_Stat){
    return @"收藏站点";
  }
  else if(ESegType == EFavorite_StatToStat){
    return @"收藏站站";
  }

  return nil;
}
- (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
  if (ESegType == EFavorite_Line){
    return [[CDataContainer Instance].favoriteLineNameArray count];
  }
  else if(ESegType == EFavorite_Stat){
    return [[CDataContainer Instance].favoriteStationNameArray count];
  }
  else if(ESegType == EFavorite_StatToStat){
    return [[CDataContainer Instance].favoriteStatToStatBeginNameArray count];
  }  
  return 0;
}
// 自定义单元格的外观
- (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;
  // 配置单元格
  if (ESegType == EFavorite_Line){
    [[CDataContainer Instance] GetLineStationFromTableSequence:
     [[[CDataContainer Instance].favoriteLineIndexArray objectAtIndex:indexPath.row] intValue]];
    NSString *beginStr = [[CDataContainer Instance].stationNameArray   
objectAtIndex:GetBusLineSequenceByIndex:0]-1];
    NSString *endStr = [[CDataContainer Instance].stationNameArray objectAtIndex:                 [[CDataContainer Instance] GetBusLineSequenceBy Index:[[CDataContainer Instance].sequenceNumArray count]-1]-1];
    NSString *detailStr = [[NSString alloc] initWithFormat:@"%@->%@",beginStr,endStr];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:12];
    cell.detailTextLabel.text = detailStr;
    [detailStr release];    
    cell.imageView.image = [UIImage imageNamed:@"bus_table_line.png"];
    cell.textLabel.text = [[CDataContainer Instance].favoriteLineNameArray   
objectAtIndex:indexPath.row];
  }
  else if(ESegType == EFavorite_Stat){
    cell.detailTextLabel.text = @"";
    cell.imageView.image = [UIImage imageNamed:@"bus_table_stat.png"];
    cell.textLabel.text = [[CDataContainer Instance].favoriteStationNameArray   
objectAtIndex:indexPath.row];
  }
  else if(ESegType == EFavorite_StatToStat){
    cell.detailTextLabel.text = @"";
    cell.imageView.image = [UIImage imageNamed:@"bus_statTostat.png"];

    NSString *beginStr = [[CDataContainer Instance].favoriteStatToStatBeginName Array objectAtIndex:indexPath.row];
    NSString *endStr  = [[CDataContainer Instance].favoriteStatToStatEndName Array objectAtIndex:indexPath.row];
    NSString *detailStr= [[NSString alloc] initWithFormat:@"%@->%@",beginStr,endStr];
    cell.textLabel.text = detailStr;
    [detailStr release];
  }

  return cell;
}
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
  return YES;
}
// 优先支持编辑表视图
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditing Style)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
  if (editingStyle == UITableViewCellEditingStyleDelete)
  {
  // 从数据源中删除行

    if (ESegType == EFavorite_Line) {
      [[CDataContainer Instance] DeleteFavoriteInfoToDatabase:0
                             DeleteName:  
[[CDataContainer Instance].favoriteLineNameArray objectAtIndex:indexPath.row]
                           DeteleNameEnd:nil];
      [[CDataContainer Instance].favoriteLineNameArray removeObjectAtIndex:  
indexPath.row];
      [[CDataContainer Instance].favoriteLineIndexArray removeObjectAtIndex:  
indexPath.row];
    }
    else if(ESegType == EFavorite_Stat){
      [[CDataContainer Instance] DeleteFavoriteInfoToDatabase:1
                             DeleteName:[[CDataContainer Instance].favoriteStationNameArray objectAtIndex:  
indexPath.row] 
                           DeteleNameEnd:nil];
      [[CDataContainer Instance].favoriteStationNameArray removeObject  
AtIndex:indexPath.row];
      [[CDataContainer Instance].favoriteStationIndexArray removeObject  
AtIndex:indexPath.row];
    }
    else if(ESegType == EFavorite_StatToStat){
      [[CDataContainer Instance] DeleteFavoriteInfoToDatabase:2
                             DeleteName:  
[[CDataContainer Instance].favoriteStatToStatBeginNameArray objectAtIndex:indexPath.row]
                           DeteleNameEnd:  
[[CDataContainer Instance].favoriteStatToStatEndNameArray objectAtIndex:indexPath.row]];

      [[CDataContainer Instance].favoriteStatToStatBeginNameArray remove  
ObjectAtIndex:indexPath.row];
      [[CDataContainer Instance].favoriteStatToStatBeginIndexArray remove  
ObjectAtIndex:indexPath.row];  
      [[CDataContainer Instance].favoriteStatToStatEndNameArray remove  
ObjectAtIndex:indexPath.row];
      [[CDataContainer Instance].favoriteStatToStatEndIndexArray remove  
ObjectAtIndex:indexPath.row];
    }

    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] with  
RowAnimation:UITableViewRowAnimationFade];
  }  
  else if (editingStyle == UITableViewCellEditingStyleInsert) {
  // 创建一个新实例的适当的类,将它插入到数组中,并添加一个新行表观
  }  
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  // 导航逻辑创造和推动另一个视图控制器  
  if (ESegType == EFavorite_Line){
    CBus_LineDetailViewController *detailViewController = [[CBus_LineDetailView Controller alloc] initWithNibName:@"CBus_LineDetailView" bundle:nil];

    detailViewController.currentLineIndex = [[[CDataContainer Instance].  
favoriteLineIndexArray objectAtIndex:indexPath.row] intValue];
    detailViewController.currentLineName = [[CDataContainer Instance].  
favoriteLineNameArray objectAtIndex:indexPath.row];

    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];
  }
  else if(ESegType == EFavorite_Stat){
    CBus_StationDetailViewController *detailViewController = [[CBus_Station DetailViewController alloc] initWithNibName:@"CBus_StationDetailView" bundle:nil];

    detailViewController.currentStationIndex = [[[CDataContainer Instance].  
favoriteStationIndexArray objectAtIndex:indexPath.row] intValue];
    detailViewController.currentStationName = [[CDataContainer Instance].  
favoriteStationNameArray objectAtIndex:indexPath.row];

    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];
  }
  else if(ESegType ==EFavorite_StatToStat){
    CBus_StationDetailViewController *detailViewController = [[CBus_Station  
DetailViewController alloc] initWithNibName:@"CBus_StationDetailView" bundle:nil];
    detailViewController.beginStationName = [[CDataContainer Instance].  
favoriteStatToStatBeginNameArray objectAtIndex:indexPath.row];
    detailViewController.beginStationIndex = [[[CDataContainer Instance].  
favoriteStatToStatBeginIndexArray objectAtIndex:indexPath.row] intValue];
    detailViewController.endStationName = [[CDataContainer Instance].favorite  
StatToStatEndNameArray objectAtIndex:indexPath.row];
    detailViewController.endStationIndex = [[[CDataContainer Instance].favorite  
StatToStatEndIndexArray objectAtIndex:indexPath.row] intValue];

    detailViewController.isStatToStat = YES;

    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];
  }
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)  

interfaceOrientation {
  // Return YES for supported orientations
  return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(IBAction)OnSegmentIndexChanged:(id)sender{
  if ([sender selectedSegmentIndex] == 0){
    ESegType = EFavorite_Line;
  }
  else if([sender selectedSegmentIndex] == 1){
    ESegType = EFavorite_Stat;
  }
  else if([sender selectedSegmentIndex] == 2){
    ESegType = EFavorite_StatToStat;
  }
  [favoriteTableView reloadData];
}
- (void)didReceiveMemoryWarning {

  [super didReceiveMemoryWarning];
}
- (void)viewDidUnload {

  [super viewDidUnload];
  // Release any retained subviews of the main view
  // e.g. self.myOutlet = nil;
  self.favoriteTableView = nil; 
}
- (void)dealloc {

  [favoriteTableView release];
  [favoriteSegCtrl release];
  [favNavigationBar release];
  [super dealloc];
}
@end

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


72fde5f40d8bdeb6ef8fb368da24166c4bfeda0a
相关文章
|
Web App开发 前端开发 JavaScript
如何利用ipad随时随地开发代码
如何利用ipad随时随地开发代码
492 1
如何利用ipad随时随地开发代码
|
网络协议 Ubuntu 网络安全
【服务器】iPad远程服务器进行开发(下)
【服务器】iPad远程服务器进行开发(下)
436 0
|
移动开发 Ubuntu 网络协议
【服务器】iPad远程服务器进行开发(上)
【服务器】iPad远程服务器进行开发
292 0
|
Android开发 iOS开发
Instagram CEO :“苹果 iPad 不咋受欢迎,不值得给开发个专属版本的 App”
Instagram CEO :“苹果 iPad 不咋受欢迎,不值得给开发个专属版本的 App”
125 0
Instagram CEO :“苹果 iPad 不咋受欢迎,不值得给开发个专属版本的 App”
|
iOS开发
iOS开发UI篇—iPad开发中得modal介绍
iOS开发UI篇—iPad开发中得modal介绍 一、简单介绍   说明1:   在iPhone开发中,Modal是一种常见的切换控制器的方式     默认是从屏幕底部往上弹出,直到完全盖住后面的内容为止 说明2:   在iPad开发中,Modal的使用频率也是非常高的   对...
790 0
|
数据安全/隐私保护 iOS开发
iOS开发UI篇—模仿ipad版QQ空间登录界面
iOS开发UI篇—模仿ipad版QQ空间登录界面 一、实现和步骤 1.一般ipad项目在命名的时候可以加一个HD,标明为高清版 2.设置项目的文件结构,分为home和login两个部分    3.
670 0
|
API iOS开发 编解码
iOS开发UI篇—iPad和iPhone开发的比较
iOS开发UI篇—iPad和iPhone开发的比较 一、iPad简介 1.什么是iPad   一款苹果公司于2010年发布的平板电脑   定位介于苹果的智能手机iPhone和笔记本电脑产品之间   跟iPhone一样,搭载的是iOS操作系统    2.
1061 0

相关实验场景

更多
下一篇
DataWorks