ContactListViewController.m 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. //
  2. // ContactListViewController.m
  3. // RedAnt ERP Mobile
  4. //
  5. // Created by Ray on 8/29/15.
  6. // Copyright (c) 2015 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "ContactListViewController.h"
  9. #import "RANetwork.h"
  10. #import "ContactListTableViewCell.h"
  11. #import "CustomerEditViewController.h"
  12. #import "ContactAdvanceSearchViewController.h"
  13. #define DEF_CELL_HEIGHT 44
  14. //#define DEF_TABLE_HEIGHT 44
  15. //#define LINE_WIDTH 0
  16. //#define CELL_MARGIN 0
  17. #define LABEL_MARGIN 15
  18. @interface ContactListViewController ()
  19. @property (nonatomic,strong) NSOperationQueue *dataOperationQueue;
  20. @end
  21. @implementation ContactListViewController
  22. - (NSOperationQueue *)dataOperationQueue {
  23. if (!_dataOperationQueue) {
  24. _dataOperationQueue = [[NSOperationQueue alloc] init];
  25. _dataOperationQueue.maxConcurrentOperationCount = 1;
  26. }
  27. return _dataOperationQueue;
  28. }
  29. - (void)awakeFromNib {
  30. self.edit_icon = true;
  31. self.assig_icon = true;
  32. self.reset_icon=false;
  33. }
  34. -(void) logout
  35. {
  36. self.offset = 0;
  37. self.keywords = nil;
  38. self.searchBar.text = nil;
  39. [self.content_data removeAllObjects];
  40. NSLog(@"%s removeAllObjects",__func__);
  41. }
  42. - (void)viewDidLoad {
  43. [super viewDidLoad];
  44. self.label_net_err.layer.borderColor = [UIColor darkGrayColor].CGColor;
  45. self.label_net_err.layer.borderWidth = 2.0;
  46. self.label_net_err.layer.cornerRadius=15;
  47. self.label_net_err.layer.masksToBounds=true;
  48. self.edgesForExtendedLayout = UIRectEdgeNone;
  49. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(refresh_btn_click:)];
  50. // tap.minimumPressDuration = 0.8; //定义按的时间
  51. [self.label_net_err addGestureRecognizer:tap];
  52. // Do any additional setup after loading the view.
  53. self.offset = 0;
  54. self.limit = 25;
  55. self.content_data = [[NSMutableArray alloc]init];
  56. UIRefreshControl *ref = [[UIRefreshControl alloc]init];
  57. ref.tag = 201 ;
  58. ref.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  59. ref.tintColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  60. // ref.hidden = true;
  61. [ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged];
  62. [self.table addSubview:ref];
  63. self.reset = false;
  64. //// UILabel* titleLabel= [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 100
  65. //
  66. // , 44)];
  67. // titleLabel.text=@"Contacts";
  68. // // titleLabel.backgroundColor =[UIColor yellowColor];
  69. // titleLabel.textAlignment=NSTextAlignmentCenter;
  70. // // [titleLabel sizeToFit];
  71. //
  72. // //将搜索条放在一个UIView上
  73. // // UIView *searchView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1768, 44)];
  74. //
  75. //
  76. //
  77. //
  78. // searchView.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleWidth;
  79. //
  80. // titleLabel.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
  81. // titleLabel.center=searchView.center;
  82. //
  83. //
  84. //
  85. // self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(searchView.frame.size.width-200.0f-8.0f,0.0f,200.0f,44.0f)];
  86. // self.searchBar.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
  87. // self.searchBar.delegate = self;
  88. // [self.searchBar setPlaceholder:@"name/address"];
  89. // //searchBar.barTintColor=[UIColor clearColor];
  90. // self.searchBar.searchBarStyle=UISearchBarStyleMinimal;
  91. // [searchView addSubview:titleLabel];
  92. // [searchView addSubview:self.searchBar];
  93. //
  94. // self.navigationItem.titleView = searchView;
  95. // NSMutableArray * items = [[NSMutableArray alloc]init];
  96. //
  97. //
  98. // UIBarButtonItem *Btnback = nil;//[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onSaveClick:)];
  99. //
  100. // Btnback = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
  101. // style:UIBarButtonItemStylePlain
  102. // target:self
  103. // action:@selector(onBack:)];
  104. //
  105. //
  106. // UIBarButtonItem *btnAS = nil;
  107. // btnAS =[[UIBarButtonItem alloc] initWithTitle:@"Advanced search"
  108. // style:UIBarButtonItemStylePlain
  109. // target:self
  110. // action:@selector(onASearch:)];
  111. //
  112. //
  113. //
  114. //
  115. // //savebtn setBackgroundImage:(nullable UIImage *) forState:<#(UIControlState)#> style:<#(UIBarButtonItemStyle)#> barMetrics:<#(UIBarMetrics)#>
  116. // //[savebtn setImage:[UIImage imageNamed:@"save"] ];
  117. //
  118. //
  119. // /*
  120. // UIBarMetricsDefault,
  121. // UIBarMetricsCompact,
  122. // UIBarMetricsDefaultPrompt = 101, // Applicable only in bars with the prompt property, such as UINavigationBar and UISearchBar
  123. // UIBarMetricsCompactPrompt
  124. //
  125. // */
  126. // // [savebtn setBackgroundImage:[UIImage imageNamed:@"save"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
  127. // // UIBarButtonItem *item1 = [[UIBarButtonItem alloc] init];
  128. // // item1.title = @"item1";
  129. // //// item1.image = [UIImage imageNamed:@"rect_setting"];
  130. // // UIBarButtonItem *item2 = [[UIBarButtonItem alloc] init];
  131. // //// item2.image = [UIImage imageNamed:@"rect_about"];
  132. // // item2.title = @"item2";
  133. //
  134. //
  135. // UIBarButtonItem *fixedItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
  136. // fixedItem.width = 20.0f;
  137. //
  138. //// AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  139. ////
  140. //// if(appDelegate.user_type ==USER_ROLE_EMPLOYEE)
  141. //
  142. // [items addObject:Btnback];
  143. //
  144. // [items addObject:fixedItem];
  145. //
  146. // [items addObject:btnAS];
  147. // // [items addObject:item2];
  148. // self.navigationItem.leftBarButtonItems=items;
  149. self.offset = 0;
  150. [self.content_data removeAllObjects];
  151. NSLog(@"%s removeAllObjects",__func__);
  152. // [self loadpage];
  153. }
  154. -(void)manually_refresh
  155. {
  156. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:201];
  157. if(self.isrefreshing)
  158. {
  159. [reF endRefreshing];
  160. return;
  161. }
  162. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Refreshing"];
  163. if ([self respondsToSelector:@selector(ReloadData)])
  164. [self performSelector:@selector(ReloadData) withObject:nil afterDelay:1];
  165. }
  166. -(void)ReloadData
  167. {
  168. self.keywords=self.searchBar.text;
  169. self.offset = 0;
  170. [self.content_data removeAllObjects];
  171. NSLog(@"%s removeAllObjects",__func__);
  172. [self.table reloadData];
  173. NSLog(@"%s reloadData",__FUNCTION__);
  174. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:201];
  175. [reF endRefreshing];
  176. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  177. [self loadpage];
  178. // [self loadpage];
  179. }
  180. - (void)didReceiveMemoryWarning {
  181. [super didReceiveMemoryWarning];
  182. // Dispose of any resources that can be recreated.
  183. }
  184. -(void) viewWillAppear:(BOOL)animated
  185. {
  186. [super viewWillAppear:animated];
  187. // [[self navigationController] setNavigationBarHidden:NO animated:NO];
  188. // self.offset = 0;
  189. // [self.content_data removeAllObjects];
  190. // [self loadpage];
  191. [[self navigationController] setNavigationBarHidden:!self.showNavibar animated:NO];
  192. }
  193. -(void) reload_container_getdata:(bool) update_data
  194. {
  195. [super reload_container_getdata:update_data];
  196. if(update_data)
  197. {
  198. self.offset = 0;
  199. [self.content_data removeAllObjects];
  200. NSLog(@"%s removeAllObjects",__func__);
  201. [self reload_data];
  202. }
  203. else
  204. {
  205. [self.table reloadData];
  206. NSLog(@"%s reloadData",__FUNCTION__);
  207. }
  208. }
  209. - (void)reRefreshView {
  210. [self.table reloadData];
  211. }
  212. -(void) operation_loadpage
  213. {
  214. if (self.dataOperationQueue.operationCount > 1) {
  215. return;
  216. }
  217. dispatch_async(dispatch_get_main_queue(), ^{
  218. if(self.isrefreshing)
  219. return;
  220. self.label_net_err.hidden=true;
  221. self.isrefreshing=true;
  222. if(self.offset==0)
  223. {
  224. self.table.hidden = true;
  225. // [self.params setValue:[NSString stringWithFormat:@"%ld",self.offset] forKey:@"offset"];
  226. self.mum.center = self.view.center;
  227. self.mum.hidden = false;
  228. [self.mum startAnimating];
  229. }
  230. // self.btnrefresh.enabled = false;
  231. //
  232. // NSTimeInterval animationDuration = 0.30f;
  233. // [UIView beginAnimations:@"ResizeView" context:nil];
  234. // [UIView setAnimationDuration:animationDuration];
  235. // self.prepageButton.enabled=false;
  236. // self.nextpageButton.enabled=false;
  237. // self.mum.hidden = false;
  238. // self.grid.hidden = true;
  239. // self.mum.hidden = false;
  240. // self.tableview.hidden = true;
  241. // [UIView commitAnimations];
  242. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  243. NSMutableDictionary* content=nil;
  244. content=[[RANetwork request_ContactList:self.offset limit : self.limit keywords:self.keywords type:self.contact_type adv_search:self.adv_search] mutableCopy];
  245. dispatch_async(dispatch_get_main_queue(), ^{
  246. [self.mum stopAnimating];
  247. self.isrefreshing=false;
  248. if (self.dataOperationQueue.operationCount > 1) {
  249. return ;
  250. }
  251. if([[content valueForKey:@"result"] intValue]==2)
  252. {
  253. self.table.hidden =false;
  254. int count = [[content valueForKey:@"count" ] intValue] ;
  255. self.offset += count;
  256. for(int i=0;i<count;i++)
  257. {
  258. NSDictionary* objmsg = [content objectForKey:[NSString stringWithFormat:@"item_%d",i]];
  259. [self.content_data addObject:[NSMutableDictionary dictionaryWithDictionary:objmsg]];
  260. }
  261. if(count<self.limit)
  262. {
  263. // int i =self.btnrefresh.state;
  264. self.load_more_hint=@"All loaded";
  265. // self.btnrefresh.titleLabel.text =NSLocalizedString(@"no_more", nil);
  266. // [self.btnrefresh setTitle:@"No more" forState:UIControlStateNormal];
  267. }
  268. else
  269. {
  270. self.load_more_hint=@"Load more...";
  271. // self.btnrefresh.enabled = true;
  272. // self.btnrefresh.titleLabel.text =NSLocalizedString(@"load_more",nil);
  273. // [self.btnrefresh setTitle:@"Load more" forState:UIControlStateNormal];
  274. }
  275. [self.table reloadData ];
  276. NSLog(@"%s reloadData",__FUNCTION__);
  277. }
  278. else if([[content valueForKey:@"result"] intValue]==RESULT_NET_ERROR &&self.offset==0)
  279. {
  280. self.label_net_err.hidden=false;
  281. self.table.hidden = true;
  282. }
  283. else
  284. {
  285. [RAUtils message_alert:[content valueForKey:@"err_msg"] title:@"Loading Contact List" controller:self] ;
  286. }
  287. // NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  288. // bool autologin =[defaults boolForKey:@"autologin"];
  289. // if(!autologin)
  290. // {
  291. // TabBarController * viewController =[self.storyboard instantiateViewControllerWithIdentifier:@"LoginTabBar"];
  292. // [self presentViewController:viewController animated:YES completion:^{
  293. //
  294. // }];
  295. // }
  296. self.isrefreshing=false;
  297. });
  298. });
  299. });
  300. }
  301. -(void) loadpage {
  302. __weak typeof(self) weakself = self;
  303. NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
  304. if (weakself) {
  305. __strong typeof(weakself) strongself = weakself;
  306. [strongself operation_loadpage];
  307. }
  308. }];
  309. [self.dataOperationQueue addOperation:operation];
  310. }
  311. - (void)loadmore
  312. {
  313. // self.btnrefresh.enabled = NO;
  314. // self.btnrefresh.titleLabel.text =NSLocalizedString(@"loading",nil);
  315. // [self.btnrefresh setTitle:@"Loading..." forState:UIControlStateNormal];
  316. [self loadpage];
  317. }
  318. - (IBAction)onBack:(UIBarButtonItem *)sender {
  319. if(self.onCancel)
  320. self.onCancel();
  321. [self.navigationController popViewControllerAnimated:(false)];
  322. }
  323. - (IBAction)onASearch:(UIBarButtonItem *)sender {
  324. ContactAdvanceSearchViewController * vc =[ self.storyboard instantiateViewControllerWithIdentifier:@"ContactAdvanceSearchViewController"];
  325. vc.url_type = URL_REMOTE;
  326. vc.request_url=URL_CUSTOMER_ADV_SEARCH;
  327. NSMutableDictionary * params= [[NSMutableDictionary alloc] init];
  328. vc.params = params;
  329. vc.data_init = self.adv_search;
  330. // vc.update_order = true;
  331. // vc.contactId=contactid;
  332. // vc.delegate = self;
  333. vc.returnValue = ^(NSMutableDictionary* value){
  334. self.adv_search = value;
  335. self.keywords=nil;
  336. self.offset = 0;
  337. [self.content_data removeAllObjects];
  338. NSLog(@"%s removeAllObjects",__func__);
  339. [self loadpage];
  340. // appDelegate.contact_id=[value valueForKey:@"customer_cid"];
  341. // appDelegate.customerInfo = value;
  342. };
  343. [self.navigationController pushViewController:vc animated:true];
  344. }
  345. - (IBAction)onNewCustomerClick:(id)sender {
  346. NSMutableDictionary * params= [[NSMutableDictionary alloc] init];
  347. CustomerEditViewController * cuseditVC =[ self.storyboard instantiateViewControllerWithIdentifier:@"CustomerEditViewController"];
  348. cuseditVC.bnewcustomer=true;
  349. cuseditVC.url_type = URL_REMOTE;
  350. cuseditVC.request_url=URL_NEW_CUSTOMER;
  351. cuseditVC.params = params;
  352. cuseditVC.delegate=self;
  353. //
  354. // if(checked.count==count)
  355. // {
  356. // orderinfoVC.have_tail = true
  357. // }
  358. [[self navigationController] setNavigationBarHidden:NO animated:NO];
  359. [self.navigationController pushViewController:cuseditVC animated:false];
  360. }
  361. - (IBAction)resetContactId:(id)sender {
  362. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  363. appDelegate.contact_id=nil;
  364. appDelegate.customerInfo = nil;
  365. // appDelegate.contact_name =nil;
  366. if(self.onReset)
  367. self.onReset();
  368. [self.navigationController popViewControllerAnimated:(false)];
  369. }
  370. - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
  371. {
  372. DebugLog(@"!!!!!!!!!!!!!!!!!!!!!route!!!!!!!!!!!!!!!!!!!!!");
  373. [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
  374. [self.table reloadData];
  375. NSLog(@"%s reloadData",__FUNCTION__);
  376. // CGSize contentsize = self.scroll.contentSize;
  377. // contentsize.height =330+2 * 230+29+20+20 +1 * 230+29+20+20 ;
  378. // UIInterfaceOrientation orientation =[UIApplication sharedApplication].statusBarOrientation;
  379. // if(orientation == UIDeviceOrientationLandscapeLeft ||orientation == UIDeviceOrientationLandscapeRight)
  380. // {
  381. // contentsize.height+=250;
  382. // }
  383. //
  384. // self.scroll.contentSize = contentsize;
  385. // // CGRect frame = self.view.frame;
  386. // // [self.carouselController.view removeFromSuperview];
  387. // // wait(2000);
  388. // // self.carouselController = [[FPCarouselNonXIBViewController alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 400)];
  389. // // [self.view addSubview:self.carouselController.view];
  390. // // int i = self.view.subviews.count;
  391. // // int b = 0;
  392. // // [self.carouselController updateFrame:CGRectMake(0, 0, self.view.frame.size.width, 400)];
  393. }
  394. //
  395. //-(void)changeCell
  396. //{
  397. // // UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  398. // // reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"refreshing"];
  399. // [self performSelector:@selector(loadmore) withObject:nil afterDelay:1];
  400. // DebugLog(@"refresh!!!!!!!!");
  401. //
  402. //}
  403. - (IBAction)onAssignClick:(id)sender {
  404. UIButton* btn = (UIButton*)sender;
  405. UITableViewCell *cell = (UITableViewCell *)btn.superview.superview;;
  406. NSIndexPath *indexPath = [self.table indexPathForCell:cell];
  407. DebugLog(@"indexPath is = %i",indexPath.row);
  408. NSString* contactid=[self.content_data[indexPath.row] valueForKey:@"contact_id"] ;
  409. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"Getting Contact information"];
  410. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  411. NSDictionary* editor_json = [RANetwork request_CustomerInfo:contactid];
  412. dispatch_async(dispatch_get_main_queue(), ^{
  413. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  414. if([[editor_json valueForKey:@"result"] intValue]==2)
  415. {
  416. __block NSMutableDictionary* data_init = [[editor_json objectForKey:@"customerInfo" ] mutableCopy];
  417. //---------------- construct customer_contact----------------------
  418. NSString* customer_first_name = [data_init valueForKey:@"customer_first_name"];
  419. if(customer_first_name==nil)
  420. customer_first_name=@"";
  421. NSString* customer_last_name= [data_init valueForKey:@"customer_last_name"];
  422. if(customer_last_name==nil)
  423. customer_last_name=@"";
  424. NSString* customer_contact = [NSString stringWithFormat:@"%@ %@",customer_first_name,customer_last_name];
  425. customer_contact=[customer_contact stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  426. [data_init setValue:customer_contact forKey:@"customer_contact"];
  427. data_init[@"is_subaction"]=self.is_subaction;
  428. data_init[@"subaction_tag"]= self.subaction_tag;
  429. [self.navigationController popToRootViewControllerAnimated:false];
  430. [self.table reloadData];
  431. NSLog(@"%s reloadData",__FUNCTION__);
  432. if(self.returnValue)
  433. self.returnValue(data_init,self.from);
  434. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  435. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  436. // NSDictionary* editor_json = [iSalesNetwork update_OrderCustomer:appDelegate.order_code customerinfo:data_init];
  437. //
  438. // dispatch_async(dispatch_get_main_queue(), ^{
  439. //
  440. //
  441. // if([[editor_json valueForKey:@"result"] intValue]==2)
  442. // {
  443. //
  444. //// [self.navigationController popViewControllerAnimated:false];
  445. //// if(self.returnValue)
  446. //// {
  447. ////
  448. //// self.returnValue(data_init);
  449. //// }
  450. //
  451. //
  452. //
  453. // }
  454. // else
  455. // {
  456. // [RAUtils message_alert:[editor_json valueForKey:@"err_msg"] title:@"Update Order Customer Infomation" controller:self] ;
  457. // }
  458. //
  459. //
  460. //
  461. // });
  462. // });
  463. }
  464. else
  465. {
  466. [RAUtils message_alert:[editor_json valueForKey:@"err_msg"] title:@"Loading Contact Information" controller:self] ;
  467. // self.btn_assign.enabled=false;
  468. }
  469. });
  470. });
  471. }
  472. #pragma mark - Table view data source
  473. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
  474. {
  475. if(indexPath.row==self.content_data.count)
  476. return 44;
  477. NSString* value = [self.content_data[indexPath.row] valueForKey:@"name"];
  478. float width = tableView.frame.size.width;
  479. width-=74;
  480. CGSize constraintkey = CGSizeMake(width-2*LABEL_MARGIN, 10.0f);//key label width is 40% cell width;
  481. // CGSize constraintval = CGSizeMake(width*0.6-2*LABEL_MARGIN, 20000.0f);//val label width is 60% cell width;
  482. // NSDictionary* item = [self.content get_item_seg:indexPath.section row:indexPath.row];
  483. // NSString* key =[self.content_data valueForKey:@"order_info"];
  484. // [cell.keyLabel sizeToFit];
  485. // NSString* val=[item_json valueForKey:@"val"] ;
  486. // if([val isEqual:[NSNull null]])
  487. // val=@"";
  488. // if(val==nil)
  489. // val=@"";
  490. // if([val isEqualToString:@"null"])
  491. // val=@"";
  492. CGRect frame;
  493. frame.size = constraintkey;
  494. frame.origin.x=0;
  495. frame.origin.y=0;
  496. RTLabel* rtlabel = [[RTLabel alloc] initWithFrame:frame];
  497. // [rtlabel setLineBreakMode: RTTextLineBreakModeWordWrapping];
  498. // rtlabel.lineSpacing = 20.0;
  499. [rtlabel setText: value];
  500. CGSize optimumSize = [rtlabel optimumSize];
  501. // DebugLog(@"%@",key);
  502. // [rtlabel sizeThatFits:constraintkey];
  503. // rtlabel frameHeight:<#(CTFrameRef)#>
  504. // rtlabel
  505. // [rtlabel sizeToFit];
  506. //CGSize newsize= rtlabel.frame.size;
  507. // CGSize sizeval=rtlabel.optimumSize;
  508. // CGSize sizekey = [key sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:constraintkey lineBreakMode:NSLineBreakByWordWrapping];
  509. // CGSize sizeval = [val sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:constraintval lineBreakMode:NSLineBreakByWordWrapping];
  510. float height = optimumSize.height;
  511. height = MAX(height+2*LABEL_MARGIN, DEF_CELL_HEIGHT);
  512. return height;
  513. }
  514. //- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
  515. //{
  516. // NSDictionary * item_json = [self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  517. // if([item_json objectForKey:@"combine"] == nil)
  518. // {
  519. // return 120;
  520. // }
  521. // else
  522. // return 150;
  523. //}
  524. //- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  525. //{
  526. // return 0;
  527. //}
  528. //- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  529. //{
  530. // return 0;
  531. //}
  532. //- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
  533. //{
  534. // UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  535. // return myView;
  536. //
  537. //}
  538. //- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  539. //{
  540. // UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  541. //// myView.backgroundColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  542. //// UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 90, 22)];
  543. //// titleLabel.textColor=[UIColor whiteColor];
  544. //// titleLabel.backgroundColor = [UIColor clearColor];
  545. //// if(section==0)
  546. //// titleLabel.text=NSLocalizedString(@"display_items", nil);
  547. //// else
  548. //// titleLabel.text=NSLocalizedString(@"hide_items", nil);
  549. //// [titleLabel sizeToFit];
  550. //// [myView addSubview:titleLabel];
  551. ////
  552. // return myView;
  553. //}
  554. //
  555. //- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  556. // if(section==0)
  557. // return nil;
  558. // else
  559. // return @"detail section";
  560. //}
  561. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  562. {
  563. return 1;
  564. }
  565. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  566. {
  567. // NSDictionary * item_json = [self.content_data objectForKey:@"items"];
  568. if( self.content_data.count==0 || /*self.refresh_type == REFRESH_DATA ||*/ self.isrefreshing) //
  569. return 0;
  570. return self.content_data.count+1;
  571. }
  572. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  573. {
  574. // if(tableView==self.itemListTable)
  575. // {
  576. // DebugLog(@"row %ld",(long)indexPath.row);
  577. if(indexPath.row==self.content_data.count)
  578. {
  579. UITableViewCell *moreCell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"mores"];
  580. UILabel *moreDataLabel=[[UILabel alloc] init];
  581. moreDataLabel.tag=200;
  582. moreDataLabel.text=self.load_more_hint;
  583. [moreDataLabel setFont:[UIFont systemFontOfSize:14.0f]];
  584. [moreDataLabel setTextAlignment:NSTextAlignmentCenter];
  585. moreDataLabel.frame=CGRectMake(0, 10, self.table.bounds.size.width, 20);
  586. [moreDataLabel setBackgroundColor:[UIColor clearColor]];
  587. // [moreDataLabel addTarget:self action:@selector(changeCell) forControlEvents:UIControlEventValueChanged];
  588. moreCell.backgroundColor = [UIColor whiteColor];
  589. [moreCell.contentView addSubview:moreDataLabel];
  590. if([self.load_more_hint isEqualToString:@"Load more..."])
  591. [self performSelector:@selector(loadmore) withObject:nil afterDelay:1];
  592. // DebugLog(@"更多...");
  593. return moreCell;
  594. }
  595. else
  596. {
  597. // UITableViewCell *cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ContactListCell"];
  598. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  599. NSString *CellIdentifier = @"ContactListTableViewCell";
  600. ContactListTableViewCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  601. NSString* value = [self.content_data[indexPath.row] valueForKey:@"name"];
  602. cell.rtlabel.text = value;
  603. cell.btn_assign.hidden = !(self.assig_icon&&(appDelegate.contact_id.length==0));
  604. /* NSString* name= [self.content_data[indexPath.row] valueForKey:@"name"];
  605. NSString* address= [self.content_data[indexPath.row] valueForKey:@"address"];
  606. cell.detailTextLabel.text=address;
  607. cell.textLabel.text=name;
  608. //cell.sep
  609. // [UIColor rg]*/
  610. cell.backgroundColor = [UIColor whiteColor];
  611. // cell.separator.backgroundColor = [UIColor grayColor];
  612. return cell;
  613. }
  614. }
  615. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  616. {
  617. if (indexPath.row>self.content_data.count-1) {
  618. return;
  619. }
  620. NSString* contactid=[self.content_data[indexPath.row] valueForKey:@"contact_id"] ;
  621. bool editable =[[self.content_data[indexPath.row] valueForKey:@"can_update"]boolValue] ;
  622. CustomerInfoViewController * vc =[ self.storyboard instantiateViewControllerWithIdentifier:@"CustomerInfoViewController"];
  623. vc.url_type = URL_LOCAL;
  624. vc.request_url=LOCAL_URL_CUSTOMER_INFO_EDITOR;
  625. vc.update_order = self.update_order;
  626. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  627. if(self.ui_type==CONTACT_LIST)
  628. {
  629. vc.edit_icon = self.edit_icon&&editable&&appDelegate.can_update_contact_info;
  630. vc.assig_icon = self.assig_icon&&(appDelegate.contact_id.length==0);
  631. vc.reset_icon = self.reset_icon&&(appDelegate.contact_id.length!=0);
  632. }
  633. else
  634. {
  635. vc.edit_icon = self.edit_icon&&editable&&appDelegate.can_update_contact_info;
  636. vc.assig_icon = self.assig_icon;
  637. vc.reset_icon = self.reset_icon;
  638. }
  639. // NSMutableDictionary * params= [[NSMutableDictionary alloc] init];
  640. // // NSString * string = [checked componentsJoinedByString:@","];
  641. // // [params setValue:string forKey:@"cart2Checkbox"];
  642. //
  643. //
  644. //
  645. //
  646. // addressVC.params = params;
  647. vc.contactId=contactid;
  648. // vc.delegate = self;
  649. vc.from = indexPath;
  650. // orderinfoVC.params = params;
  651. vc.returnValue = ^(NSMutableDictionary* value){
  652. value[@"is_subaction"]=self.is_subaction;
  653. value[@"subaction_tag"]= self.subaction_tag;
  654. [self.navigationController popViewControllerAnimated:false];
  655. if(self.returnValue)
  656. self.returnValue(value,self.from);
  657. };
  658. [self.navigationController pushViewController:vc animated:true];
  659. /*
  660. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  661. appDelegate.contact_id=contactid;
  662. appDelegate.contact_name =[self.content_data[indexPath.row] valueForKey:@"name"];
  663. */
  664. // [self.navigationController popViewControllerAnimated:(false)];
  665. // NSString* orderid= [self.content_data[indexPath.row] valueForKey:@"order_id"];
  666. // OrderDetailViewController* dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"OrderDetailViewController" ];
  667. //
  668. // dvc.orderid=orderid;
  669. // [self.navigationController pushViewController:dvc animated:true];
  670. //
  671. //
  672. }
  673. /*
  674. #pragma mark - Navigation
  675. // In a storyboard-based application, you will often want to do a little preparation before navigation
  676. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  677. // Get the new view controller using [segue destinationViewController].
  678. // Pass the selected object to the new view controller.
  679. }
  680. */
  681. #pragma mark - searchBar delegate;
  682. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
  683. {
  684. self.keywords=searchBar.text;
  685. self.offset = 0;
  686. [self.content_data removeAllObjects];
  687. NSLog(@"%s removeAllObjects",__func__);
  688. self.adv_search=nil;
  689. [self loadpage];
  690. DebugLog(@"search");
  691. }
  692. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
  693. {
  694. if([searchText isEqualToString:@""] && !self.reset)
  695. {
  696. DebugLog(@"clear");
  697. self.reset=true;
  698. self.keywords=nil;
  699. self.offset = 0;
  700. [self.content_data removeAllObjects];
  701. NSLog(@"%s removeAllObjects",__func__);
  702. self.adv_search=nil;
  703. [self loadpage];
  704. // [self loadpage];
  705. }
  706. else
  707. self.reset = false;
  708. }
  709. #pragma mark - RA_NOTIFICAITON
  710. -(void) refresh_ui
  711. {
  712. [self.table reloadData];
  713. }
  714. -(void) reload_data
  715. {
  716. self.offset = 0;
  717. [self.content_data removeAllObjects];
  718. [self.table reloadData];
  719. [self loadpage];
  720. }
  721. @end