// // FavoritesViewController.m // Apex Mobile // // Created by Ray on 14-4-25. // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved. // #import "FavoritesViewController.h" @interface FavoritesViewController () @end @implementation FavoritesViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (BOOL)shouldAutorotate { return YES; } - (UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationPortrait; } - (void)viewDidLoad { [super viewDidLoad]; self.data =[[FavoritesData alloc]init]; [self.tableview reloadData]; self.tableview.tableFooterView = [UIView new]; UIBarButtonItem *clearItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"ic_clear"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(clearClick:)]; self.navigationItem.rightBarButtonItem = clearItem; } -(void)viewWillAppear:(BOOL)animated { } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)clearClick:(id)sender { __weak typeof(self) weakSelf = self; [self.data deleteAll:^(BOOL success){ if (success) { dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf.tableview reloadData]; }); } }]; } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.data get_count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { DebugLog(@"cellForRowAtIndexPath"); NSString *CellIdentifier = @"cell_item_history"; CellItemHistory *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; NSString* name = [self.data.pagedata[indexPath.row] valueForKey:@"name"]; NSString* create_time = [self.data.pagedata[indexPath.row] valueForKey:@"create_time"]; NSString *module_name = [self.data.pagedata[indexPath.row] valueForKey:@"module_name"]; // Cargo Tracking if ([module_name isEqualToString:@"Ocean Booking"]) { cell.icon.image = [[UIImage imageNamed:@"mode_booking"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; } else if ([module_name isEqualToString:@"Container detail"]) { cell.icon.image = [[UIImage imageNamed:@"mode_container"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; } else if ([module_name isEqualToString:@"Ocean B/L info."]) { cell.icon.image = [[UIImage imageNamed:@"mode_bl"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; } else if ([module_name isEqualToString:@"Cargo Tracking"]) { cell.icon.image = [[UIImage imageNamed:@"mode_cargo_tracking"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; } else { cell.icon.image = [[UIImage imageNamed:@"mode_document"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; } cell.name.text = name; [cell.name sizeToFit]; cell.time.text =create_time; [cell.time sizeToFit]; // cell.imageView.image = [UIImage imageNamed:[self.toolsinfo[indexPath.row] valueForKey:@"img"]]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithDictionary:self.params copyItems:true]; NSString* str_params =[self.data.pagedata[indexPath.row] valueForKey:@"params"]; NSData* data_params = [str_params dataUsingEncoding:NSUTF8StringEncoding]; NSMutableDictionary* params = [[NSMutableDictionary alloc] initWithDictionary:[NSJSONSerialization JSONObjectWithData:data_params options:NSJSONReadingMutableLeaves error:nil] copyItems:true]; NSString * str_actions =[self.data.pagedata[indexPath.row] valueForKey:@"action"]; NSArray* actions = [str_actions componentsSeparatedByString:@","]; NSString* function_name = [self.data.pagedata[indexPath.row] valueForKey:@"module_name"]; DetailTabBarController *detailViewController=[[DetailTabBarController alloc] init:function_name actions:actions params:params]; // DetailTabBarController *detailViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"DetailTabBarController"]; // detailViewController.function_name = self.function_name; // detailViewController.actions = self.actions; // detailViewController.params =params; [self.navigationController pushViewController:detailViewController animated:YES]; } - (UITableViewCellEditingStyle)tableView:(UITableView *)tv editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleDelete; //不能是UITableViewCellEditingStyleNone } - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { sqlite3* db = [ApexMobileDB get_db]; NSString* sql = [NSString stringWithFormat:@"delete from favorites where _id=%@",[self.data.pagedata[indexPath.row] valueForKey:@"_id"]]; [ApexMobileDB execSql:sql db:db]; sqlite3_close(db); [self.data.pagedata removeObjectAtIndex:indexPath.row]; [tableView reloadData]; } @end