FavoritesViewController.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // FavoritesViewController.m
  3. // Apex Mobile
  4. //
  5. // Created by Ray on 14-4-25.
  6. // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "FavoritesViewController.h"
  9. @interface FavoritesViewController ()
  10. @end
  11. @implementation FavoritesViewController
  12. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  13. {
  14. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  15. if (self) {
  16. // Custom initialization
  17. }
  18. return self;
  19. }
  20. - (BOOL)shouldAutorotate
  21. {
  22. return YES;
  23. }
  24. - (UIInterfaceOrientationMask)supportedInterfaceOrientations
  25. {
  26. return UIInterfaceOrientationMaskPortrait;
  27. }
  28. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
  29. {
  30. return UIInterfaceOrientationPortrait;
  31. }
  32. - (void)viewDidLoad
  33. {
  34. [super viewDidLoad];
  35. self.data =[[FavoritesData alloc]init];
  36. [self.tableview reloadData];
  37. self.tableview.tableFooterView = [UIView new];
  38. UIBarButtonItem *clearItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"ic_clear"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(clearClick:)];
  39. self.navigationItem.rightBarButtonItem = clearItem;
  40. }
  41. -(void)viewWillAppear:(BOOL)animated
  42. {
  43. }
  44. - (void)didReceiveMemoryWarning
  45. {
  46. [super didReceiveMemoryWarning];
  47. // Dispose of any resources that can be recreated.
  48. }
  49. - (void)clearClick:(id)sender {
  50. __weak typeof(self) weakSelf = self;
  51. [self.data deleteAll:^(BOOL success){
  52. if (success) {
  53. dispatch_async(dispatch_get_main_queue(), ^{
  54. [weakSelf.tableview reloadData];
  55. });
  56. }
  57. }];
  58. }
  59. /*
  60. #pragma mark - Navigation
  61. // In a storyboard-based application, you will often want to do a little preparation before navigation
  62. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  63. {
  64. // Get the new view controller using [segue destinationViewController].
  65. // Pass the selected object to the new view controller.
  66. }
  67. */
  68. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  69. {
  70. return 1;
  71. }
  72. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  73. {
  74. return [self.data get_count];
  75. }
  76. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  77. {
  78. DebugLog(@"cellForRowAtIndexPath");
  79. NSString *CellIdentifier = @"cell_item_history";
  80. CellItemHistory *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  81. NSString* name = [self.data.pagedata[indexPath.row] valueForKey:@"name"];
  82. NSString* create_time = [self.data.pagedata[indexPath.row] valueForKey:@"create_time"];
  83. NSString *module_name = [self.data.pagedata[indexPath.row] valueForKey:@"module_name"];
  84. // Cargo Tracking
  85. if ([module_name isEqualToString:@"Ocean Booking"]) {
  86. cell.icon.image = [[UIImage imageNamed:@"mode_booking"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  87. } else if ([module_name isEqualToString:@"Container detail"]) {
  88. cell.icon.image = [[UIImage imageNamed:@"mode_container"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  89. } else if ([module_name isEqualToString:@"Ocean B/L info."]) {
  90. cell.icon.image = [[UIImage imageNamed:@"mode_bl"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  91. } else if ([module_name isEqualToString:@"Cargo Tracking"]) {
  92. cell.icon.image = [[UIImage imageNamed:@"mode_cargo_tracking"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  93. } else {
  94. cell.icon.image = [[UIImage imageNamed:@"mode_document"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  95. }
  96. cell.name.text = name;
  97. [cell.name sizeToFit];
  98. cell.time.text =create_time;
  99. [cell.time sizeToFit];
  100. // cell.imageView.image = [UIImage imageNamed:[self.toolsinfo[indexPath.row] valueForKey:@"img"]];
  101. return cell;
  102. }
  103. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  104. {
  105. // NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithDictionary:self.params copyItems:true];
  106. NSString* str_params =[self.data.pagedata[indexPath.row] valueForKey:@"params"];
  107. NSData* data_params = [str_params dataUsingEncoding:NSUTF8StringEncoding];
  108. NSMutableDictionary* params = [[NSMutableDictionary alloc] initWithDictionary:[NSJSONSerialization
  109. JSONObjectWithData:data_params
  110. options:NSJSONReadingMutableLeaves
  111. error:nil] copyItems:true];
  112. NSString * str_actions =[self.data.pagedata[indexPath.row] valueForKey:@"action"];
  113. NSArray* actions = [str_actions componentsSeparatedByString:@","];
  114. NSString* function_name = [self.data.pagedata[indexPath.row] valueForKey:@"module_name"];
  115. DetailTabBarController *detailViewController=[[DetailTabBarController alloc] init:function_name actions:actions params:params];
  116. // DetailTabBarController *detailViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"DetailTabBarController"];
  117. // detailViewController.function_name = self.function_name;
  118. // detailViewController.actions = self.actions;
  119. // detailViewController.params =params;
  120. [self.navigationController pushViewController:detailViewController animated:YES];
  121. }
  122. - (UITableViewCellEditingStyle)tableView:(UITableView *)tv editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  123. return UITableViewCellEditingStyleDelete;
  124. //不能是UITableViewCellEditingStyleNone
  125. }
  126. - (void)tableView:(UITableView *)tableView
  127. commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  128. sqlite3* db = [ApexMobileDB get_db];
  129. NSString* sql = [NSString stringWithFormat:@"delete from favorites where _id=%@",[self.data.pagedata[indexPath.row] valueForKey:@"_id"]];
  130. [ApexMobileDB execSql:sql db:db];
  131. sqlite3_close(db);
  132. [self.data.pagedata removeObjectAtIndex:indexPath.row];
  133. [tableView reloadData];
  134. }
  135. @end