RAViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. //The MIT License (MIT)
  2. //
  3. //Copyright (c) 2013 Rafał Augustyniak
  4. //
  5. //Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. //this software and associated documentation files (the "Software"), to deal in
  7. //the Software without restriction, including without limitation the rights to
  8. //use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. //the Software, and to permit persons to whom the Software is furnished to do so,
  10. //subject to the following conditions:
  11. //
  12. //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. //FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. //COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. //IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. //CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. //
  19. #import "RAViewController.h"
  20. #import "RATreeView.h"
  21. //#import "RADataObject.h"
  22. #import "AppDelegate.h"
  23. #import "MainViewController.h"
  24. #define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
  25. @interface RAViewController () <RATreeViewDelegate, RATreeViewDataSource,UIGestureRecognizerDelegate>
  26. //@property (strong, nonatomic) NSArray *data;
  27. @property (strong,nonatomic)NSDictionary* categoryMenu;
  28. @property (strong, nonatomic) id expanded;
  29. @property (weak, nonatomic) RATreeView *treeView;
  30. @end
  31. @implementation RAViewController
  32. -(void) reload_container_getdata:(bool) update_data
  33. {
  34. [super reload_container_getdata:update_data];
  35. // if(update_data)
  36. // [self reload_data];
  37. // else
  38. // {
  39. // [self.itemListTable reloadData];
  40. // [self refresh_total];
  41. // }
  42. if(update_data==false)
  43. return;
  44. UIApplication * app = [UIApplication sharedApplication];
  45. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  46. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  47. NSDictionary* category=[iSalesNetwork request_Cagegory];
  48. dispatch_async(dispatch_get_main_queue(), ^{
  49. if([[category valueForKey:@"result"] intValue]==2)
  50. {
  51. appDelegate.categoryMenu = category;
  52. [self SetMenu:appDelegate.categoryMenu ];
  53. }
  54. else
  55. if([[category valueForKey:@"result"] intValue]==RESULT_NET_ERROR)
  56. {
  57. [self SetMenu:nil ];
  58. }
  59. else
  60. {
  61. [RAUtils message_alert:[category valueForKey:@"err_msg"] title:@"Load Category Menu" controller:self] ;
  62. }
  63. });
  64. });
  65. }
  66. -(void) SetMenu:(NSDictionary*) menu
  67. {
  68. if(menu==nil)
  69. {
  70. self.label_net_err.hidden=false;
  71. self.treeView.hidden = true;
  72. }
  73. else
  74. {
  75. self.label_net_err.hidden=true;
  76. self.treeView.hidden = false;
  77. }
  78. self.categoryMenu = menu;
  79. [self.treeView reloadData];
  80. }
  81. - (IBAction)CloseClick:(id)sender {
  82. [self dismissViewControllerAnimated:true completion:nil];
  83. }
  84. - (void)viewDidLoad
  85. {
  86. [super viewDidLoad];
  87. if(self.categoryMenu==nil)
  88. self.refresh_type = REFRESH_DATA;
  89. else
  90. self.refresh_type = REFRESH_VIEW;
  91. self.label_net_err.layer.borderColor = [UIColor darkGrayColor].CGColor;
  92. self.label_net_err.layer.borderWidth = 2.0;
  93. self.label_net_err.layer.cornerRadius=15;
  94. self.label_net_err.layer.masksToBounds=true;
  95. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(refresh_btn_click:)];
  96. // tap.minimumPressDuration = 0.8; //定义按的时间
  97. [self.label_net_err addGestureRecognizer:tap];
  98. UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  99. style:UIBarButtonItemStylePlain
  100. target:self
  101. action:@selector( CloseClick:)];
  102. self.navigationItem.rightBarButtonItem = closeButton;
  103. // CGRect frame = self.view.frame;
  104. // frame.origin.y = 200;//self.headerview.frame.size.height;
  105. // frame.size.height = frame.size.height - self.headerview.frame.size.height;
  106. RATreeView *treeView = [[RATreeView alloc] initWithFrame:self.treeContainer.frame];
  107. treeView.delegate = self;
  108. treeView.dataSource = self;
  109. treeView.separatorStyle = RATreeViewCellSeparatorStyleSingleLine;
  110. [treeView reloadData];
  111. // [treeView expandRowForItem:phone withRowAnimation:RATreeViewRowAnimationLeft]; //expands Row
  112. [treeView setBackgroundColor: [UIColor whiteColor]];
  113. self.treeView = treeView;
  114. [self.treeContainer addSubview:treeView];
  115. // UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handletapPressGesture:)];
  116. // AppDelegate *app = [[UIApplication sharedApplication] delegate];
  117. // [app.window addGestureRecognizer:tapGesture];
  118. // tapGesture.delegate = self;
  119. // self.tapGesture = tapGesture;
  120. }
  121. //-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
  122. //{
  123. // if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {//如果当前是tableView
  124. // //做自己想做的事
  125. // return NO;
  126. // }
  127. // NSLog(NSStringFromClass([touch.view class]));
  128. // return YES;
  129. //}
  130. //
  131. //-(void)handletapPressGesture:(UITapGestureRecognizer*)sender{
  132. // CGPoint point = [sender locationInView:self.view];
  133. // if (point.x<self.view.frame.origin.x || point.x >self.view.frame.origin.x+self.view.frame.size.width||point.y<self.view.frame.origin.y||point.y>self.view.frame.origin.y+self.view.frame.size.height) {
  134. // [self dismissViewControllerAnimated:YES
  135. // completion:^{
  136. // //一定要移除手势 否则下次 没有子视图的时候 点击 会崩溃拉
  137. // AppDelegate *app = [[UIApplication sharedApplication] delegate];
  138. // [app.window removeGestureRecognizer:sender];
  139. // }];
  140. // }
  141. //
  142. //}
  143. - (void)viewWillAppear:(BOOL)animated
  144. {
  145. [super viewWillAppear:animated];
  146. // if([[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."][0] intValue] >= 7) {
  147. // CGRect statusBarViewRect = [[UIApplication sharedApplication] statusBarFrame];
  148. // float heightPadding = statusBarViewRect.size.height+self.navigationController.navigationBar.frame.size.height;
  149. // self.treeView.contentInset = UIEdgeInsetsMake(heightPadding, 0.0, 0.0, 0.0);
  150. // self.treeView.contentOffset = CGPointMake(0.0, -heightPadding);
  151. // }
  152. self.treeView.frame = self.treeContainer.bounds;
  153. }
  154. #pragma mark TreeView Delegate methods
  155. - (CGFloat)treeView:(RATreeView *)treeView heightForRowForItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
  156. {
  157. return 47;
  158. }
  159. - (NSInteger)treeView:(RATreeView *)treeView indentationLevelForRowForItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
  160. {
  161. return 3 * treeNodeInfo.treeDepthLevel;
  162. }
  163. - (BOOL)treeView:(RATreeView *)treeView shouldExpandItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
  164. {
  165. return YES;
  166. }
  167. - (BOOL)treeView:(RATreeView *)treeView shouldItemBeExpandedAfterDataReload:(id)item treeDepthLevel:(NSInteger)treeDepthLevel
  168. {
  169. if ([item isEqual:self.expanded]) {
  170. return YES;
  171. }
  172. return NO;
  173. }
  174. - (void)treeView:(RATreeView *)treeView didSelectRowForItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
  175. {
  176. NSDictionary *data = item;
  177. int count = [[data valueForKey:@"count"] intValue];
  178. if(count==0)
  179. {
  180. NSString* category = [data valueForKey:@"id"];
  181. if (self.CateMenu_delegate && [self.CateMenu_delegate respondsToSelector:@selector(SelectCategory:)]) {
  182. [self.CateMenu_delegate SelectCategory:category];
  183. }
  184. /* MainViewController* pvc = (MainViewController*)self.rootViewController;
  185. [pvc switchToCagegory:category];*/
  186. [self dismissViewControllerAnimated:YES
  187. completion:^{
  188. //一定要移除手势 否则下次 没有子视图的时候 点击 会崩溃拉
  189. // AppDelegate *app = [[UIApplication sharedApplication] delegate];
  190. // [app.window removeGestureRecognizer:self.tapGesture];
  191. }];
  192. }
  193. // if (item == nil) {
  194. // NSDictionary * ret =[self.categoryMenu objectForKey:[NSString stringWithFormat:@"category_%d",index] ];
  195. //
  196. // }
  197. // else
  198. // data=[data objectForKey:[NSString stringWithFormat:@"category_%d",index]];
  199. NSLog(@"menu select %@",data);
  200. }
  201. - (void)treeView:(RATreeView *)treeView willDisplayCell:(UITableViewCell *)cell forItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
  202. {
  203. if (treeNodeInfo.treeDepthLevel == 0) {
  204. cell.backgroundColor = [UIColor whiteColor];
  205. } else if (treeNodeInfo.treeDepthLevel == 1) {
  206. cell.backgroundColor = UIColorFromRGB(0xD1EEFC);
  207. } else if (treeNodeInfo.treeDepthLevel == 2) {
  208. cell.backgroundColor = UIColorFromRGB(0xE0F8D8);
  209. }
  210. }
  211. #pragma mark TreeView Data Source
  212. - (UITableViewCell *)treeView:(RATreeView *)treeView cellForItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
  213. {
  214. // NSInteger numberOfChildren = [treeNodeInfo.children count];
  215. UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
  216. // if(numberOfChildren>0)
  217. // cell.detailTextLabel.text = [NSString stringWithFormat:@"Number of children %@", [@(numberOfChildren) stringValue]];
  218. cell.textLabel.text = [((NSDictionary *)item) valueForKey:@"title"];
  219. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  220. if (treeNodeInfo.treeDepthLevel == 0) {
  221. cell.detailTextLabel.textColor = [UIColor blackColor];
  222. }
  223. return cell;
  224. }
  225. - (NSInteger)treeView:(RATreeView *)treeView numberOfChildrenOfItem:(id)item
  226. {
  227. if (item == nil) {
  228. return [[self.categoryMenu valueForKey:@"count" ] intValue];
  229. // return [self.data count];
  230. }
  231. NSDictionary* data = item;
  232. return [[data valueForKey:@"count"] intValue];
  233. // RADataObject *data = item;
  234. // return [data.children count];
  235. }
  236. - (id)treeView:(RATreeView *)treeView child:(NSInteger)index ofItem:(id)item
  237. {
  238. NSDictionary *data = item;
  239. if (item == nil) {
  240. NSDictionary * ret =[self.categoryMenu objectForKey:[NSString stringWithFormat:@"category_%ld",(long)index] ];
  241. return ret;
  242. }
  243. return[data objectForKey:[NSString stringWithFormat:@"category_%ld",(long)index]];
  244. /*
  245. RADataObject *data = item;
  246. if (item == nil) {
  247. return [self.data objectAtIndex:index];
  248. }
  249. return [data.children objectAtIndex:index];
  250. */
  251. }
  252. @end