DetailViewController.m 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689
  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. // NSString* value =[DefaultAppearance get_noneappearance_value:@"DetailTableHeaderView" valuename:@"refresh_button_color"];
  461. //
  462. // if(value==nil)
  463. // value=@"";
  464. // unsigned long color = strtoul([value UTF8String],0,16);
  465. UIButton * btn =[[UIButton alloc] initWithFrame:CGRectMake(tableView.bounds.size.width-100-15, 0, 100, 33)];
  466. // [btn setTitleColor:UIColorFromRGB(color) forState:UIControlStateNormal];
  467. btn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentRight;
  468. btn.tag=section;
  469. [btn addTarget:self action:@selector(RefreshSection:) forControlEvents:UIControlEventTouchUpInside];
  470. [btn setTitle:btntitle forState:UIControlStateNormal];
  471. [myView addSubview:btn];
  472. }
  473. [myView addSubview:titlelabel];
  474. return myView;
  475. }
  476. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  477. {
  478. int section_count =[[self.detail_data valueForKey:@"detail_section_count"] intValue];
  479. NSDictionary* related_json = [self.detail_data objectForKey:@"related_model"];
  480. if(related_json==nil)
  481. return section_count+1;
  482. else
  483. return section_count+2;
  484. }
  485. //- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
  486. //{
  487. // DebugLog(@"canMoveRowAtIndexPath");
  488. //
  489. // if([tableView numberOfRowsInSection:indexPath.section]>1)
  490. // return true;
  491. // else
  492. // return false;
  493. //}
  494. //- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
  495. //{
  496. // NSMutableArray * from;
  497. // NSMutableArray * to ;
  498. // if(fromIndexPath.section==0)
  499. // from = self.displayfields;
  500. // else
  501. // from = self.hidefields;
  502. // if(toIndexPath.section==0)
  503. // to = self.displayfields;
  504. // else
  505. // to = self.hidefields;
  506. // id content=from[fromIndexPath.row];
  507. // [from removeObjectAtIndex:fromIndexPath.row];
  508. // [to insertObject:content atIndex:toIndexPath.row];
  509. //
  510. // [tableView reloadData];
  511. //
  512. //}
  513. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  514. {
  515. if(section==0)
  516. return 1;
  517. NSDictionary* related_json = [self.detail_data objectForKey:@"related_model"];
  518. if(related_json==nil)
  519. {
  520. NSDictionary* section_json = [self.detail_data objectForKey:[NSString stringWithFormat:@"detail_%ld",(long)section-1]];
  521. NSString* type=[section_json valueForKey:@"type"] ;
  522. if([type isEqualToString:@"content"])
  523. return 1;
  524. return [[section_json valueForKey:@"count"] intValue];
  525. }
  526. else
  527. {
  528. if(section==1)
  529. return 1;
  530. else
  531. {
  532. NSDictionary* section_json = [self.detail_data objectForKey:[NSString stringWithFormat:@"detail_%ld",(long)section-2]];
  533. NSString* type=[section_json valueForKey:@"type"] ;
  534. if([type isEqualToString:@"content"])
  535. return 1;
  536. return [[section_json valueForKey:@"count"] intValue];
  537. }
  538. }
  539. }
  540. //- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  541. //{
  542. // return UITableViewCellEditingStyleNone;
  543. //}
  544. //- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  545. //{
  546. // //如果两次点击的时间间隔小于1秒,则断定为双击事件
  547. //
  548. // if(tableView.editing==false)
  549. // return;
  550. //
  551. //
  552. // if([tableView numberOfRowsInSection:indexPath.section]<=1)
  553. // return;
  554. // // int between = 1;
  555. // NSUInteger curr = [[NSDate date] timeIntervalSince1970];
  556. //
  557. // if(indexPath.row==self.lasttap.row&&indexPath.section==self.lasttap.section)
  558. // {
  559. // if (curr-self.taptime<1) {
  560. // DebugLog(@"double click!");
  561. // self.lasttap =nil;
  562. //
  563. // NSMutableArray * from;
  564. // NSMutableArray * to ;
  565. // int section;
  566. // if(indexPath.section==0)
  567. // {
  568. // from = self.displayfields;
  569. // to = self.hidefields;
  570. // section = 1;
  571. // }
  572. // else
  573. // {
  574. //
  575. // from = self.hidefields;
  576. // to = self.displayfields;
  577. // section = 0;
  578. // }
  579. // NSIndexPath * toIndexPath = [NSIndexPath indexPathForRow:0 inSection:section];
  580. // // NSIndexPath * headerIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
  581. // // NSIndexPath * headerIndexPath1 = toIndexPath;
  582. //
  583. // id content=from[indexPath.row];
  584. // [tableView beginUpdates];
  585. // [from removeObjectAtIndex:indexPath.row];
  586. // [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
  587. //
  588. // [to insertObject:content atIndex:0];
  589. // [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:toIndexPath] withRowAnimation:UITableViewRowAnimationFade];
  590. // [tableView endUpdates];
  591. //
  592. // // [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:headerIndexPath] withRowAnimation:UITableViewRowAnimationFade];
  593. // // [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:headerIndexPath1] withRowAnimation:UITableViewRowAnimationFade];
  594. // [tableView reloadData];
  595. //
  596. //
  597. // }
  598. // self.taptime = curr;
  599. //
  600. //
  601. // }
  602. // else
  603. // {
  604. // self.taptime = curr;
  605. // self.lasttap = indexPath;
  606. // }
  607. //}
  608. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  609. {
  610. if(section ==0)
  611. return 0;
  612. else
  613. return 33;
  614. }
  615. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
  616. {
  617. if(indexPath.section ==0)
  618. return 550;
  619. NSDictionary* nsdic = self.detail_data;
  620. NSDictionary* related_json = [self.detail_data objectForKey:@"related_model"];
  621. if(related_json==nil)
  622. {
  623. NSDictionary* section_json = [self.detail_data objectForKey:[NSString stringWithFormat:@"detail_%ld",(long)indexPath.section-1]];
  624. NSDictionary* item_json =[section_json objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row ]];
  625. NSString* type=[section_json valueForKey:@"type"] ;
  626. if([type isEqualToString:@"kv"])
  627. {
  628. float width = tableView.frame.size.width;
  629. width-=CELL_MARGIN*2;
  630. CGSize constraintkey = CGSizeMake(width*0.4-2*LABEL_MARGIN, 20000.0f);//key label width is 40% cell width;
  631. CGSize constraintval = CGSizeMake(width*0.6-2*LABEL_MARGIN, 20000.0f);//val label width is 60% cell width;
  632. // NSDictionary* item = [self.content get_item_seg:indexPath.section row:indexPath.row];
  633. NSString* key =[item_json valueForKey:@"key"] ;
  634. // [cell.keyLabel sizeToFit];
  635. NSString* val=[item_json valueForKey:@"val"] ;
  636. if([val isEqual:[NSNull null]])
  637. val=@"";
  638. if(val==nil)
  639. val=@"";
  640. if([val isEqualToString:@"null"])
  641. val=@"";
  642. CGRect frame;
  643. frame.size = constraintval;
  644. frame.origin.x=0;
  645. frame.origin.y=0;
  646. RTLabel* rtlabel = [[RTLabel alloc] initWithFrame:frame];
  647. [rtlabel setLineBreakMode: RTTextLineBreakModeWordWrapping];
  648. // rtlabel.lineSpacing = 20.0;
  649. [rtlabel setText: val];
  650. CGSize sizeval=rtlabel.optimumSize;
  651. CGSize sizekey = [key sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:constraintkey lineBreakMode:NSLineBreakByWordWrapping];
  652. // CGSize sizeval = [val sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:constraintval lineBreakMode:NSLineBreakByWordWrapping];
  653. float height = MAX(sizekey.height,sizeval.height);
  654. height = MAX(height+LINE_WIDTH, DEF_CELL_HEIGHT);
  655. return height;
  656. }
  657. else if([type isEqualToString:@"img"])
  658. {
  659. int img_count = [[item_json valueForKey:@"count"] intValue];
  660. double ret = 0;
  661. for(int j=0;j<img_count;j++)
  662. {
  663. NSDictionary* img_json =[item_json objectForKey:[NSString stringWithFormat:@"image_%d",j ]];
  664. float width = [[img_json valueForKey:@"width"]floatValue];
  665. float height = [[img_json valueForKey:@"height"]floatValue];
  666. ret+=height*tableView.bounds.size.width / width;
  667. }
  668. return ret;
  669. }
  670. else if([type isEqualToString:@"content"])
  671. return 270;
  672. return 44;
  673. }
  674. else
  675. {
  676. if(indexPath.section==1)
  677. return 270;
  678. else
  679. {
  680. NSDictionary* section_json = [self.detail_data objectForKey:[NSString stringWithFormat:@"detail_%ld",(long)indexPath.section-2]];
  681. NSDictionary* item_json =[section_json objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row ]];
  682. NSString* type=[section_json valueForKey:@"type"] ;
  683. if([type isEqualToString:@"kv"])
  684. {
  685. float width = tableView.frame.size.width;
  686. width-=CELL_MARGIN*2;
  687. CGSize constraintkey = CGSizeMake(width*0.4-2*LABEL_MARGIN, 20000.0f);//key label width is 40% cell width;
  688. CGSize constraintval = CGSizeMake(width*0.6-2*LABEL_MARGIN, 20000.0f);//val label width is 60% cell width;
  689. // NSDictionary* item = [self.content get_item_seg:indexPath.section row:indexPath.row];
  690. NSString* key =[item_json valueForKey:@"key"] ;
  691. // [cell.keyLabel sizeToFit];
  692. NSString* val=[item_json valueForKey:@"val"] ;
  693. if([val isEqual:[NSNull null]])
  694. val=@"";
  695. if(val==nil)
  696. val=@"";
  697. if([val isEqualToString:@"null"])
  698. val=@"";
  699. CGRect frame;
  700. frame.size = constraintval;
  701. frame.origin.x=0;
  702. frame.origin.y=0;
  703. RTLabel* rtlabel = [[RTLabel alloc] initWithFrame:frame];
  704. [rtlabel setLineBreakMode: RTTextLineBreakModeWordWrapping];
  705. // rtlabel.lineSpacing = 20.0;
  706. [rtlabel setText: val];
  707. CGSize sizeval=rtlabel.optimumSize;
  708. CGSize sizekey = [key sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:constraintkey lineBreakMode:NSLineBreakByWordWrapping];
  709. // CGSize sizeval = [val sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:constraintval lineBreakMode:NSLineBreakByWordWrapping];
  710. float height = MAX(sizekey.height,sizeval.height);
  711. height = MAX(height+LINE_WIDTH, DEF_CELL_HEIGHT);
  712. return height;
  713. }
  714. else if([type isEqualToString:@"img"])
  715. {
  716. int img_count = [[item_json valueForKey:@"count"] intValue];
  717. double ret = 0;
  718. for(int j=0;j<img_count;j++)
  719. {
  720. NSDictionary* img_json =[item_json objectForKey:[NSString stringWithFormat:@"image_%d",j ]];
  721. float width = [[img_json valueForKey:@"width"]floatValue];
  722. float height = [[img_json valueForKey:@"height"]floatValue];
  723. ret+=height*tableView.bounds.size.width / width;
  724. }
  725. return ret;
  726. }
  727. return 44;
  728. }
  729. }
  730. }
  731. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  732. {
  733. // DebugLog(@"cellForRowAtIndexPath");
  734. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  735. if(indexPath.section==0)
  736. {
  737. NSString *CellIdentifier = @"DetailHeaderCell";
  738. DetailHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  739. NSMutableDictionary* section_json = [[self.detail_data objectForKey:@"img_section"] mutableCopy];
  740. self.anchor_button.frame=cell.selector_Button.frame;
  741. cell.celldelegate=self;
  742. cell.quantity_text.text = [NSString stringWithFormat:@"%d",self.quantity];
  743. cell.step = self.step;
  744. cell.quantity = self.quantity;
  745. // cell.stepper.stepValue = self.step;
  746. // cell.stepper.value = self.quantity;
  747. NSString* price = [section_json valueForKey:@"price"];
  748. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  749. if(appDelegate.contact_id ==nil)
  750. {
  751. // appDelegate.bLogin = false;
  752. // cell.btnaddCart.enabled = false;
  753. }
  754. else
  755. {
  756. if ([price isEqualToString:@"No Price."]) {
  757. // cell.btnaddCart.enabled = false;
  758. }
  759. else
  760. {
  761. // cell.btnaddCart.enabled = true;
  762. }
  763. }
  764. int cqty = [[section_json valueForKey:@"Model QTY in cart"] intValue];
  765. cell.cqty = cqty;
  766. cell.set_cqty=^(int cqty)
  767. {
  768. [section_json setValue:[NSString stringWithFormat:@"%d",cqty] forKey:@"Model QTY in cart"];
  769. [self.detail_data setObject:section_json forKey:@"img_section"];
  770. [self.detailTable reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
  771. };
  772. cell.set_update_data=^(bool bupdate)
  773. {
  774. //self.update_data = bupdate;
  775. if(bupdate)
  776. [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  777. else
  778. [((MainViewController*)appDelegate.main_vc) reloadCategory:false immediately:false];
  779. };
  780. if(cqty>0)
  781. {
  782. cell.cqyt_label.hidden=false;
  783. cell.cqyt_label.text=[NSString stringWithFormat:@"Model QTY in Cart:%d",cqty];
  784. }
  785. else
  786. {
  787. cell.cqyt_label.hidden=true;
  788. cell.cqyt_label.text=nil;
  789. }
  790. cell.descriptionRTLabel.text =[NSString stringWithFormat:@"%@\n\n",[section_json valueForKey:@"model_descrition"]];
  791. // cell.descriptionLabel.text =[NSString stringWithFormat:@"%@\n\n",[section_json valueForKey:@"model_descrition"]];
  792. cell.model_label.text =[section_json valueForKey:@"model_name"];
  793. // UIImage* qrimg=[QRCodeGenerator qrImageForString:[section_json valueForKey:@"model_name"] imageSize:80];
  794. // cell.qrcode_imageview.image = qrimg;
  795. if(appDelegate.bLogin==true)
  796. {
  797. if(appDelegate.can_see_price&&appDelegate.price_hidden==false)
  798. {
  799. }
  800. else
  801. {
  802. price=nil;
  803. }
  804. }
  805. cell.priceLabel.text =price;
  806. // cell.shipLabel.text = [section_json valueForKey:@"ship"];
  807. cell.count_Label.text =[section_json valueForKey:@"Availability"] ;
  808. NSString* eta =[section_json valueForKey:@"ETA"] ;
  809. if(eta.length>0)
  810. {
  811. cell.eta_label.hidden = false;
  812. cell.etaval_label.text = eta;
  813. cell.etaval_label.hidden = false;
  814. cell.incoming_stock_label.hidden=false;
  815. cell.incomingcount_Label.text =[section_json valueForKey:@"incoming_stock"] ;
  816. cell.incomingcount_Label.hidden = false;
  817. }
  818. else
  819. {
  820. cell.eta_label.hidden = true;
  821. cell.etaval_label.text = nil;
  822. cell.etaval_label.hidden = true;
  823. cell.incoming_stock_label.hidden = true;
  824. cell.incomingcount_Label.text = nil;
  825. cell.incomingcount_Label.hidden = true;
  826. }
  827. self.selector = [section_json objectForKey:@"selector"];
  828. if(self.selector!=nil)
  829. {
  830. [cell Hide_selector:false];
  831. cell.selector_nameLabel.text =[self.selector valueForKey:@"name"];
  832. int selector_count = [[self.selector valueForKey:@"count"] intValue];
  833. cell.selector_imageView.image = [UIImage imageNamed:@"loading_s"];
  834. for(int i=0;i<selector_count;i++)
  835. {
  836. NSDictionary* params_json=[[self.selector objectForKey:[NSString stringWithFormat:@"item_%d",i]] objectForKey:@"params"] ;
  837. // NSString* aaa=[[[params_json objectForKey:@"param_0"] valueForKey:@"val"] stringValue];
  838. if([[[[params_json objectForKey:@"param_0"] valueForKey:@"val"] stringValue] isEqualToString:self.product_id])
  839. {
  840. cell.selector_valLabel.text=[[self.selector objectForKey:[NSString stringWithFormat:@"item_%d",i]] valueForKey:@"title"];
  841. NSString* selectstr=[NSString stringWithFormat:@"%@: %@",[self.selector valueForKey:@"name"],[[self.selector objectForKey:[NSString stringWithFormat:@"item_%d",i]] valueForKey:@"title"]];
  842. cell.selector_label.text = selectstr;
  843. NSString* img_url = [[self.selector objectForKey:[NSString stringWithFormat:@"item_%d",i]] valueForKey:@"pic_url"];
  844. NSString* file_name=[img_url lastPathComponent];
  845. NSData* img_data=[iSalesDB load_cached_img:file_name loadFrom:img_url];
  846. if(img_data!=nil)
  847. {
  848. dispatch_async(dispatch_get_main_queue(), ^{
  849. UIImage * img =[UIImage imageWithData:img_data];
  850. cell.selector_imageView.image=img;
  851. });
  852. }
  853. else
  854. {
  855. NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  856. dispatch_async(dispatch_get_main_queue(), ^{
  857. if(downloadimg_data!=nil)
  858. {
  859. [iSalesDB cache_img:downloadimg_data filename:file_name saveTo:img_url];
  860. UIImage * img =[UIImage imageWithData:downloadimg_data];
  861. cell.selector_imageView.image=img;
  862. }
  863. else
  864. cell.selector_imageView.image = [UIImage imageNamed:@"notfound_s"];
  865. });
  866. }
  867. }
  868. }
  869. }
  870. else
  871. {
  872. [cell Hide_selector:true];
  873. }
  874. // cell.selector_valLabel.text =[[section_json objectForKey:@"selector"] valueForKey:@"name"];
  875. NSDictionary* property_json = [section_json objectForKey:@"property"];
  876. // int pcount = [[property_json valueForKey:@"count"]intValue];
  877. cell.property_nameLabel.text=[[property_json objectForKey:@"item_0"] valueForKey:@"key"];
  878. cell.property_valLabel.text=[[property_json objectForKey:@"item_0"] valueForKey:@"val"];
  879. cell.product_id = self.product_id;
  880. // cell.itemIDLabel.text = [NSString stringWithFormat:@"Item id:%d",3947105];
  881. [cell setup];
  882. [cell ClearPhotos];
  883. NSDictionary* imgs_json = [section_json objectForKey:@"images"];
  884. int count = [[imgs_json valueForKey:@"img_count"]intValue];
  885. if(count<2-1)//1 for qrcode;
  886. cell.pageControl.hidden = true;
  887. else
  888. cell.pageControl.hidden = false;
  889. // NSMutableArray* urls= [[NSMutableArray alloc]init];
  890. // cell.urls = urls;
  891. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  892. for(int i=0;i<count;i++)
  893. {
  894. NSDictionary* img_json=[imgs_json objectForKey:[NSString stringWithFormat:@"img_%d",i]];
  895. NSString* img_url = [img_json valueForKey:@"s"];
  896. // NSString* img_url_l = [img_json valueForKey:@"l"];
  897. // if(img_url_l.length>0)
  898. // [urls addObject:img_url_l];
  899. NSString* file_name=[img_url lastPathComponent];
  900. NSData* img_data=[iSalesDB load_cached_img:file_name loadFrom:img_url];
  901. if(img_data!=nil)
  902. {
  903. dispatch_async(dispatch_get_main_queue(), ^{
  904. UIImage * img =[UIImage imageWithData:img_data];
  905. [cell AddPhoto:img];
  906. if(i==count-1)
  907. {
  908. UIImage* qrimg=[QRCodeGenerator qrImageForString:[section_json valueForKey:@"model_name"] imageSize:350];
  909. if(qrimg!=nil)
  910. [cell AddPhoto:qrimg];
  911. }
  912. // if(i==0)
  913. // cell.imageView.image = img;
  914. });
  915. }
  916. else
  917. {
  918. NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  919. dispatch_async(dispatch_get_main_queue(), ^{
  920. if(downloadimg_data!=nil)
  921. {
  922. [iSalesDB cache_img:downloadimg_data filename:file_name saveTo:img_url];
  923. UIImage * img =[UIImage imageWithData:downloadimg_data];
  924. [cell AddPhoto:img];
  925. if(i==count-1)
  926. {
  927. UIImage* qrimg=[QRCodeGenerator qrImageForString:[section_json valueForKey:@"model_name"] imageSize:350];
  928. if(qrimg!=nil)
  929. [cell AddPhoto:qrimg];
  930. }
  931. // if(i==0)
  932. // cell.imageView.image = img;
  933. }
  934. else
  935. {
  936. UIImage * img =[UIImage imageNamed:@"notfound_l"];
  937. [cell AddPhoto:img];
  938. if(i==count-1)
  939. {
  940. UIImage* qrimg=[QRCodeGenerator qrImageForString:[section_json valueForKey:@"model_name"] imageSize:350];
  941. if(qrimg!=nil)
  942. [cell AddPhoto:qrimg];
  943. }
  944. // if(i==0)
  945. // cell.imageView.image = img;
  946. }
  947. });
  948. }
  949. }
  950. });
  951. // cell.backgroundColor = [UIColor whiteColor];
  952. cell.shopCartBlock = ^(UIImageView *imageView){
  953. self.add_cart=true;
  954. CGRect rect = [tableView rectForRowAtIndexPath:indexPath];
  955. rect.origin.y = rect.origin.y - [self.detailTable contentOffset].y;
  956. CGRect headRect = imageView.frame;
  957. headRect.origin.y = rect.origin.y+headRect.origin.y;
  958. [self startAnimationWithRect:headRect ImageView:imageView];
  959. [self.navigationController popToRootViewControllerAnimated:true];
  960. // [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  961. [((MainViewController*)appDelegate.main_vc) switchToCart];
  962. };
  963. cell.WatchlistBlock = ^(UIImageView *imageView){
  964. self.add_wish=true;
  965. CGRect rect = [tableView rectForRowAtIndexPath:indexPath];
  966. rect.origin.y = rect.origin.y - [self.detailTable contentOffset].y;
  967. CGRect headRect = imageView.frame;
  968. headRect.origin.y = rect.origin.y+headRect.origin.y;
  969. [self startAnimationWithRect:headRect ImageView:imageView];
  970. };
  971. cell.PortfolioBlock = ^(UIImageView *imageView){
  972. self.add_portfolio=true;
  973. CGRect rect = [tableView rectForRowAtIndexPath:indexPath];
  974. rect.origin.y = rect.origin.y - [self.detailTable contentOffset].y;
  975. CGRect headRect = imageView.frame;
  976. headRect.origin.y = rect.origin.y+headRect.origin.y;
  977. [self startAnimationWithRect:headRect ImageView:imageView];
  978. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  979. [((MainViewController*)appDelegate.main_vc) reloadPortfolio:true immediately:false];
  980. };
  981. return cell;
  982. }
  983. // NSDictionary* related_json = [self.detail_data objectForKey:@"related_model"];
  984. // if(related_json==nil)
  985. else
  986. {
  987. NSDictionary* section_json = [self.detail_data objectForKey:[NSString stringWithFormat:@"detail_%ld",(long)indexPath.section-1]];
  988. NSDictionary* item_json =[section_json objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row ]];
  989. NSString *CellIdentifier = @"DetailInfoCell";
  990. NSString* type=[section_json valueForKey:@"type"] ;
  991. if([type isEqualToString:@"kv"])
  992. {
  993. float width = tableView.frame.size.width;
  994. width-=CELL_MARGIN*2;
  995. CGSize constraintkey = CGSizeMake(width*0.4-2*LABEL_MARGIN, 20000.0f);//key label width is 40% cell width;
  996. CGSize constraintval = CGSizeMake(width*0.6-2*LABEL_MARGIN, 20000.0f);//val label width is 60% cell width;
  997. // NSDictionary* item = [self.content get_item_seg:indexPath.section row:indexPath.row];
  998. NSString* key =[item_json valueForKey:@"key"] ;
  999. NSString* val=[item_json valueForKey:@"val"] ;
  1000. NSString* type=[item_json valueForKey:@"type"] ;
  1001. if([type isEqualToString:@"price"])
  1002. {
  1003. if(appDelegate.can_see_price&&appDelegate.price_hidden==false)
  1004. {
  1005. }
  1006. else
  1007. {
  1008. val=nil;
  1009. }
  1010. }
  1011. if([val isEqual:[NSNull null]])
  1012. val=@"";
  1013. if(val==nil)
  1014. val=@"";
  1015. if([val isEqualToString:@"null"])
  1016. val=@"";
  1017. CGRect frame;
  1018. frame.size = constraintval;
  1019. frame.origin.x=0;
  1020. frame.origin.y=0;
  1021. RTLabel* rtlabel = [[RTLabel alloc] initWithFrame:frame];
  1022. [rtlabel setLineBreakMode: RTTextLineBreakModeWordWrapping];
  1023. // rtlabel.lineSpacing = 20.0;
  1024. [rtlabel setText: val];
  1025. CGSize sizeval=rtlabel.optimumSize;
  1026. CGSize sizekey = [key sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:constraintkey lineBreakMode:NSLineBreakByWordWrapping];
  1027. float height = MAX(sizekey.height,sizeval.height);
  1028. height = MAX(height, DEF_CELL_HEIGHT-LINE_WIDTH);
  1029. sizekey.height = height;
  1030. sizekey.width = constraintkey.width;
  1031. // sizeval.height = height;
  1032. sizeval.width =constraintval.width;
  1033. // NSString *CellIdentifier = @"detail_item_kv";
  1034. DetailKVCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  1035. if(cell==nil)
  1036. DebugLog(@"cell is nil.........................");
  1037. CGRect framekey = cell.keyLabel.frame;
  1038. framekey.origin.x=CELL_MARGIN+LABEL_MARGIN;
  1039. framekey.origin.y=LINE_WIDTH;
  1040. framekey.size = sizekey;
  1041. cell.keyLabel.frame = framekey;
  1042. cell.keyLabel.text=key;
  1043. CGRect frameval = cell.valLabel.frame;
  1044. frameval.origin.x=constraintkey.width+CELL_MARGIN+3*LABEL_MARGIN; //2 margin for key 1 margin for value self
  1045. frameval.origin.y=LINE_WIDTH;
  1046. frameval.size = sizeval;
  1047. cell.valLabel.frame = frameval;
  1048. // [cell.valLabel setTextAlignment:RTTextAlignmentCenter];
  1049. cell.valLabel.text= val;
  1050. CGPoint rcenter;
  1051. rcenter.x = width * 0.4+width * 0.6/2;
  1052. rcenter.y = height /2;
  1053. cell.valLabel.center=rcenter;
  1054. // cell.valLabel.backgroundColor =[UIColor redColor];
  1055. // cell.contentView.superview.backgroundColor = [UIColor clearColor];
  1056. // cell.backgroundColor = [UIColor whiteColor];
  1057. UIView * lineview = [[LineView alloc] initWithFrame:cell.contentView.frame];
  1058. lineview.userInteractionEnabled = NO;// 不设为NO会屏蔽cell的点击事件
  1059. lineview.backgroundColor = [UIColor clearColor];// 设为透明从而使得cell.backgroundColor有效.
  1060. lineview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  1061. [cell.contentView addSubview:lineview];// cell.contentView是个readonly属性,所以别想着替换contentView了.
  1062. cell.backgroundColor = [UIColor whiteColor];
  1063. return cell;
  1064. }
  1065. else if ([type isEqualToString:@"img"])
  1066. {
  1067. DetailImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailImageCell" forIndexPath:indexPath];
  1068. int img_count = [[item_json valueForKey:@"count"] intValue];
  1069. double posy = 0;
  1070. for(int j=0;j<img_count;j++)
  1071. {
  1072. NSDictionary* img_json =[item_json objectForKey:[NSString stringWithFormat:@"image_%d",j ]];
  1073. float width = [[img_json valueForKey:@"width"]floatValue];
  1074. float height = [[img_json valueForKey:@"height"]floatValue];
  1075. //
  1076. UIImageView * imgview = [[UIImageView alloc] initWithFrame:CGRectMake(0, posy, tableView.bounds.size.width , height*tableView.bounds.size.width / width)];
  1077. NSString* img_url = [img_json valueForKey:@"img_url"];
  1078. posy+=height*tableView.bounds.size.width / width;
  1079. NSString* file_name=[img_url lastPathComponent];
  1080. NSData* img_data=[iSalesDB load_cached_img:file_name loadFrom:img_url];
  1081. if(img_data!=nil)
  1082. {
  1083. UIImage * img =[UIImage imageWithData:img_data];
  1084. imgview.image = img;
  1085. [cell addSubview:imgview];
  1086. }
  1087. else
  1088. {
  1089. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1090. NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  1091. dispatch_async(dispatch_get_main_queue(), ^{
  1092. if(downloadimg_data!=nil)
  1093. {
  1094. [iSalesDB cache_img:downloadimg_data filename:file_name saveTo:img_url];
  1095. UIImage * img =[UIImage imageWithData:downloadimg_data];
  1096. imgview.image = img;
  1097. [cell addSubview:imgview];
  1098. }
  1099. else
  1100. {
  1101. imgview.image = [UIImage imageNamed:@"notfound_l"];
  1102. [cell addSubview:imgview];
  1103. }
  1104. });
  1105. });
  1106. }
  1107. }
  1108. cell.backgroundColor = [UIColor whiteColor];
  1109. return cell;
  1110. }
  1111. else if ([type isEqualToString:@"content"])
  1112. {
  1113. NSString *CellIdentifier = @"DetailTopicCell";
  1114. NSString* data=[section_json valueForKey:@"data"] ;
  1115. if([data isEqualToString:@"local"])
  1116. {
  1117. [appDelegate.recent_model addEntriesFromDictionary:section_json];
  1118. // NSDictionary* section_json = [self.detail_data objectForKey:@"related_model"];
  1119. DetailTopicCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  1120. cell.itemdelegate = self;
  1121. cell.related_data = appDelegate.recent_model;
  1122. [cell.topicCollectionView reloadData];
  1123. cell.backgroundColor = [UIColor whiteColor];
  1124. return cell;
  1125. }
  1126. else
  1127. {
  1128. // NSDictionary* section_json = [self.detail_data objectForKey:@"related_model"];
  1129. DetailTopicCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  1130. cell.itemdelegate = self;
  1131. cell.related_data = section_json;
  1132. [cell.topicCollectionView reloadData];
  1133. cell.backgroundColor = [UIColor whiteColor];
  1134. return cell;
  1135. }
  1136. }
  1137. return nil;
  1138. }
  1139. }
  1140. -(void)startAnimationWithRect:(CGRect)rect ImageView:(UIImageView *)imageView
  1141. {
  1142. if (!_layer) {
  1143. // _btn.enabled = NO;
  1144. _layer = [CALayer layer];
  1145. _layer.contents = (id)imageView.layer.contents;
  1146. _layer.contentsGravity = kCAGravityResizeAspectFill;
  1147. _layer.bounds = rect;
  1148. [_layer setCornerRadius:CGRectGetHeight([_layer bounds]) / 2];
  1149. _layer.masksToBounds = YES;
  1150. // 导航64
  1151. _layer.position = CGPointMake(imageView.center.x, CGRectGetMidY(rect)+64);
  1152. // [_tableView.layer addSublayer:_layer];
  1153. [self.view.layer addSublayer:_layer];
  1154. self.path = [UIBezierPath bezierPath];
  1155. // [_path moveToPoint:_layer.position];
  1156. [_path moveToPoint:imageView.center];
  1157. [_path addLineToPoint:CGPointMake(imageView.center.x, imageView.center.y-100)];
  1158. // (SCREEN_WIDTH - 60), 0, 50, 50)
  1159. // [_path addQuadCurveToPoint:CGPointMake(SCREEN_WIDTH - 40, SCREEN_HEIGHT-40) controlPoint:CGPointMake(SCREEN_WIDTH/2,rect.origin.y-80)];
  1160. // [_path addLineToPoint:CGPointMake(SCREEN_WIDTH-40, 30)];
  1161. }
  1162. [self groupAnimation];
  1163. }
  1164. -(void)groupAnimation
  1165. {
  1166. self.detailTable.userInteractionEnabled = NO;
  1167. CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
  1168. animation.path = _path.CGPath;
  1169. // animation.rotationMode = kCAAnimationRotateAuto;
  1170. //
  1171. CABasicAnimation *expandAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  1172. expandAnimation.duration = 0.3f;
  1173. expandAnimation.fromValue = [NSNumber numberWithFloat:1];
  1174. expandAnimation.toValue = [NSNumber numberWithFloat:2.0f];
  1175. expandAnimation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
  1176. CABasicAnimation *narrowAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  1177. narrowAnimation.beginTime = 0.3;
  1178. narrowAnimation.fromValue = [NSNumber numberWithFloat:2.0f];
  1179. narrowAnimation.duration = 0.9f;
  1180. narrowAnimation.toValue = [NSNumber numberWithFloat:0.3f];
  1181. //
  1182. // narrowAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
  1183. CAAnimationGroup *groups = [CAAnimationGroup animation];
  1184. groups.animations = @[animation,expandAnimation,narrowAnimation];
  1185. groups.duration = 1.0f;
  1186. groups.removedOnCompletion=NO;
  1187. groups.fillMode=kCAFillModeForwards;
  1188. groups.delegate = self;
  1189. [_layer addAnimation:groups forKey:@"group"];
  1190. }
  1191. -(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
  1192. {
  1193. // [anim def];
  1194. if (anim == [_layer animationForKey:@"group"]) {
  1195. self.detailTable.userInteractionEnabled = YES;
  1196. // _btn.enabled = YES;
  1197. [_layer removeFromSuperlayer];
  1198. _layer = nil;
  1199. // _cnt++;
  1200. // if (_cnt) {
  1201. // _cntLabel.hidden = NO;
  1202. // }
  1203. // CATransition *animation = [CATransition animation];
  1204. // animation.duration = 0.25f;
  1205. // _cntLabel.text = [NSString stringWithFormat:@"%d",_cnt];
  1206. // [_cntLabel.layer addAnimation:animation forKey:nil];
  1207. // CABasicAnimation *shakeAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
  1208. // shakeAnimation.duration = 0.25f;
  1209. // shakeAnimation.fromValue = [NSNumber numberWithFloat:-5];
  1210. // shakeAnimation.toValue = [NSNumber numberWithFloat:5];
  1211. // shakeAnimation.autoreverses = YES;
  1212. // [_btn.layer addAnimation:shakeAnimation forKey:nil];
  1213. }
  1214. }
  1215. #pragma mark - hide section button clicked
  1216. - (void)RefreshSection:(UIButton *)sender {
  1217. sender.enabled=false;
  1218. NSDictionary* section_json = nil;
  1219. NSDictionary* related_json = [self.detail_data objectForKey:@"related_model"];
  1220. if(related_json==nil)
  1221. {
  1222. section_json = [self.detail_data objectForKey:[NSString stringWithFormat:@"detail_%ld",(long)(sender.tag-1)]];
  1223. }
  1224. else
  1225. {
  1226. if(sender.tag==1)
  1227. {
  1228. section_json = related_json ;
  1229. }
  1230. else
  1231. {
  1232. section_json = [self.detail_data objectForKey:[NSString stringWithFormat:@"detail_%ld",(long)sender.tag-2]];
  1233. }
  1234. }
  1235. NSString* url = section_json[@"data_interface"];
  1236. NSMutableDictionary* params = [section_json[@"params"] mutableCopy];
  1237. if(params==nil)
  1238. params = [[NSMutableDictionary alloc] init];
  1239. NSUInteger ii[2] = {sender.tag,0};
  1240. NSIndexPath* indexpath= [NSIndexPath indexPathWithIndexes:ii length:2];
  1241. DetailTopicCell * cell=[self.detailTable cellForRowAtIndexPath:indexpath];
  1242. [cell begin_refresh];
  1243. // UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..."title:@"Refreshing"];
  1244. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1245. NSDictionary* editor_json = [iSalesNetwork commoneditor_partialrefresh:params url:url];
  1246. dispatch_async(dispatch_get_main_queue(), ^{
  1247. [cell end_refresh ];
  1248. // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  1249. sender.enabled=true;
  1250. if([[editor_json valueForKey:@"result"] intValue]==2)
  1251. {
  1252. [self.detail_data addEntriesFromDictionary:editor_json];
  1253. NSRange range = NSMakeRange(sender.tag, 1);
  1254. NSIndexSet *sectionToReload = [NSIndexSet indexSetWithIndexesInRange:range];
  1255. [self.detailTable reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationAutomatic];
  1256. }
  1257. else
  1258. {
  1259. [RAUtils message_alert:[editor_json valueForKey:@"err_msg"] title:nil controller:self] ;
  1260. }
  1261. });
  1262. });
  1263. // self.showMore=!self.showMore;
  1264. // NSRange range = NSMakeRange(1, 1);
  1265. // NSIndexSet *sectionToReload = [NSIndexSet indexSetWithIndexesInRange:range];
  1266. // [self.detailTable reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationAutomatic];
  1267. }
  1268. #pragma mark - Support scanner
  1269. -(void) onDecodedData:(NSString*) value
  1270. {
  1271. // UIApplication * app = [UIApplication sharedApplication];
  1272. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1273. // MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  1274. //
  1275. self.model_name = value;
  1276. self.product_id = nil;
  1277. self.use_model_name = true;
  1278. [self reload];
  1279. NSLog(@"color selected");
  1280. }
  1281. @end