HomeViewController.m 32 KB

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