DetailViewController.m 59 KB

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