ContactListViewController.m 31 KB

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