DetailViewController.m 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618
  1. //
  2. // DetailViewController.m
  3. // RedAnt ERP Mobile
  4. //
  5. // Created by Ray on 7/25/15.
  6. // Copyright (c) 2015 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "DetailViewController.h"
  9. #import "DetailHeaderCell.h"
  10. #import "DetailImageCell.h"
  11. #import "iSalesNetwork.h"
  12. #import "RTLabel.h"
  13. #import "DetailKVCell.h"
  14. #import "LineView.h"
  15. //#import "SelectorViewController.h"
  16. #import "RAUtils.h"
  17. #import "MainViewController.h"
  18. #import "QRCodeGenerator.h"
  19. #define DEF_CELL_HEIGHT 44
  20. #define DEF_TABLE_HEIGHT 44
  21. #define LINE_WIDTH 0
  22. #define CELL_MARGIN 0
  23. #define LABEL_MARGIN 10
  24. #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
  25. #define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
  26. #define NavColor ([UIColor colorWithRed:237/255.0 green:20/255.0f blue:91/255.0f alpha:1.0f])
  27. @interface DetailViewController ()
  28. @property (nonatomic,strong) UIBezierPath *path;
  29. @end
  30. @implementation DetailViewController
  31. {
  32. CALayer *_layer;
  33. }
  34. -(void) reload_container_getdata:(bool) update_data
  35. {
  36. [super reload_container_getdata:update_data];
  37. if(update_data)
  38. [self reload];
  39. else
  40. {
  41. [self.detailTable reloadData];
  42. // [self refresh_total];
  43. }
  44. }
  45. -(void)manually_refresh
  46. {
  47. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:300];
  48. if(self.isrefreshing)
  49. {
  50. [reF endRefreshing];
  51. return;
  52. }
  53. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Refreshing"];
  54. if ([self respondsToSelector:@selector(ReloadData)])
  55. [self performSelector:@selector(ReloadData) withObject:nil afterDelay:1];
  56. // DebugLog(@"refresh!!!!!!!!");
  57. }
  58. -(void) ReloadData
  59. {
  60. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:300];
  61. [reF endRefreshing];
  62. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  63. [self reload];
  64. }
  65. -(void) reload
  66. {
  67. dispatch_async(dispatch_get_main_queue(), ^{
  68. if(self.isrefreshing)
  69. return;
  70. self.label_net_err.hidden=true;
  71. self.detailTable.hidden = true;
  72. self.isrefreshing=true;
  73. self.mum.center = self.view.center;
  74. self.mum.hidden = false;
  75. [self.mum startAnimating];
  76. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  77. NSDictionary* detail_json = [iSalesNetwork request_ItemDetail:self.product_id model_name:self.model_name category_id:self.category_id use_name:self.use_model_name];
  78. dispatch_async(dispatch_get_main_queue(), ^{
  79. [self.mum stopAnimating];
  80. self.detail_data = [detail_json mutableCopy];
  81. self.product_id = [NSString stringWithFormat:@"%d",[[detail_json valueForKey:@"product_id"] intValue] ];
  82. NSDictionary* imgsection = [detail_json objectForKey:@"img_section"];
  83. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  84. if(/*appDelegate.user_type==USER_ROLE_CUSTOMER*/true)
  85. self.quantity =[[imgsection valueForKey:@"stockUom"] intValue];
  86. else
  87. self.quantity =1;
  88. if(self.quantity==0)
  89. self.quantity=1;
  90. self.step=self.quantity;
  91. [self.detailTable reloadData];
  92. if([[detail_json valueForKey:@"result"] intValue]==2)
  93. {
  94. self.detailTable.hidden = false;
  95. NSString* model_name = self.detail_data[@"img_section"][@"model_name"];
  96. NSString* img_url = self.detail_data[@"img_section"][@"model_s_img"];
  97. NSMutableDictionary* item = [[NSMutableDictionary alloc]init];
  98. [item setValue:self.product_id forKey:@"product_id"];
  99. [item setValue:self.category_id forKey:@"category"];
  100. [item setValue:model_name forKey:@"fash_name"];
  101. [item setValue:img_url forKey:@"picture_path"];
  102. [RAUtils add_recent_model:item];
  103. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  104. // int count=[appDelegate.recent_model[@"count"] intValue];
  105. // bool bexist = false;
  106. // for(int i=0;i<count;i++)
  107. // {
  108. // NSMutableDictionary * mitem= appDelegate.recent_model[[NSString stringWithFormat:@"item_%d",i]];
  109. // if([mitem[@"product_id"] isEqualToString:self.product_id])
  110. // {
  111. // bexist=true;
  112. // break;
  113. // }
  114. // }
  115. //
  116. // if(!bexist)
  117. // {
  118. // [appDelegate.recent_model setObject:item forKey:[NSString stringWithFormat:@"item_%d",count]];
  119. // appDelegate.recent_model[@"count"]=[NSString stringWithFormat:@"%d",count+1];
  120. // }
  121. }
  122. else
  123. if([[detail_json valueForKey:@"result"] intValue]==RESULT_NET_ERROR)
  124. {
  125. self.detailTable.hidden = true;
  126. self.label_net_err.hidden=false;
  127. }
  128. else if([[detail_json valueForKey:@"result"] intValue]==RESULT_NO_MODEL)
  129. {
  130. self.detailTable.hidden = true;
  131. [RAUtils message_alert:[detail_json valueForKey:@"err_msg"] title:@"Model Detail"/*@"Loading Model Detail Failed."*/ controller:self] ;
  132. if(self.ispush)
  133. [ self.navigationController popViewControllerAnimated:true];
  134. }
  135. else
  136. {
  137. [RAUtils message_alert:[detail_json valueForKey:@"err_msg"] title:@"Model Detail"/*@"Loading Model Detail Failed."*/ controller:self] ;
  138. }
  139. self.isrefreshing=false;
  140. });
  141. });
  142. });
  143. }
  144. - (void)onBackClick:(UIButton *)sender {
  145. if(self.OnBack)
  146. {
  147. self.OnBack(self.add_cart,self.add_wish,self.update_data);
  148. }
  149. [self.navigationController popViewControllerAnimated:(false)];
  150. // [self.navigationController dismissViewControllerAnimated:true completion:^{
  151. // ;
  152. // }];
  153. }
  154. - (void)viewDidLoad {
  155. [super viewDidLoad];
  156. self.refresh_type = REFRESH_VIEW;
  157. UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  158. style:UIBarButtonItemStylePlain
  159. target:self
  160. action:@selector( onBackClick:)];
  161. self.navigationItem.leftBarButtonItem = backButton;
  162. // self.detailTable.style = UITableViewStylePlain;
  163. self.edgesForExtendedLayout = UIRectEdgeNone;
  164. if(self.ispush)
  165. {
  166. UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.000001)];
  167. [line setBackgroundColor:[UIColor clearColor]];
  168. self.detailTable.tableHeaderView = line;
  169. }
  170. //
  171. //
  172. // self.automaticallyAdjustsScrollViewInsets = false;
  173. self.step=1;
  174. self.quantity=1;
  175. UIRefreshControl *ref = [[UIRefreshControl alloc]init];
  176. ref.tag = 300 ;
  177. ref.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  178. ref.tintColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  179. // ref.hidden = true;
  180. [ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged];
  181. [self.detailTable addSubview:ref];
  182. self.label_net_err.layer.borderColor = [UIColor darkGrayColor].CGColor;
  183. self.label_net_err.layer.borderWidth = 2.0;
  184. self.label_net_err.layer.cornerRadius=15;
  185. self.label_net_err.layer.masksToBounds=true;
  186. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(refresh_btn_click:)];
  187. // tap.minimumPressDuration = 0.8; //定义按的时间
  188. [self.label_net_err addGestureRecognizer:tap];
  189. // Do any additional setup after loading the view.
  190. }
  191. - (void)viewWillAppear:(BOOL)animated
  192. {
  193. if(self.ispush)
  194. {
  195. [super viewWillAppear:animated];
  196. [[self navigationController] setNavigationBarHidden:NO animated:NO];
  197. }
  198. }
  199. - (void) viewWillDisappear:(BOOL)animated
  200. {
  201. if(self.ispush)
  202. [[self navigationController] setNavigationBarHidden:YES animated:NO];
  203. }
  204. - (void)didReceiveMemoryWarning {
  205. [super didReceiveMemoryWarning];
  206. // Dispose of any resources that can be recreated.
  207. }
  208. /*
  209. #pragma mark - Navigation
  210. // In a storyboard-based application, you will often want to do a little preparation before navigation
  211. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  212. // Get the new view controller using [segue destinationViewController].
  213. // Pass the selected object to the new view controller.
  214. }
  215. */
  216. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  217. //将page2设定成Storyboard Segue的目标UIViewController
  218. id page2 = segue.destinationViewController;
  219. //将值透过Storyboard Segue带给页面2的string变数
  220. [page2 setValue: _selector forKey:@"selector"];
  221. [page2 setValue: self forKey:@"selectordelegate"];
  222. }
  223. - (IBAction)onShareClick:(id)sender {
  224. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  225. //
  226. // [appDelegate test_onDecodedDataResult:@"371252"];
  227. // [self onDecodedData:@"3712524-235"];
  228. return;
  229. }
  230. -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
  231. {
  232. // [self.detailTable reloadData];
  233. // return;
  234. [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
  235. // NSMutableIndexSet *sectionToReload = [[NSMutableIndexSet alloc] init];
  236. // int section_count =[[self.detail_data valueForKey:@"detail_section_count"] intValue];
  237. //
  238. // for(int i=0;i<section_count;i++)
  239. // {
  240. // NSDictionary* section_json = [self.detail_data objectForKey:[NSString stringWithFormat:@"detail_%ld",(long)i]];
  241. //
  242. //
  243. //
  244. // NSString* type=[section_json valueForKey:@"type"] ;
  245. //
  246. // if([type isEqualToString:@"kv"])
  247. // {
  248. //
  249. // [sectionToReload addIndex:i+1];
  250. // }
  251. // }
  252. //
  253. //
  254. // [self.detailTable reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationNone];
  255. //
  256. [self.detailTable reloadData];
  257. }
  258. #pragma mark - Selector delegate
  259. -(void) selected:(NSString*) detail_id category:(NSString*) category
  260. {
  261. self.product_id = detail_id;
  262. self.use_model_name = false;
  263. [self reload];
  264. NSLog(@"color selected");
  265. }
  266. #pragma mark - Header cell delegate
  267. -(void) SelectorClicked:(UIView*) trigger
  268. {
  269. // int selcount = [[self.selector valueForKey:@"count"] intValue];
  270. int selector_count = [[self.selector valueForKey:@"count"] intValue];
  271. int current_sel = -1;
  272. for(int i=0;i<selector_count;i++)
  273. {
  274. NSDictionary* params_json=[[self.selector objectForKey:[NSString stringWithFormat:@"item_%d",i]] objectForKey:@"params"] ;
  275. // NSString* aaa=[[[params_json objectForKey:@"param_0"] valueForKey:@"val"] stringValue];
  276. if([[[[params_json objectForKey:@"param_0"] valueForKey:@"val"] stringValue] isEqualToString:self.product_id])
  277. {
  278. current_sel = i;
  279. }
  280. }
  281. // NSArray * arr = [[NSArray alloc] init];
  282. // arr = [NSArray arrayWithObjects:@"Hello 0", @"Hello 1", @"Hello 2", @"Hello 3", @"Hello 4", @"Hello 5", @"Hello 6", @"Hello 7", @"Hello 8", @"Hello 9",nil];
  283. // NSArray * arrImage = [[NSArray alloc] init];
  284. // arrImage = [NSArray arrayWithObjects:[UIImage imageNamed:@"apple.png"], [UIImage imageNamed:@"apple2.png"], [UIImage imageNamed:@"apple.png"], [UIImage imageNamed:@"apple2.png"], [UIImage imageNamed:@"apple.png"], [UIImage imageNamed:@"apple2.png"], [UIImage imageNamed:@"apple.png"], [UIImage imageNamed:@"apple2.png"], [UIImage imageNamed:@"apple.png"], [UIImage imageNamed:@"apple2.png"], nil];
  285. if(self.dropDown == nil)
  286. {
  287. CGFloat height = 70*selector_count;
  288. self.dropDown =[[NIDropDown alloc] showDropDown:self.detailTable based:(id)trigger height:height data:self.selector direction:@"down" current_sel:current_sel];
  289. //Capturing strongly warring;
  290. __block DetailViewController *brself= self;
  291. self.dropDown.selectChanged =^(long index){
  292. NSDictionary* item = [brself.selector objectForKey:[NSString stringWithFormat:@"item_%ld",index]];
  293. NSDictionary* params = [item objectForKey:@"params"];
  294. NSString* detail_id=[[[params objectForKey:@"param_0"] valueForKey:@"val" ] stringValue];
  295. brself.product_id = detail_id;
  296. brself.use_model_name = false;
  297. [brself reload];
  298. NSLog(@"color selected");
  299. brself.dropDown = nil;
  300. };
  301. //[[NIDropDown alloc]showDropDown:(id)trigger :&f :arr :arrImage :@"down"];
  302. // self.dropDown.delegate = self;
  303. }
  304. else {
  305. [self.dropDown hideDropDown:(id)trigger];
  306. self.dropDown= nil;
  307. // [self rel];
  308. }
  309. return;
  310. /*
  311. // CGRect rect = trigger.frame;
  312. //
  313. // NSLog(@"%@",NSStringFromCGRect(rect));
  314. //
  315. //
  316. //
  317. // NSLog(@"%@",NSStringFromCGPoint (trigger.center));
  318. //
  319. //
  320. // CGRect new=[trigger convertRect:rect toView:self.view];
  321. //
  322. // NSLog(@"convert %@",NSStringFromCGRect(new));
  323. //
  324. CGRect cellrect_screen = [RAUtils relativeFrameForScreenWithView:trigger];
  325. CGRect rect1=[self.view convertRect:cellrect_screen fromView:[[[UIApplication sharedApplication] delegate] window]];
  326. NSLog(@"convert1 %@",NSStringFromCGRect(rect1));
  327. SelectorViewController *menu = [ self.storyboard instantiateViewControllerWithIdentifier:@"SelectorViewController"];
  328. NSString * name = [self.selector valueForKey:@"name"];
  329. menu.title=name;
  330. menu.selector = self.selector;
  331. menu.selectordelegate = self;
  332. // 1.创建一个UIPopover
  333. UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:[[UINavigationController alloc] initWithRootViewController:menu]];
  334. // 2.设置尺寸
  335. // popover.popoverContentSize = CGSizeMake(320, 44 * 5);
  336. // 3.从哪里显示出来 --> 指向item
  337. [popover presentPopoverFromRect:rect1 inView:self.view permittedArrowDirections:0 animated:YES];
  338. // [popover presentPopoverFromBarButtonItem:item permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
  339. self.popover = popover;
  340. // [self performSegueWithIdentifier:@"selector_popover" sender:self];
  341. */
  342. }
  343. -(void) stepClicked:(int) value
  344. {
  345. self.quantity = value;
  346. }
  347. #pragma mark - Topic cell delegate
  348. -(void) TopicItemClicked:(NSString*) product_id category:(NSString*) category
  349. {
  350. // DetailViewController* dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController" ];
  351. // // dvc
  352. //
  353. //
  354. // dvc.OnBack=^(bool add_cart,bool add_wish,bool update_data)
  355. // {
  356. //
  357. //
  358. // [self reload_container_getdata:false]
  359. // };
  360. //
  361. //
  362. // dvc.product_id=product_id;
  363. // dvc.category_id=category;
  364. // dvc.ispush=true;
  365. // [dvc reload];
  366. // [self.navigationController pushViewController:dvc animated:true];
  367. // self.model_name = value;
  368. self.product_id = product_id;
  369. self.category_id=category;
  370. [self reload];
  371. NSLog(@"color selected");
  372. }
  373. #pragma mark - Table view data source
  374. //- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  375. //{
  376. // return 0;
  377. //}
  378. //- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  379. //{
  380. // return 0;
  381. //}
  382. //- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
  383. //{
  384. // UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  385. // return myView;
  386. //
  387. //}
  388. //- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  389. //{
  390. // UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  391. //// myView.backgroundColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  392. //// UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 90, 22)];
  393. //// titleLabel.textColor=[UIColor whiteColor];
  394. //// titleLabel.backgroundColor = [UIColor clearColor];
  395. //// if(section==0)
  396. //// titleLabel.text=NSLocalizedString(@"display_items", nil);
  397. //// else
  398. //// titleLabel.text=NSLocalizedString(@"hide_items", nil);
  399. //// [titleLabel sizeToFit];
  400. //// [myView addSubview:titleLabel];
  401. ////
  402. // return myView;
  403. //}
  404. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  405. if(section==0)
  406. return nil;
  407. NSDictionary* related_json = [self.detail_data objectForKey:@"related_model"];
  408. if(related_json==nil)
  409. {
  410. NSDictionary* section_json = [self.detail_data objectForKey:[NSString stringWithFormat:@"detail_%ld",(long)(section-1)]];
  411. return [section_json valueForKey:@"title"] ;
  412. }
  413. else
  414. {
  415. if(section==1)
  416. return @"related model";
  417. else
  418. {
  419. NSDictionary* section_json = [self.detail_data objectForKey:[NSString stringWithFormat:@"detail_%ld",(long)section-2]];
  420. return [section_json valueForKey:@"title"] ;
  421. }
  422. }
  423. }
  424. //- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  425. //{
  426. // return 33;
  427. //}
  428. -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  429. {
  430. NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
  431. if (sectionTitle == nil) {
  432. return nil;
  433. }
  434. UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  435. myView.backgroundColor = UIColorFromRGB(0x996633);
  436. myView.layer.shadowPath =[UIBezierPath bezierPathWithRect:myView.bounds].CGPath;
  437. myView.layer.shadowColor = [UIColor blackColor].CGColor;
  438. myView.layer.shadowOffset = CGSizeMake(0, 0);
  439. myView.layer.shadowOpacity = 0.5;
  440. myView.layer.shadowRadius = 2.0;
  441. UILabel *titlelabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5.5, 90, 22)];
  442. titlelabel.textColor=[UIColor whiteColor];
  443. titlelabel.backgroundColor = [UIColor clearColor];
  444. titlelabel.text=sectionTitle;
  445. [titlelabel sizeToFit];
  446. NSDictionary* section_json = [self.detail_data objectForKey:[NSString stringWithFormat:@"detail_%ld",(long)(section-1)]];
  447. bool refresh= [[section_json valueForKey:@"partial_refresh"] boolValue] ;
  448. if(refresh)
  449. {
  450. NSString* btntitle=@"Refresh" ;
  451. UIButton * btn =[[UIButton alloc] initWithFrame:CGRectMake(tableView.bounds.size.width-100-15, 0, 100, 33)];
  452. btn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentRight;
  453. btn.tag=section;
  454. [btn addTarget:self action:@selector(RefreshSction:) forControlEvents:UIControlEventTouchUpInside];
  455. [btn setTitle:btntitle forState:UIControlStateNormal];
  456. [myView addSubview:btn];
  457. }
  458. [myView addSubview:titlelabel];
  459. return myView;
  460. }
  461. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  462. {
  463. int section_count =[[self.detail_data valueForKey:@"detail_section_count"] intValue];
  464. NSDictionary* related_json = [self.detail_data objectForKey:@"related_model"];
  465. if(related_json==nil)
  466. return section_count+1;
  467. else
  468. return section_count+2;
  469. }
  470. //- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
  471. //{
  472. // DebugLog(@"canMoveRowAtIndexPath");
  473. //
  474. // if([tableView numberOfRowsInSection:indexPath.section]>1)
  475. // return true;
  476. // else
  477. // return false;
  478. //}
  479. //- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
  480. //{
  481. // NSMutableArray * from;
  482. // NSMutableArray * to ;
  483. // if(fromIndexPath.section==0)
  484. // from = self.displayfields;
  485. // else
  486. // from = self.hidefields;
  487. // if(toIndexPath.section==0)
  488. // to = self.displayfields;
  489. // else
  490. // to = self.hidefields;
  491. // id content=from[fromIndexPath.row];
  492. // [from removeObjectAtIndex:fromIndexPath.row];
  493. // [to insertObject:content atIndex:toIndexPath.row];
  494. //
  495. // [tableView reloadData];
  496. //
  497. //}
  498. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  499. {
  500. if(section==0)
  501. return 1;
  502. NSDictionary* related_json = [self.detail_data objectForKey:@"related_model"];
  503. if(related_json==nil)
  504. {
  505. NSDictionary* section_json = [self.detail_data objectForKey:[NSString stringWithFormat:@"detail_%ld",(long)section-1]];
  506. NSString* type=[section_json valueForKey:@"type"] ;
  507. if([type isEqualToString:@"content"])
  508. return 1;
  509. return [[section_json valueForKey:@"count"] intValue];
  510. }
  511. else
  512. {
  513. if(section==1)
  514. return 1;
  515. else
  516. {
  517. NSDictionary* section_json = [self.detail_data objectForKey:[NSString stringWithFormat:@"detail_%ld",(long)section-2]];
  518. NSString* type=[section_json valueForKey:@"type"] ;
  519. if([type isEqualToString:@"content"])
  520. return 1;
  521. return [[section_json valueForKey:@"count"] intValue];
  522. }
  523. }
  524. }
  525. //- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  526. //{
  527. // return UITableViewCellEditingStyleNone;
  528. //}
  529. //- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  530. //{
  531. // //如果两次点击的时间间隔小于1秒,则断定为双击事件
  532. //
  533. // if(tableView.editing==false)
  534. // return;
  535. //
  536. //
  537. // if([tableView numberOfRowsInSection:indexPath.section]<=1)
  538. // return;
  539. // // int between = 1;
  540. // NSUInteger curr = [[NSDate date] timeIntervalSince1970];
  541. //
  542. // if(indexPath.row==self.lasttap.row&&indexPath.section==self.lasttap.section)
  543. // {
  544. // if (curr-self.taptime<1) {
  545. // DebugLog(@"double click!");
  546. // self.lasttap =nil;
  547. //
  548. // NSMutableArray * from;
  549. // NSMutableArray * to ;
  550. // int section;
  551. // if(indexPath.section==0)
  552. // {
  553. // from = self.displayfields;
  554. // to = self.hidefields;
  555. // section = 1;
  556. // }
  557. // else
  558. // {
  559. //
  560. // from = self.hidefields;
  561. // to = self.displayfields;
  562. // section = 0;
  563. // }
  564. // NSIndexPath * toIndexPath = [NSIndexPath indexPathForRow:0 inSection:section];
  565. // // NSIndexPath * headerIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
  566. // // NSIndexPath * headerIndexPath1 = toIndexPath;
  567. //
  568. // id content=from[indexPath.row];
  569. // [tableView beginUpdates];
  570. // [from removeObjectAtIndex:indexPath.row];
  571. // [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
  572. //
  573. // [to insertObject:content atIndex:0];
  574. // [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:toIndexPath] withRowAnimation:UITableViewRowAnimationFade];
  575. // [tableView endUpdates];
  576. //
  577. // // [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:headerIndexPath] withRowAnimation:UITableViewRowAnimationFade];
  578. // // [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:headerIndexPath1] withRowAnimation:UITableViewRowAnimationFade];
  579. // [tableView reloadData];
  580. //
  581. //
  582. // }
  583. // self.taptime = curr;
  584. //
  585. //
  586. // }
  587. // else
  588. // {
  589. // self.taptime = curr;
  590. // self.lasttap = indexPath;
  591. // }
  592. //}
  593. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  594. {
  595. if(section ==0)
  596. return 0;
  597. else
  598. return 33;
  599. }
  600. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
  601. {
  602. if(indexPath.section ==0)
  603. return 550;
  604. NSDictionary* nsdic = self.detail_data;
  605. NSDictionary* related_json = [self.detail_data objectForKey:@"related_model"];
  606. if(related_json==nil)
  607. {
  608. NSDictionary* section_json = [self.detail_data objectForKey:[NSString stringWithFormat:@"detail_%ld",(long)indexPath.section-1]];
  609. NSDictionary* item_json =[section_json objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row ]];
  610. NSString* type=[section_json valueForKey:@"type"] ;
  611. if([type isEqualToString:@"kv"])
  612. {
  613. float width = tableView.frame.size.width;
  614. width-=CELL_MARGIN*2;
  615. CGSize constraintkey = CGSizeMake(width*0.4-2*LABEL_MARGIN, 20000.0f);//key label width is 40% cell width;
  616. CGSize constraintval = CGSizeMake(width*0.6-2*LABEL_MARGIN, 20000.0f);//val label width is 60% cell width;
  617. // NSDictionary* item = [self.content get_item_seg:indexPath.section row:indexPath.row];
  618. NSString* key =[item_json valueForKey:@"key"] ;
  619. // [cell.keyLabel sizeToFit];
  620. NSString* val=[item_json valueForKey:@"val"] ;
  621. if([val isEqual:[NSNull null]])
  622. val=@"";
  623. if(val==nil)
  624. val=@"";
  625. if([val isEqualToString:@"null"])
  626. val=@"";
  627. CGRect frame;
  628. frame.size = constraintval;
  629. frame.origin.x=0;
  630. frame.origin.y=0;
  631. RTLabel* rtlabel = [[RTLabel alloc] initWithFrame:frame];
  632. [rtlabel setLineBreakMode: RTTextLineBreakModeWordWrapping];
  633. // rtlabel.lineSpacing = 20.0;
  634. [rtlabel setText: val];
  635. CGSize sizeval=rtlabel.optimumSize;
  636. CGSize sizekey = [key sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:constraintkey lineBreakMode:NSLineBreakByWordWrapping];
  637. // CGSize sizeval = [val sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:constraintval lineBreakMode:NSLineBreakByWordWrapping];
  638. float height = MAX(sizekey.height,sizeval.height);
  639. height = MAX(height+LINE_WIDTH, DEF_CELL_HEIGHT);
  640. return height;
  641. }
  642. else if([type isEqualToString:@"img"])
  643. {
  644. int img_count = [[item_json valueForKey:@"count"] intValue];
  645. double ret = 0;
  646. for(int j=0;j<img_count;j++)
  647. {
  648. NSDictionary* img_json =[item_json objectForKey:[NSString stringWithFormat:@"image_%d",j ]];
  649. float width = [[img_json valueForKey:@"width"]floatValue];
  650. float height = [[img_json valueForKey:@"height"]floatValue];
  651. ret+=height*tableView.bounds.size.width / width;
  652. }
  653. return ret;
  654. }
  655. else if([type isEqualToString:@"content"])
  656. return 270;
  657. return 44;
  658. }
  659. else
  660. {
  661. if(indexPath.section==1)
  662. return 270;
  663. else
  664. {
  665. NSDictionary* section_json = [self.detail_data objectForKey:[NSString stringWithFormat:@"detail_%ld",(long)indexPath.section-2]];
  666. NSDictionary* item_json =[section_json objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row ]];
  667. NSString* type=[section_json valueForKey:@"type"] ;
  668. if([type isEqualToString:@"kv"])
  669. {
  670. float width = tableView.frame.size.width;
  671. width-=CELL_MARGIN*2;
  672. CGSize constraintkey = CGSizeMake(width*0.4-2*LABEL_MARGIN, 20000.0f);//key label width is 40% cell width;
  673. CGSize constraintval = CGSizeMake(width*0.6-2*LABEL_MARGIN, 20000.0f);//val label width is 60% cell width;
  674. // NSDictionary* item = [self.content get_item_seg:indexPath.section row:indexPath.row];
  675. NSString* key =[item_json valueForKey:@"key"] ;
  676. // [cell.keyLabel sizeToFit];
  677. NSString* val=[item_json valueForKey:@"val"] ;
  678. if([val isEqual:[NSNull null]])
  679. val=@"";
  680. if(val==nil)
  681. val=@"";
  682. if([val isEqualToString:@"null"])
  683. val=@"";
  684. CGRect frame;
  685. frame.size = constraintval;
  686. frame.origin.x=0;
  687. frame.origin.y=0;
  688. RTLabel* rtlabel = [[RTLabel alloc] initWithFrame:frame];
  689. [rtlabel setLineBreakMode: RTTextLineBreakModeWordWrapping];
  690. // rtlabel.lineSpacing = 20.0;
  691. [rtlabel setText: val];
  692. CGSize sizeval=rtlabel.optimumSize;
  693. CGSize sizekey = [key sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:constraintkey lineBreakMode:NSLineBreakByWordWrapping];
  694. // CGSize sizeval = [val sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:constraintval lineBreakMode:NSLineBreakByWordWrapping];
  695. float height = MAX(sizekey.height,sizeval.height);
  696. height = MAX(height+LINE_WIDTH, DEF_CELL_HEIGHT);
  697. return height;
  698. }
  699. else if([type isEqualToString:@"img"])
  700. {
  701. int img_count = [[item_json valueForKey:@"count"] intValue];
  702. double ret = 0;
  703. for(int j=0;j<img_count;j++)
  704. {
  705. NSDictionary* img_json =[item_json objectForKey:[NSString stringWithFormat:@"image_%d",j ]];
  706. float width = [[img_json valueForKey:@"width"]floatValue];
  707. float height = [[img_json valueForKey:@"height"]floatValue];
  708. ret+=height*tableView.bounds.size.width / width;
  709. }
  710. return ret;
  711. }
  712. return 44;
  713. }
  714. }
  715. }
  716. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  717. {
  718. // DebugLog(@"cellForRowAtIndexPath");
  719. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  720. if(indexPath.section==0)
  721. {
  722. NSString *CellIdentifier = @"DetailHeaderCell";
  723. DetailHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  724. NSMutableDictionary* section_json = [[self.detail_data objectForKey:@"img_section"] mutableCopy];
  725. self.anchor_button.frame=cell.selector_Button.frame;
  726. cell.celldelegate=self;
  727. cell.quantity_text.text = [NSString stringWithFormat:@"%d",self.quantity];
  728. cell.step = self.step;
  729. cell.quantity = self.quantity;
  730. // cell.stepper.stepValue = self.step;
  731. // cell.stepper.value = self.quantity;
  732. NSString* price = [section_json valueForKey:@"price"];
  733. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  734. if(appDelegate.contact_id ==nil)
  735. {
  736. // appDelegate.bLogin = false;
  737. // cell.btnaddCart.enabled = false;
  738. }
  739. else
  740. {
  741. if ([price isEqualToString:@"No Price."]) {
  742. // cell.btnaddCart.enabled = false;
  743. }
  744. else
  745. {
  746. // cell.btnaddCart.enabled = true;
  747. }
  748. }
  749. int cqty = [[section_json valueForKey:@"Model QTY in cart"] intValue];
  750. cell.cqty = cqty;
  751. cell.set_cqty=^(int cqty)
  752. {
  753. [section_json setValue:[NSString stringWithFormat:@"%d",cqty] forKey:@"Model QTY in cart"];
  754. [self.detail_data setObject:section_json forKey:@"img_section"];
  755. [self.detailTable reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
  756. };
  757. cell.set_update_data=^(bool bupdate)
  758. {
  759. //self.update_data = bupdate;
  760. if(bupdate)
  761. [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  762. else
  763. [((MainViewController*)appDelegate.main_vc) reloadCategory:false immediately:false];
  764. };
  765. if(cqty>0)
  766. {
  767. cell.cqyt_label.hidden=false;
  768. cell.cqyt_label.text=[NSString stringWithFormat:@"Model QTY in Cart:%d",cqty];
  769. }
  770. else
  771. {
  772. cell.cqyt_label.hidden=true;
  773. cell.cqyt_label.text=nil;
  774. }
  775. cell.descriptionRTLabel.text =[NSString stringWithFormat:@"%@\n\n",[section_json valueForKey:@"model_descrition"]];
  776. // cell.descriptionLabel.text =[NSString stringWithFormat:@"%@\n\n",[section_json valueForKey:@"model_descrition"]];
  777. cell.model_label.text =[section_json valueForKey:@"model_name"];
  778. // UIImage* qrimg=[QRCodeGenerator qrImageForString:[section_json valueForKey:@"model_name"] imageSize:80];
  779. // cell.qrcode_imageview.image = qrimg;
  780. if(appDelegate.bLogin==true)
  781. {
  782. if(appDelegate.can_see_price&&appDelegate.price_hidden==false)
  783. {
  784. }
  785. else
  786. {
  787. price=nil;
  788. }
  789. }
  790. cell.priceLabel.text =price;
  791. // cell.shipLabel.text = [section_json valueForKey:@"ship"];
  792. cell.count_Label.text =[section_json valueForKey:@"Availability"] ;
  793. NSString* eta =[section_json valueForKey:@"ETA"] ;
  794. if(eta.length>0)
  795. {
  796. cell.eta_label.hidden = false;
  797. cell.etaval_label.text = eta;
  798. cell.etaval_label.hidden = false;
  799. cell.incoming_stock_label.hidden=false;
  800. cell.incomingcount_Label.text =[section_json valueForKey:@"incoming_stock"] ;
  801. cell.incomingcount_Label.hidden = false;
  802. }
  803. else
  804. {
  805. cell.eta_label.hidden = true;
  806. cell.etaval_label.text = nil;
  807. cell.etaval_label.hidden = true;
  808. cell.incoming_stock_label.hidden = true;
  809. cell.incomingcount_Label.text = nil;
  810. cell.incomingcount_Label.hidden = true;
  811. }
  812. self.selector = [section_json objectForKey:@"selector"];
  813. cell.selector_nameLabel.text =[self.selector valueForKey:@"name"];
  814. int selector_count = [[self.selector valueForKey:@"count"] intValue];
  815. cell.selector_imageView.image = [UIImage imageNamed:@"loading_s"];
  816. for(int i=0;i<selector_count;i++)
  817. {
  818. NSDictionary* params_json=[[self.selector objectForKey:[NSString stringWithFormat:@"item_%d",i]] objectForKey:@"params"] ;
  819. // NSString* aaa=[[[params_json objectForKey:@"param_0"] valueForKey:@"val"] stringValue];
  820. if([[[[params_json objectForKey:@"param_0"] valueForKey:@"val"] stringValue] isEqualToString:self.product_id])
  821. {
  822. cell.selector_valLabel.text=[[self.selector objectForKey:[NSString stringWithFormat:@"item_%d",i]] valueForKey:@"title"];
  823. NSString* selectstr=[NSString stringWithFormat:@"%@: %@",[self.selector valueForKey:@"name"],[[self.selector objectForKey:[NSString stringWithFormat:@"item_%d",i]] valueForKey:@"title"]];
  824. cell.selector_label.text = selectstr;
  825. NSString* img_url = [[self.selector objectForKey:[NSString stringWithFormat:@"item_%d",i]] valueForKey:@"pic_url"];
  826. NSString* file_name=[img_url lastPathComponent];
  827. NSData* img_data=[iSalesDB load_cached_img:file_name];
  828. if(img_data!=nil)
  829. {
  830. dispatch_async(dispatch_get_main_queue(), ^{
  831. UIImage * img =[UIImage imageWithData:img_data];
  832. cell.selector_imageView.image=img;
  833. });
  834. }
  835. else
  836. {
  837. NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  838. dispatch_async(dispatch_get_main_queue(), ^{
  839. if(downloadimg_data!=nil)
  840. {
  841. [iSalesDB cache_img:downloadimg_data :file_name ];
  842. UIImage * img =[UIImage imageWithData:downloadimg_data];
  843. cell.selector_imageView.image=img;
  844. }
  845. else
  846. cell.selector_imageView.image = [UIImage imageNamed:@"notfound_s"];
  847. });
  848. }
  849. }
  850. }
  851. // cell.selector_valLabel.text =[[section_json objectForKey:@"selector"] valueForKey:@"name"];
  852. NSDictionary* property_json = [section_json objectForKey:@"property"];
  853. // int pcount = [[property_json valueForKey:@"count"]intValue];
  854. cell.property_nameLabel.text=[[property_json objectForKey:@"item_0"] valueForKey:@"key"];
  855. cell.property_valLabel.text=[[property_json objectForKey:@"item_0"] valueForKey:@"val"];
  856. cell.product_id = self.product_id;
  857. // cell.itemIDLabel.text = [NSString stringWithFormat:@"Item id:%d",3947105];
  858. [cell setup];
  859. NSDictionary* imgs_json = [section_json objectForKey:@"images"];
  860. int count = [[imgs_json valueForKey:@"img_count"]intValue];
  861. if(count<2-1)//1 for qrcode;
  862. cell.pageControl.hidden = true;
  863. else
  864. cell.pageControl.hidden = false;
  865. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  866. for(int i=0;i<count;i++)
  867. {
  868. NSDictionary* img_json=[imgs_json objectForKey:[NSString stringWithFormat:@"img_%d",i]];
  869. NSString* img_url = [img_json valueForKey:@"s"];
  870. NSString* file_name=[img_url lastPathComponent];
  871. NSData* img_data=[iSalesDB load_cached_img:file_name];
  872. if(img_data!=nil)
  873. {
  874. dispatch_async(dispatch_get_main_queue(), ^{
  875. UIImage * img =[UIImage imageWithData:img_data];
  876. [cell AddPhoto:img];
  877. if(i==count-1)
  878. {
  879. UIImage* qrimg=[QRCodeGenerator qrImageForString:[section_json valueForKey:@"model_name"] imageSize:350];
  880. if(qrimg!=nil)
  881. [cell AddPhoto:qrimg];
  882. }
  883. // if(i==0)
  884. // cell.imageView.image = img;
  885. });
  886. }
  887. else
  888. {
  889. NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  890. dispatch_async(dispatch_get_main_queue(), ^{
  891. if(downloadimg_data!=nil)
  892. {
  893. [iSalesDB cache_img:downloadimg_data :file_name ];
  894. UIImage * img =[UIImage imageWithData:downloadimg_data];
  895. [cell AddPhoto:img];
  896. if(i==count-1)
  897. {
  898. UIImage* qrimg=[QRCodeGenerator qrImageForString:[section_json valueForKey:@"model_name"] imageSize:350];
  899. if(qrimg!=nil)
  900. [cell AddPhoto:qrimg];
  901. }
  902. // if(i==0)
  903. // cell.imageView.image = img;
  904. }
  905. else
  906. {
  907. UIImage * img =[UIImage imageNamed:@"notfound_l"];
  908. [cell AddPhoto:img];
  909. if(i==count-1)
  910. {
  911. UIImage* qrimg=[QRCodeGenerator qrImageForString:[section_json valueForKey:@"model_name"] imageSize:350];
  912. if(qrimg!=nil)
  913. [cell AddPhoto:qrimg];
  914. }
  915. // if(i==0)
  916. // cell.imageView.image = img;
  917. }
  918. });
  919. }
  920. }
  921. });
  922. // cell.backgroundColor = [UIColor whiteColor];
  923. cell.shopCartBlock = ^(UIImageView *imageView){
  924. self.add_cart=true;
  925. CGRect rect = [tableView rectForRowAtIndexPath:indexPath];
  926. rect.origin.y = rect.origin.y - [self.detailTable contentOffset].y;
  927. CGRect headRect = imageView.frame;
  928. headRect.origin.y = rect.origin.y+headRect.origin.y;
  929. [self startAnimationWithRect:headRect ImageView:imageView];
  930. [self.navigationController popToRootViewControllerAnimated:true];
  931. // [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  932. [((MainViewController*)appDelegate.main_vc) switchToCart];
  933. };
  934. cell.WatchlistBlock = ^(UIImageView *imageView){
  935. self.add_wish=true;
  936. CGRect rect = [tableView rectForRowAtIndexPath:indexPath];
  937. rect.origin.y = rect.origin.y - [self.detailTable contentOffset].y;
  938. CGRect headRect = imageView.frame;
  939. headRect.origin.y = rect.origin.y+headRect.origin.y;
  940. [self startAnimationWithRect:headRect ImageView:imageView];
  941. };
  942. return cell;
  943. }
  944. // NSDictionary* related_json = [self.detail_data objectForKey:@"related_model"];
  945. // if(related_json==nil)
  946. else
  947. {
  948. NSDictionary* section_json = [self.detail_data objectForKey:[NSString stringWithFormat:@"detail_%ld",(long)indexPath.section-1]];
  949. NSDictionary* item_json =[section_json objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row ]];
  950. NSString *CellIdentifier = @"DetailInfoCell";
  951. NSString* type=[section_json valueForKey:@"type"] ;
  952. if([type isEqualToString:@"kv"])
  953. {
  954. float width = tableView.frame.size.width;
  955. width-=CELL_MARGIN*2;
  956. CGSize constraintkey = CGSizeMake(width*0.4-2*LABEL_MARGIN, 20000.0f);//key label width is 40% cell width;
  957. CGSize constraintval = CGSizeMake(width*0.6-2*LABEL_MARGIN, 20000.0f);//val label width is 60% cell width;
  958. // NSDictionary* item = [self.content get_item_seg:indexPath.section row:indexPath.row];
  959. NSString* key =[item_json valueForKey:@"key"] ;
  960. NSString* val=[item_json valueForKey:@"val"] ;
  961. NSString* type=[item_json valueForKey:@"type"] ;
  962. if([type isEqualToString:@"price"])
  963. {
  964. if(appDelegate.can_see_price&&appDelegate.price_hidden==false)
  965. {
  966. }
  967. else
  968. {
  969. val=nil;
  970. }
  971. }
  972. if([val isEqual:[NSNull null]])
  973. val=@"";
  974. if(val==nil)
  975. val=@"";
  976. if([val isEqualToString:@"null"])
  977. val=@"";
  978. CGRect frame;
  979. frame.size = constraintval;
  980. frame.origin.x=0;
  981. frame.origin.y=0;
  982. RTLabel* rtlabel = [[RTLabel alloc] initWithFrame:frame];
  983. [rtlabel setLineBreakMode: RTTextLineBreakModeWordWrapping];
  984. // rtlabel.lineSpacing = 20.0;
  985. [rtlabel setText: val];
  986. CGSize sizeval=rtlabel.optimumSize;
  987. CGSize sizekey = [key sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:constraintkey lineBreakMode:NSLineBreakByWordWrapping];
  988. float height = MAX(sizekey.height,sizeval.height);
  989. height = MAX(height, DEF_CELL_HEIGHT-LINE_WIDTH);
  990. sizekey.height = height;
  991. sizekey.width = constraintkey.width;
  992. // sizeval.height = height;
  993. sizeval.width =constraintval.width;
  994. // NSString *CellIdentifier = @"detail_item_kv";
  995. DetailKVCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  996. if(cell==nil)
  997. DebugLog(@"cell is nil.........................");
  998. CGRect framekey = cell.keyLabel.frame;
  999. framekey.origin.x=CELL_MARGIN+LABEL_MARGIN;
  1000. framekey.origin.y=LINE_WIDTH;
  1001. framekey.size = sizekey;
  1002. cell.keyLabel.frame = framekey;
  1003. cell.keyLabel.text=key;
  1004. CGRect frameval = cell.valLabel.frame;
  1005. frameval.origin.x=constraintkey.width+CELL_MARGIN+3*LABEL_MARGIN; //2 margin for key 1 margin for value self
  1006. frameval.origin.y=LINE_WIDTH;
  1007. frameval.size = sizeval;
  1008. cell.valLabel.frame = frameval;
  1009. // [cell.valLabel setTextAlignment:RTTextAlignmentCenter];
  1010. cell.valLabel.text= val;
  1011. CGPoint rcenter;
  1012. rcenter.x = width * 0.4+width * 0.6/2;
  1013. rcenter.y = height /2;
  1014. cell.valLabel.center=rcenter;
  1015. // cell.valLabel.backgroundColor =[UIColor redColor];
  1016. // cell.contentView.superview.backgroundColor = [UIColor clearColor];
  1017. // cell.backgroundColor = [UIColor whiteColor];
  1018. UIView * lineview = [[LineView alloc] initWithFrame:cell.contentView.frame];
  1019. lineview.userInteractionEnabled = NO;// 不设为NO会屏蔽cell的点击事件
  1020. lineview.backgroundColor = [UIColor clearColor];// 设为透明从而使得cell.backgroundColor有效.
  1021. lineview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  1022. [cell.contentView addSubview:lineview];// cell.contentView是个readonly属性,所以别想着替换contentView了.
  1023. cell.backgroundColor = [UIColor whiteColor];
  1024. return cell;
  1025. }
  1026. else if ([type isEqualToString:@"img"])
  1027. {
  1028. DetailImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailImageCell" forIndexPath:indexPath];
  1029. int img_count = [[item_json valueForKey:@"count"] intValue];
  1030. double posy = 0;
  1031. for(int j=0;j<img_count;j++)
  1032. {
  1033. NSDictionary* img_json =[item_json objectForKey:[NSString stringWithFormat:@"image_%d",j ]];
  1034. float width = [[img_json valueForKey:@"width"]floatValue];
  1035. float height = [[img_json valueForKey:@"height"]floatValue];
  1036. //
  1037. UIImageView * imgview = [[UIImageView alloc] initWithFrame:CGRectMake(0, posy, tableView.bounds.size.width , height*tableView.bounds.size.width / width)];
  1038. NSString* img_url = [img_json valueForKey:@"img_url"];
  1039. posy+=height*tableView.bounds.size.width / width;
  1040. NSString* file_name=[img_url lastPathComponent];
  1041. NSData* img_data=[iSalesDB load_cached_img:file_name];
  1042. if(img_data!=nil)
  1043. {
  1044. UIImage * img =[UIImage imageWithData:img_data];
  1045. imgview.image = img;
  1046. [cell addSubview:imgview];
  1047. }
  1048. else
  1049. {
  1050. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1051. NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  1052. dispatch_async(dispatch_get_main_queue(), ^{
  1053. if(downloadimg_data!=nil)
  1054. {
  1055. [iSalesDB cache_img:downloadimg_data :file_name ];
  1056. UIImage * img =[UIImage imageWithData:downloadimg_data];
  1057. imgview.image = img;
  1058. [cell addSubview:imgview];
  1059. }
  1060. else
  1061. {
  1062. imgview.image = [UIImage imageNamed:@"notfound_l"];
  1063. [cell addSubview:imgview];
  1064. }
  1065. });
  1066. });
  1067. }
  1068. }
  1069. cell.backgroundColor = [UIColor whiteColor];
  1070. return cell;
  1071. }
  1072. else if ([type isEqualToString:@"content"])
  1073. {
  1074. NSString *CellIdentifier = @"DetailTopicCell";
  1075. NSString* data=[section_json valueForKey:@"data"] ;
  1076. if([data isEqualToString:@"local"])
  1077. {
  1078. [appDelegate.recent_model addEntriesFromDictionary:section_json];
  1079. // NSDictionary* section_json = [self.detail_data objectForKey:@"related_model"];
  1080. DetailTopicCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  1081. cell.itemdelegate = self;
  1082. cell.related_data = appDelegate.recent_model;
  1083. [cell.topicCollectionView reloadData];
  1084. cell.backgroundColor = [UIColor whiteColor];
  1085. return cell;
  1086. }
  1087. else
  1088. {
  1089. // NSDictionary* section_json = [self.detail_data objectForKey:@"related_model"];
  1090. DetailTopicCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  1091. cell.itemdelegate = self;
  1092. cell.related_data = section_json;
  1093. [cell.topicCollectionView reloadData];
  1094. cell.backgroundColor = [UIColor whiteColor];
  1095. return cell;
  1096. }
  1097. }
  1098. return nil;
  1099. }
  1100. }
  1101. -(void)startAnimationWithRect:(CGRect)rect ImageView:(UIImageView *)imageView
  1102. {
  1103. if (!_layer) {
  1104. // _btn.enabled = NO;
  1105. _layer = [CALayer layer];
  1106. _layer.contents = (id)imageView.layer.contents;
  1107. _layer.contentsGravity = kCAGravityResizeAspectFill;
  1108. _layer.bounds = rect;
  1109. [_layer setCornerRadius:CGRectGetHeight([_layer bounds]) / 2];
  1110. _layer.masksToBounds = YES;
  1111. // 导航64
  1112. _layer.position = CGPointMake(imageView.center.x, CGRectGetMidY(rect)+64);
  1113. // [_tableView.layer addSublayer:_layer];
  1114. [self.view.layer addSublayer:_layer];
  1115. self.path = [UIBezierPath bezierPath];
  1116. // [_path moveToPoint:_layer.position];
  1117. [_path moveToPoint:imageView.center];
  1118. [_path addLineToPoint:CGPointMake(imageView.center.x, imageView.center.y-100)];
  1119. // (SCREEN_WIDTH - 60), 0, 50, 50)
  1120. // [_path addQuadCurveToPoint:CGPointMake(SCREEN_WIDTH - 40, SCREEN_HEIGHT-40) controlPoint:CGPointMake(SCREEN_WIDTH/2,rect.origin.y-80)];
  1121. // [_path addLineToPoint:CGPointMake(SCREEN_WIDTH-40, 30)];
  1122. }
  1123. [self groupAnimation];
  1124. }
  1125. -(void)groupAnimation
  1126. {
  1127. self.detailTable.userInteractionEnabled = NO;
  1128. CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
  1129. animation.path = _path.CGPath;
  1130. // animation.rotationMode = kCAAnimationRotateAuto;
  1131. //
  1132. CABasicAnimation *expandAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  1133. expandAnimation.duration = 0.3f;
  1134. expandAnimation.fromValue = [NSNumber numberWithFloat:1];
  1135. expandAnimation.toValue = [NSNumber numberWithFloat:2.0f];
  1136. expandAnimation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
  1137. CABasicAnimation *narrowAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  1138. narrowAnimation.beginTime = 0.3;
  1139. narrowAnimation.fromValue = [NSNumber numberWithFloat:2.0f];
  1140. narrowAnimation.duration = 0.9f;
  1141. narrowAnimation.toValue = [NSNumber numberWithFloat:0.3f];
  1142. //
  1143. // narrowAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
  1144. CAAnimationGroup *groups = [CAAnimationGroup animation];
  1145. groups.animations = @[animation,expandAnimation,narrowAnimation];
  1146. groups.duration = 1.0f;
  1147. groups.removedOnCompletion=NO;
  1148. groups.fillMode=kCAFillModeForwards;
  1149. groups.delegate = self;
  1150. [_layer addAnimation:groups forKey:@"group"];
  1151. }
  1152. -(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
  1153. {
  1154. // [anim def];
  1155. if (anim == [_layer animationForKey:@"group"]) {
  1156. self.detailTable.userInteractionEnabled = YES;
  1157. // _btn.enabled = YES;
  1158. [_layer removeFromSuperlayer];
  1159. _layer = nil;
  1160. // _cnt++;
  1161. // if (_cnt) {
  1162. // _cntLabel.hidden = NO;
  1163. // }
  1164. // CATransition *animation = [CATransition animation];
  1165. // animation.duration = 0.25f;
  1166. // _cntLabel.text = [NSString stringWithFormat:@"%d",_cnt];
  1167. // [_cntLabel.layer addAnimation:animation forKey:nil];
  1168. // CABasicAnimation *shakeAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
  1169. // shakeAnimation.duration = 0.25f;
  1170. // shakeAnimation.fromValue = [NSNumber numberWithFloat:-5];
  1171. // shakeAnimation.toValue = [NSNumber numberWithFloat:5];
  1172. // shakeAnimation.autoreverses = YES;
  1173. // [_btn.layer addAnimation:shakeAnimation forKey:nil];
  1174. }
  1175. }
  1176. #pragma mark - hide section button clicked
  1177. - (void)RefreshSction:(UIButton *)sender {
  1178. NSDictionary* section_json = nil;
  1179. NSDictionary* related_json = [self.detail_data objectForKey:@"related_model"];
  1180. if(related_json==nil)
  1181. {
  1182. section_json = [self.detail_data objectForKey:[NSString stringWithFormat:@"detail_%ld",(long)(sender.tag-1)]];
  1183. }
  1184. else
  1185. {
  1186. if(sender.tag==1)
  1187. {
  1188. section_json = related_json ;
  1189. }
  1190. else
  1191. {
  1192. section_json = [self.detail_data objectForKey:[NSString stringWithFormat:@"detail_%ld",(long)sender.tag-2]];
  1193. }
  1194. }
  1195. NSString* url = section_json[@"data_interface"];
  1196. NSMutableDictionary* params = [section_json[@"params"] mutableCopy];
  1197. if(params==nil)
  1198. params = [[NSMutableDictionary alloc] init];
  1199. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..."title:@"Refreshing"];
  1200. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1201. NSDictionary* editor_json = [iSalesNetwork commoneditor_partialrefresh:params url:url];
  1202. dispatch_async(dispatch_get_main_queue(), ^{
  1203. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  1204. if([[editor_json valueForKey:@"result"] intValue]==2)
  1205. {
  1206. [self.detail_data addEntriesFromDictionary:editor_json];
  1207. NSRange range = NSMakeRange(sender.tag, 1);
  1208. NSIndexSet *sectionToReload = [NSIndexSet indexSetWithIndexesInRange:range];
  1209. [self.detailTable reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationAutomatic];
  1210. }
  1211. else
  1212. {
  1213. [RAUtils message_alert:[editor_json valueForKey:@"err_msg"] title:nil controller:self] ;
  1214. }
  1215. });
  1216. });
  1217. // self.showMore=!self.showMore;
  1218. // NSRange range = NSMakeRange(1, 1);
  1219. // NSIndexSet *sectionToReload = [NSIndexSet indexSetWithIndexesInRange:range];
  1220. // [self.detailTable reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationAutomatic];
  1221. }
  1222. #pragma mark - Support scanner
  1223. -(void) onDecodedData:(NSString*) value
  1224. {
  1225. // UIApplication * app = [UIApplication sharedApplication];
  1226. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1227. // MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  1228. //
  1229. self.model_name = value;
  1230. self.product_id = nil;
  1231. self.use_model_name = true;
  1232. [self reload];
  1233. NSLog(@"color selected");
  1234. }
  1235. @end