DetailViewController.m 60 KB

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