HomeViewController.m 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. //
  2. // HomeViewController.m
  3. // RedAnt ERP Suite
  4. //
  5. // Created by Ray on 14-5-23.
  6. // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "HomeViewController.h"
  9. #import "RANetwork.h"
  10. #import "const.h"
  11. #import "HomeTableViewCellBanner.h"
  12. #import "DetailViewController.h"
  13. #import "MainViewController.h"
  14. #import "HomeTableViewCellButtonBanner.h"
  15. #import "WebViewController.h"
  16. #import "ImageUtils.h"
  17. #import "HomeTableViewCellSlide.h"
  18. //#import "MainViewController.h"
  19. @interface HomeViewController ()
  20. @end
  21. @implementation HomeViewController
  22. -(void) dealloc
  23. {
  24. NSLog(@"HOME VIEWCONTROLLER DEALLOC");
  25. }
  26. -(void) refresh_on_login
  27. {
  28. // do not refresh on login;
  29. }
  30. -(void) reload_container_getdata:(bool)update_data
  31. {
  32. [super reload_container_getdata:update_data];
  33. if(update_data)
  34. [ self LoadData];
  35. else
  36. [self.homeTable reloadData];
  37. }
  38. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  39. {
  40. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  41. if (self) {
  42. // Custom initialization
  43. }
  44. return self;
  45. }
  46. - (void)viewWillAppear:(BOOL)animated
  47. {
  48. [super viewWillAppear:animated];
  49. [[self navigationController] setNavigationBarHidden:YES animated:NO];
  50. if(!CGSizeEqualToSize(self.ori_size, [[UIScreen mainScreen] applicationFrame].size))
  51. {
  52. [self.homeTable reloadData];
  53. }
  54. // self.content = self.bak_content;
  55. // [self reload_container_getdata:false];
  56. }
  57. -(void) viewWillDisappear:(BOOL)animated
  58. {
  59. self.ori_size=[[UIScreen mainScreen] applicationFrame].size;
  60. // [[NSNotificationCenter defaultCenter] postNotificationName:CYCLESCROLL_STOPTIMMER object:nil];
  61. // self.bak_content = self.content;
  62. // self.content= nil;
  63. //// self.homeTable re
  64. // [self.homeTable reloadData];
  65. }
  66. -(void)manually_refresh
  67. {
  68. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  69. if(self.isrefreshing)
  70. {
  71. [reF endRefreshing];
  72. return;
  73. }
  74. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Refreshing"];
  75. [self performSelector:@selector(ReloadData) withObject:nil afterDelay:1];
  76. // DebugLog(@"refresh!!!!!!!!");
  77. }
  78. -(void) LoadData
  79. {
  80. dispatch_async(dispatch_get_main_queue(), ^{
  81. if(self.isrefreshing)
  82. {
  83. return;
  84. }
  85. self.isrefreshing=true;
  86. DebugLog(@"HOMEVIEW BEGIN LOAD");
  87. self.mum.hidden = false;
  88. [self.mum startAnimating];
  89. self.mum.center = self.view.center;
  90. self.label_net_err.hidden=true;
  91. self.homeTable.hidden=true;
  92. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  93. //debug
  94. // NSData* json =[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"home_template" ofType:@"json" ]];
  95. // NSError *error=nil;
  96. // NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  97. //
  98. // NSMutableDictionary* data=[jsobj mutableCopy];
  99. NSMutableDictionary* data=[[RANetwork load_HomePage:0 customid:0] mutableCopy];
  100. dispatch_async(dispatch_get_main_queue(), ^{
  101. [self.mum stopAnimating];
  102. self.content = data;
  103. [self.homeTable reloadData];
  104. self.homeTable.hidden=false;
  105. if([[data valueForKey:@"result"] intValue]==2)
  106. {
  107. DebugLog(@"HOMEVIEW JSON LOADED!");
  108. //debug
  109. // NSData* json =[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"button_banner1" ofType:@"json" ]];
  110. // NSError *error=nil;
  111. // NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  112. // [data setObject:jsobj forKey:@"view_3" ];
  113. }
  114. else
  115. if([[data valueForKey:@"result"] intValue]==RESULT_NET_ERROR)
  116. {
  117. self.label_net_err.hidden=false;
  118. }
  119. else
  120. {
  121. [RAUtils message_alert:[data valueForKey:@"err_msg"] title:@"Loading Home" controller:self] ;
  122. }
  123. self.isrefreshing=false;
  124. });
  125. });
  126. });
  127. }
  128. -(void) ReloadData
  129. {
  130. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  131. [reF endRefreshing];
  132. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  133. [self LoadData];
  134. }
  135. - (void)viewDidLoad
  136. {
  137. [super viewDidLoad];
  138. self.label_net_err.layer.borderColor = [UIColor darkGrayColor].CGColor;
  139. self.label_net_err.layer.borderWidth = 2.0;
  140. self.label_net_err.layer.cornerRadius=15;
  141. self.label_net_err.layer.masksToBounds=true;
  142. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(refresh_btn_click:)];
  143. // tap.minimumPressDuration = 0.8; //定义按的时间
  144. [self.label_net_err addGestureRecognizer:tap];
  145. // [[self navigationController] setNavigationBarHidden:YES animated:NO];
  146. UIRefreshControl *ref = [[UIRefreshControl alloc]init];
  147. ref.tag = 200 ;
  148. ref.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  149. ref.tintColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  150. // ref.hidden = true;
  151. [ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged];
  152. [self.homeTable addSubview:ref];
  153. RATreeView *treeView = [[RATreeView alloc] initWithFrame:self.treeContainer.frame];
  154. treeView.delegate = self;
  155. treeView.dataSource = self;
  156. treeView.separatorStyle = RATreeViewCellSeparatorStyleSingleLine;
  157. [treeView reloadData];
  158. // [treeView expandRowForItem:phone withRowAnimation:RATreeViewRowAnimationLeft]; //expands Row
  159. [treeView setBackgroundColor:UIColorFromRGB(0xF7F7F7)];
  160. self.treeView = treeView;
  161. [self.treeContainer addSubview:treeView];
  162. // UIApplication * app = [UIApplication sharedApplication];
  163. }
  164. //- (void)viewWillAppear:(BOOL)animated
  165. //{
  166. // [super viewWillAppear:animated];
  167. //
  168. // CGRect rt1 = self.treeContainer.bounds;
  169. // CGRect rt2 = self.treeContainer.frame;
  170. //
  171. //
  172. //
  173. // self.treeView.frame = self.treeContainer.bounds;
  174. //}
  175. -(void)viewDidAppear:(BOOL)animated
  176. {
  177. [super viewDidAppear:animated];
  178. // CGRect rt = self.treeContainer.bounds;
  179. // CGRect rt1 = self.treeContainer.frame;
  180. self.treeView.frame = self.treeContainer.bounds;
  181. // if([[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."][0] intValue] >= 7) {
  182. // CGRect statusBarViewRect = [[UIApplication sharedApplication] statusBarFrame];
  183. // float heightPadding = statusBarViewRect.size.height+self.navigationController.navigationBar.frame.size.height;
  184. // self.treeView.contentInset = UIEdgeInsetsMake(heightPadding, 0.0, 0.0, 0.0);
  185. // self.treeView.contentOffset = CGPointMake(0.0, -heightPadding);
  186. // }
  187. // self.homeTable.contentSize = self
  188. // self.scroll.contentSize = self.contentsize;
  189. }
  190. - (void)didReceiveMemoryWarning
  191. {
  192. [super didReceiveMemoryWarning];
  193. // Dispose of any resources that can be recreated.
  194. }
  195. - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
  196. {
  197. DebugLog(@"!!!!!!!!!!!!!!!!!!!!!route!!!!!!!!!!!!!!!!!!!!!");
  198. [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
  199. [self.homeTable reloadData];
  200. // CGSize contentsize = self.scroll.contentSize;
  201. // contentsize.height =330+2 * 230+29+20+20 +1 * 230+29+20+20 ;
  202. // UIInterfaceOrientation orientation =[UIApplication sharedApplication].statusBarOrientation;
  203. // if(orientation == UIDeviceOrientationLandscapeLeft ||orientation == UIDeviceOrientationLandscapeRight)
  204. // {
  205. // contentsize.height+=250;
  206. // }
  207. //
  208. // self.scroll.contentSize = contentsize;
  209. // // CGRect frame = self.view.frame;
  210. // // [self.carouselController.view removeFromSuperview];
  211. // // wait(2000);
  212. // // self.carouselController = [[FPCarouselNonXIBViewController alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 400)];
  213. // // [self.view addSubview:self.carouselController.view];
  214. // // int i = self.view.subviews.count;
  215. // // int b = 0;
  216. // // [self.carouselController updateFrame:CGRectMake(0, 0, self.view.frame.size.width, 400)];
  217. }
  218. #pragma mark - Table view data source
  219. - (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath
  220. {
  221. if([cell isKindOfClass:[HomeTableViewCellSlide class]])
  222. {
  223. HomeTableViewCellSlide* slidecell=(HomeTableViewCellSlide*)cell;
  224. [slidecell.imgScroll stopTimmer];
  225. }
  226. }
  227. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  228. {
  229. // UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
  230. // if (UIInterfaceOrientationIsLandscape(orientation)|| UIInterfaceOrientationIsPortrait(orientation))
  231. // {
  232. // self.orientation = orientation;
  233. // [self.collectionview.collectionViewLayout invalidateLayout];
  234. // DebugLog(@"routed");
  235. // }
  236. NSDictionary* view_json = [self.content objectForKey:[NSString stringWithFormat:@"view_%ld",(long)indexPath.section ]];
  237. NSString* type = [view_json valueForKeyPath:@"type"];
  238. if([type isEqualToString:@"slideview"])
  239. {
  240. // if(UIInterfaceOrientationIsPortrait(orientation))
  241. // return 400;
  242. // else
  243. // return 533;
  244. int i_width = 981;
  245. int i_height = 512;
  246. int ui_height = (tableView.frame.size.width * i_height)/i_width;
  247. return ui_height;
  248. }
  249. else if([type isEqualToString:@"topicview"])
  250. {
  251. // int item_count =[[view_json valueForKey:@"item_count"] intValue];
  252. // if(UIInterfaceOrientationIsPortrait(orientation))
  253. // return item_count/4 * 230 +49;
  254. return 310;
  255. }
  256. else if([type isEqualToString:@"banner"])
  257. {
  258. // if(UIInterfaceOrientationIsPortrait(orientation))
  259. // return 327;//[[view_json valueForKey:@"height"] intValue];
  260. int i_width = 498;
  261. int i_height = 197;
  262. int ui_height = (tableView.frame.size.width * i_height)/i_width;
  263. return ui_height;
  264. }
  265. else if([type isEqualToString:@"button_banner"])
  266. {
  267. int i_width = [[view_json valueForKey:@"width"]intValue];
  268. int i_height = [[view_json valueForKey:@"height"]intValue];
  269. float ui_height = (tableView.frame.size.width * i_height)/(float)i_width;
  270. return ui_height;
  271. }
  272. return 44;
  273. }
  274. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  275. {
  276. //-----debug!!
  277. // if(self.content !=nil)
  278. // return 2;
  279. return [[self.content valueForKeyPath:@"count"]intValue];
  280. }
  281. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  282. {
  283. return 1;
  284. }
  285. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  286. {
  287. NSDictionary* view_json = [self.content objectForKey:[NSString stringWithFormat:@"view_%ld",(long)indexPath.section ]];
  288. // NSString* type = [view_json valueForKeyPath:@"type"];
  289. NSString* action = [view_json valueForKeyPath:@"action"];
  290. if([action isEqualToString:@"itemSearch"])
  291. {
  292. NSString* covertype = [view_json valueForKeyPath:@"covertype"];
  293. UIApplication * app = [UIApplication sharedApplication];
  294. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  295. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  296. [main_vc switchToItemSearch:covertype];
  297. }
  298. }
  299. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  300. {
  301. UITableViewCell *cell = nil;
  302. NSDictionary* view_json = [self.content objectForKey:[NSString stringWithFormat:@"view_%ld",(long)indexPath.section ]];
  303. NSString* type = [view_json valueForKeyPath:@"type"];
  304. if([type isEqualToString:@"slideview"])
  305. {
  306. NSString *CellIdentifier = @"HomeTableViewCellSlide";
  307. cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  308. HomeTableViewCellSlide* slidecell = (HomeTableViewCellSlide*)cell;
  309. [slidecell.imgScroll stopTimmer];
  310. [slidecell.imgScroll removeFromSuperview];
  311. // for(UIView* v in cell.contentView.subviews)
  312. // {
  313. // [v removeFromSuperview];
  314. //
  315. // }
  316. int i_width = 981;
  317. int i_height = 512;
  318. int ui_height = (tableView.frame.size.width * i_height)/i_width;
  319. // HomeTableViewCellSlide* slidecell = (HomeTableViewCellSlide*)cell;
  320. // imgScroll.tag = 1394;
  321. //
  322. //
  323. // for (UIView *v in cell.contentView.subviews) {
  324. // if(v.tag==1394)
  325. // [v removeFromSuperview];
  326. // }
  327. CycleScrollView *imgScroll = [[CycleScrollView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, ui_height) animationDuration:5];
  328. imgScroll.autoresizingMask = UIViewAutoresizingFlexibleHeight |UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;
  329. int slide_item_count = [[view_json valueForKey:@"count"] intValue];
  330. //imgScroll.frame =CGRectMake(0, 5, width, 300);
  331. [imgScroll reset];
  332. // [imgScroll animationDuration:5];
  333. imgScroll.backgroundColor = [[UIColor purpleColor] colorWithAlphaComponent:0.1];
  334. for(int i = 0;i<slide_item_count;i++)
  335. {
  336. NSDictionary * slide_itemjson =[view_json objectForKey:[NSString stringWithFormat:@"item_%d",i] ];
  337. SliderPage * tempview = [[SliderPage alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, ui_height)];
  338. tempview.autoresizingMask = UIViewAutoresizingFlexibleHeight |UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;
  339. NSString* img_url = [slide_itemjson valueForKey:@"img"];
  340. tempview.contentMode = UIViewContentModeScaleToFill;
  341. tempview.img_url = img_url;
  342. // tempview.url =@"";
  343. // tempview.action_type = ;
  344. // tempview.params = ;
  345. [imgScroll addPage:tempview];
  346. }
  347. // return cell;
  348. // return cell;
  349. //Capturing strongly warring;
  350. // __block CycleScrollView *brimgScroll= imgScroll;
  351. // imgScroll.fetchContentViewAtIndex = ^UIView *(NSInteger pageIndex){
  352. //
  353. //
  354. //
  355. // SliderPage* page =brimgScroll.arr_Pages[pageIndex];
  356. // [page loadImage];
  357. //
  358. //
  359. //
  360. // return page ;
  361. //
  362. // // UIImageView * v= viewsArray[pageIndex];
  363. // // return viewsArray[pageIndex];
  364. // };
  365. // imgScroll.totalPagesCount = ^NSInteger(void){
  366. // return brimgScroll.arr_Pages.count; //viewsArray.count;
  367. // };
  368. [imgScroll setTotalPagesCount];
  369. imgScroll.TapActionBlock = ^(NSInteger pageIndex){
  370. DebugLog(@"点击了第%ld个",(long)pageIndex);
  371. // NSDictionary * slide_itemjson =[view_json objectForKey:[NSString stringWithFormat:@"item_%d",pageIndex] ];
  372. //
  373. // NSString*
  374. //
  375. // DetailViewController* dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController" ];
  376. //// dvc
  377. //
  378. // dvc.product_id=@"5008";
  379. // dvc.category_id=@"003001";
  380. // dvc.ispush = true;
  381. //
  382. // [dvc reload];
  383. // [self.navigationController pushViewController:dvc animated:true];
  384. };
  385. // HomeTableViewCellSlide* slidecell = (HomeTableViewCellSlide*)cell;
  386. // [slidecell.imgScroll stopTimmer];
  387. // [slidecell.imgScroll removeFromSuperview];
  388. // imgScroll.tag = 1394;
  389. //
  390. //
  391. // for (UIView *v in cell.contentView.subviews) {
  392. // if(v.tag==1394)
  393. // [v removeFromSuperview];
  394. // }
  395. //
  396. slidecell.imgScroll = imgScroll;
  397. // return cell;
  398. [cell.contentView addSubview:imgScroll];
  399. }
  400. else if([type isEqualToString:@"topicview"])
  401. {
  402. // NSString *CellIdentifier = @"HomeTableViewCellTopic";
  403. // cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  404. //
  405. // // NSDictionary * topicview_json =[topicjson objectForKey:[NSString stringWithFormat:@"topicview_%d",i] ];
  406. // int item_count =[[view_json valueForKey:@"item_count"] intValue];
  407. // NSString * topictitle =[view_json valueForKey:@"title"];
  408. //
  409. //
  410. // TopicViewController* topicview = [self.storyboard instantiateViewControllerWithIdentifier:@"TopicViewController"];
  411. // topicview.content = view_json;
  412. // [topicview itemCount:item_count];
  413. //
  414. // topicview.view.frame = CGRectMake(0, 0, self.view.frame.size.width, item_count/4 * 230 +49);
  415. // CGRect rt = topicview.TopicCollection.frame;// =CGRectMake(0, 20+29, self.view.frame.size.width, item_count/4 * 230);
  416. // rt.size.height = item_count/4 * 230;
  417. // topicview.TopicCollection.frame = rt;
  418. // // [topicview.TopicCollection sizeToFit];
  419. // // [topicview.view sizeToFit];
  420. // topicview.topicTitle.text = topictitle;
  421. // topicview.rootViewController = self.parentViewController;
  422. //
  423. //
  424. //
  425. // [cell.contentView addSubview:topicview.view];
  426. NSString *CellIdentifier = @"HomeTableViewCellTopic";
  427. // NSDictionary* section_json = [self.detail_data objectForKey:@"related_model"];
  428. HomeTableViewCellTopic * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  429. cell.itemdelegate = self;
  430. cell.related_data = view_json;
  431. [cell.topicCollectionView reloadData];
  432. NSString * topictitle =[view_json valueForKey:@"title"];
  433. cell.label_title.text =topictitle;
  434. cell.backgroundColor = [UIColor whiteColor];
  435. return cell;
  436. }
  437. else if([type isEqualToString:@"banner"])
  438. {
  439. NSString *CellIdentifier = @"HomeTableViewCellBanner";
  440. cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  441. HomeTableViewCellBanner * bannercell=cell;
  442. NSString* img_url =[view_json valueForKeyPath:@"img"];
  443. NSString* file_name=[img_url lastPathComponent];
  444. NSData* img_data=[ImageUtils load_cached_img:file_name loadFrom:img_url size:bannercell.BannerImageView.frame.size allow_enlarge:false];
  445. if(img_data!=nil)
  446. {
  447. UIImage * img =[UIImage imageWithData:img_data];
  448. bannercell.BannerImageView.image = img;
  449. }
  450. else
  451. {
  452. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  453. NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  454. dispatch_async(dispatch_get_main_queue(), ^{
  455. if(downloadimg_data!=nil)
  456. {
  457. [iSalesDB cache_img:downloadimg_data filename:file_name saveTo:img_url];
  458. UIImage * img =[UIImage imageWithData:downloadimg_data];
  459. bannercell.BannerImageView.image = img;
  460. }
  461. else
  462. bannercell.BannerImageView.image=[UIImage imageNamed:@"notfound_l"];
  463. });
  464. });
  465. }
  466. cell.backgroundColor = [UIColor whiteColor];
  467. }
  468. else if([type isEqualToString:@"button_banner"])
  469. {
  470. // CellIdentifier = @"CommonEditorCellEdit";
  471. // UITableViewCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  472. // cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  473. // cell.backgroundColor = [UIColor whiteColor];
  474. // return cell;
  475. int cell_width = [[view_json valueForKey:@"width"]intValue];
  476. int cell_height = [[view_json valueForKey:@"height"]intValue];
  477. float ui_height = (tableView.frame.size.width * cell_height)/(float)cell_width;
  478. float ui_width = tableView.frame.size.width ;
  479. NSString *CellIdentifier = @"HomeTableViewCellButtonBanner";
  480. cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  481. // HomeTableViewCellButtonBanner * bannercell=(HomeTableViewCellButtonBanner*)cell;
  482. for(UIView* v in cell.contentView.subviews)
  483. {
  484. [v removeFromSuperview];
  485. }
  486. int count = [[view_json valueForKey:@"count"] intValue];
  487. for(int i_item = 0;i_item<count;i_item++)
  488. {
  489. NSDictionary* item_json = [view_json objectForKey:[NSString stringWithFormat:@"item_%d",i_item]];
  490. int x = [[item_json valueForKey:@"x"]intValue];
  491. int y = [[item_json valueForKey:@"y"]intValue];
  492. int i_width = [[item_json valueForKey:@"width"]intValue];
  493. int i_height = [[item_json valueForKey:@"height"]intValue];
  494. float f_width = i_width/(cell_width/ui_width);
  495. float f_htight = i_height/(cell_height/ui_height);
  496. float fx = x/(cell_width/ui_width);
  497. float fy = y/(cell_height/ui_height);
  498. UIButton* btn_item = [[UIButton alloc] initWithFrame:CGRectMake(fx, fy, f_width, f_htight)];
  499. long tag = indexPath.section*1000+ indexPath.row*100+i_item;
  500. btn_item.tag = tag;
  501. [btn_item addTarget:self action:@selector(ButtonBannerClick:) forControlEvents:UIControlEventTouchUpInside];
  502. [cell.contentView addSubview:btn_item];
  503. NSString* img_url =[item_json valueForKeyPath:@"img"];
  504. NSString* file_name=[img_url lastPathComponent];
  505. NSData* img_data=[ImageUtils load_cached_img:file_name loadFrom:img_url size:btn_item.frame.size allow_enlarge:false];
  506. if(img_data!=nil)
  507. {
  508. UIImage * img =[UIImage imageWithData:img_data];
  509. [btn_item setBackgroundImage:img forState:UIControlStateNormal];
  510. }
  511. else
  512. {
  513. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  514. NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  515. dispatch_async(dispatch_get_main_queue(), ^{
  516. if(downloadimg_data!=nil)
  517. {
  518. [iSalesDB cache_img:downloadimg_data filename:file_name saveTo:img_url];
  519. UIImage * img =[UIImage imageWithData:downloadimg_data];
  520. [btn_item setBackgroundImage:img forState:UIControlStateNormal];
  521. }
  522. else
  523. [btn_item setBackgroundImage:[UIImage imageNamed:@"notfound_l"] forState:UIControlStateNormal];
  524. });
  525. });
  526. }
  527. }
  528. cell.backgroundColor = [UIColor whiteColor];
  529. // // NSDictionary * topicview_json =[topicjson objectForKey:[NSString stringWithFormat:@"topicview_%d",i] ];
  530. // int item_count =[[view_json valueForKey:@"item_count"] intValue];
  531. // NSString * topictitle =[view_json valueForKey:@"title"];
  532. //
  533. //
  534. //
  535. //
  536. //
  537. //
  538. // TopicViewController* topicview = [self.storyboard instantiateViewControllerWithIdentifier:@"TopicViewController"];
  539. // topicview.content = view_json;
  540. // [topicview itemCount:item_count];
  541. //
  542. // topicview.view.frame = CGRectMake(0, 0, self.view.frame.size.width, item_count/4 * 230 +49);
  543. // CGRect rt = topicview.TopicCollection.frame;// =CGRectMake(0, 20+29, self.view.frame.size.width, item_count/4 * 230);
  544. // rt.size.height = item_count/4 * 230;
  545. // topicview.TopicCollection.frame = rt;
  546. // // [topicview.TopicCollection sizeToFit];
  547. // // [topicview.view sizeToFit];
  548. // topicview.topicTitle.text = topictitle;
  549. // topicview.rootViewController = self.parentViewController;
  550. //
  551. //
  552. //
  553. //
  554. // [cell.contentView addSubview:topicview.view];
  555. }
  556. return cell;
  557. }
  558. -(void)ButtonBannerClick:(id)sender{
  559. //这个sender其实就是UIButton,因此通过sender.tag就可以拿到刚才的参数
  560. int tag = [sender tag];
  561. int section = tag/1000;
  562. int row = (tag-section*1000)/100;
  563. int idx =(tag-section*1000 - row*100);
  564. NSDictionary* view_json = [self.content objectForKey:[NSString stringWithFormat:@"view_%ld",(long)section ]];
  565. NSDictionary* item_json = [view_json objectForKey:[NSString stringWithFormat:@"item_%d",idx]];
  566. NSString* action = [item_json valueForKeyPath:@"action"];
  567. [Singleton sharedInstance].homeClickedItemName = item_json[@"description"];
  568. if([action isEqualToString:@"itemSearch"])
  569. {
  570. [Singleton sharedInstance].homeItemClick = YES;
  571. NSString* covertype = [item_json valueForKeyPath:@"covertype"];
  572. UIApplication * app = [UIApplication sharedApplication];
  573. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  574. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  575. [main_vc switchToItemSearch:covertype];
  576. }
  577. else if([action isEqualToString:@"category"])
  578. {
  579. [Singleton sharedInstance].homeItemClick = YES;
  580. NSString* cid = [item_json valueForKeyPath:@"category_id"];
  581. UIApplication * app = [UIApplication sharedApplication];
  582. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  583. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  584. [main_vc switchToCagegory:cid];
  585. }
  586. else if([action isEqualToString:@"webview"])
  587. {
  588. NSString* url = [item_json valueForKeyPath:@"url"];
  589. NSString* title = [item_json valueForKeyPath:@"title"];
  590. // UIApplication * app = [UIApplication sharedApplication];
  591. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  592. // MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  593. //
  594. //
  595. WebViewController *ViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"WebViewController"];
  596. ViewController.url = url;
  597. ViewController.title = title;
  598. [self.navigationController pushViewController:ViewController animated:YES];
  599. }
  600. }
  601. #pragma mark TreeView Delegate methods
  602. - (CGFloat)treeView:(RATreeView *)treeView heightForRowForItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
  603. {
  604. return 47;
  605. }
  606. - (NSInteger)treeView:(RATreeView *)treeView indentationLevelForRowForItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
  607. {
  608. return 3 * treeNodeInfo.treeDepthLevel;
  609. }
  610. - (BOOL)treeView:(RATreeView *)treeView shouldExpandItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
  611. {
  612. return YES;
  613. }
  614. - (BOOL)treeView:(RATreeView *)treeView shouldItemBeExpandedAfterDataReload:(id)item treeDepthLevel:(NSInteger)treeDepthLevel
  615. {
  616. if ([item isEqual:self.expanded]) {
  617. return YES;
  618. }
  619. return NO;
  620. }
  621. - (void)treeView:(RATreeView *)treeView didSelectRowForItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
  622. {
  623. NSDictionary *data = item;
  624. int count = [[data valueForKey:@"count"] intValue];
  625. if(count==0)
  626. {
  627. // NSString* category = [data valueForKey:@"id"];
  628. // if (self.CateMenu_delegate && [self.CateMenu_delegate respondsToSelector:@selector(SelectCategory:)]) {
  629. // [self.CateMenu_delegate SelectCategory:category];
  630. // }
  631. // /* MainViewController* pvc = (MainViewController*)self.rootViewController;
  632. // [pvc switchToCagegory:category];*/
  633. // [self dismissViewControllerAnimated:YES
  634. // completion:^{
  635. // //一定要移除手势 否则下次 没有子视图的时候 点击 会崩溃拉
  636. // AppDelegate *app = [[UIApplication sharedApplication] delegate];
  637. // [app.window removeGestureRecognizer:self.tapGesture];
  638. // }];
  639. }
  640. // if (item == nil) {
  641. // NSDictionary * ret =[self.categoryMenu objectForKey:[NSString stringWithFormat:@"category_%d",index] ];
  642. //
  643. // }
  644. // else
  645. // data=[data objectForKey:[NSString stringWithFormat:@"category_%d",index]];
  646. DebugLog(@"menu select %@",data);
  647. }
  648. - (void)treeView:(RATreeView *)treeView willDisplayCell:(UITableViewCell *)cell forItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
  649. {
  650. if (treeNodeInfo.treeDepthLevel == 0) {
  651. cell.backgroundColor = UIColorFromRGB(0xF7F7F7);
  652. } else if (treeNodeInfo.treeDepthLevel == 1) {
  653. cell.backgroundColor = UIColorFromRGB(0xD1EEFC);
  654. } else if (treeNodeInfo.treeDepthLevel == 2) {
  655. cell.backgroundColor = UIColorFromRGB(0xE0F8D8);
  656. }
  657. }
  658. #pragma mark TreeView Data Source
  659. - (UITableViewCell *)treeView:(RATreeView *)treeView cellForItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
  660. {
  661. // NSInteger numberOfChildren = [treeNodeInfo.children count];
  662. UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
  663. // if(numberOfChildren>0)
  664. // cell.detailTextLabel.text = [NSString stringWithFormat:@"Number of children %@", [@(numberOfChildren) stringValue]];
  665. cell.textLabel.text = [((NSDictionary *)item) valueForKey:@"title"];
  666. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  667. if (treeNodeInfo.treeDepthLevel == 0) {
  668. cell.detailTextLabel.textColor = [UIColor blackColor];
  669. }
  670. return cell;
  671. }
  672. - (NSInteger)treeView:(RATreeView *)treeView numberOfChildrenOfItem:(id)item
  673. {
  674. if (item == nil) {
  675. UIApplication * app = [UIApplication sharedApplication];
  676. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  677. return [[appDelegate.categoryMenu valueForKey:@"count" ] intValue];
  678. // return [self.data count];
  679. }
  680. NSDictionary* data = item;
  681. return [[data valueForKey:@"count"] intValue];
  682. // RADataObject *data = item;
  683. // return [data.children count];
  684. }
  685. - (id)treeView:(RATreeView *)treeView child:(NSInteger)index ofItem:(id)item
  686. {
  687. NSDictionary *data = item;
  688. if (item == nil) {
  689. UIApplication * app = [UIApplication sharedApplication];
  690. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  691. NSDictionary * ret =[appDelegate.categoryMenu objectForKey:[NSString stringWithFormat:@"category_%ld",(long)index] ];
  692. return ret;
  693. }
  694. return[data objectForKey:[NSString stringWithFormat:@"category_%ld",(long)index]];
  695. /*
  696. RADataObject *data = item;
  697. if (item == nil) {
  698. return [self.data objectAtIndex:index];
  699. }
  700. return [data.children objectAtIndex:index];
  701. */
  702. }
  703. #pragma mark - Topic cell delegate
  704. -(void) TopicItemClicked:(NSString*) product_id category:(NSString*) category
  705. {
  706. DetailViewController* dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController" ];
  707. // dvc
  708. dvc.product_id=product_id;
  709. dvc.category_id=category;
  710. dvc.ispush=true;
  711. [dvc reload];
  712. [self.navigationController pushViewController:dvc animated:true];
  713. }
  714. //- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  715. //{
  716. //
  717. // MainViewController* pvc = (MainViewController*)self.parentViewController;
  718. // // [pvc categoryMenuSelected:indexPath.row];
  719. //}
  720. - (void)reRefreshView {
  721. [self.homeTable reloadData];
  722. }
  723. #pragma mark - RA_NOTIFICAITON
  724. -(void) refresh_ui
  725. {
  726. [self.homeTable reloadData];
  727. }
  728. -(void) reload_data
  729. {
  730. [self LoadData];
  731. }
  732. @end