ResultViewController.m 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  1. //
  2. // ViewController.m
  3. // phoneTest
  4. //
  5. // Created by Ray on 01/09/2017.
  6. //
  7. //
  8. #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]
  9. #import "ResultViewController.h"
  10. #import "FullyShowViewController.h"
  11. #import "RANetwork.h"
  12. #import "RAUtils.h"
  13. #import "JLRefreshHeader.h"
  14. #import "JLRefreshFooter.h"
  15. //#import "OrderDetailViewController.h"
  16. //#import "KVViewController.h"
  17. #import <QuickLook/QuickLook.h>
  18. #import "config.h"
  19. #import "RAConvertor.h"
  20. //#import "RTLabel.h"
  21. const int delta = 25;
  22. @interface ResultViewController () <JLRefreshDelegate,QLPreviewControllerDataSource,QLPreviewControllerDelegate,UIViewControllerPreviewingDelegate>
  23. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *w_constraint;
  24. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *h_constraint;
  25. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *trail_constraint;
  26. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *lead_constraint;
  27. @end
  28. @implementation ResultViewController
  29. - (void)labelClick {
  30. NSLog(@"underlineLabel被点击了");
  31. }
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. self.titleLabel.text = nil;
  35. // self.labelTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTapAction:)];
  36. //
  37. // self.labelTap.numberOfTouchesRequired = 1; //手指数
  38. // self.labelTap.numberOfTapsRequired = 2; //tap次数
  39. //
  40. //
  41. //
  42. //
  43. // self.cellDoubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellDoubleTapAction:)];
  44. //
  45. // self.cellDoubleTap.numberOfTouchesRequired = 1; //手指数
  46. // self.cellDoubleTap.numberOfTapsRequired = 2; //tap次数
  47. // UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self
  48. // action:nil];
  49. // singleFingerOne.numberOfTouchesRequired = 1; //手指数
  50. // singleFingerOne.numberOfTapsRequired = 1; //tap次数
  51. //
  52. //
  53. // singleFingerTwo.delegate = self;
  54. self.label = [MDHTMLLabel new];
  55. // NSString* Path = [[NSBundle mainBundle] pathForResource:@"layout.json" ofType:nil];
  56. //
  57. // NSData *data = [NSData dataWithContentsOfFile:Path];
  58. // self.content_layout = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  59. //
  60. //
  61. // Path = [[NSBundle mainBundle] pathForResource:@"data.json" ofType:nil];
  62. //
  63. // data = [NSData dataWithContentsOfFile:Path];
  64. // self.content_data = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  65. // int width=[self.content_layout[@"header"][@"width"] intValue];
  66. // if(width<self.tableview.frame.size.width)
  67. // width=self.tableview.frame.size.width;
  68. //
  69. // CGRect frame = CGRectMake(self.tableview.frame.origin.x, self.tableview.frame.origin.y, width, self.tableview.frame.size.height);
  70. // self.tableview.frame=frame;
  71. // self.scrollview.contentSize=self.tableview.frame.size;
  72. // Do any additional setup after loading the view, typically from a nib.
  73. self.automaticallyAdjustsScrollViewInsets = NO;
  74. self.tableview.tableFooterView = [UIView new];
  75. [self setupNavigationBar];
  76. [self setupTableRefreshView];
  77. [self loadContent];
  78. // self.tableview.tableHeaderView = [self get_tableHeader];
  79. }
  80. - (void)presses3DTouchForIndexPath:(NSIndexPath *)indexPath {
  81. }
  82. -(void)cellDoubleTapAction:(UIGestureRecognizer*)gestureRecognizer
  83. {
  84. NSAssert(true, ([NSString stringWithFormat:@"%s cellDoubleTapAction not impl",object_getClassName([self class])]));
  85. }
  86. //{
  87. // NSLog(@"cell double tap");
  88. // UITableViewCell* cell = (UITableViewCell*)[gestureRecognizer view];
  89. // NSIndexPath* indexPath=[self.tableview indexPathForCell:cell];
  90. // //strongSelf.content_action
  91. // if(self.content_action!=nil)
  92. // {
  93. //
  94. // if(self.content_action.count==1)
  95. // {
  96. // NSDictionary* action =self.content_action.firstObject;
  97. // NSString * module = action[@"module"];
  98. // if([module isEqualToString:@"quick_look"])
  99. // {
  100. //
  101. // // +(NSString*)download_file : (NSMutableDictionary*) params url:(NSString*) url
  102. // NSMutableDictionary *params = action[@"params"];
  103. // NSString* URL =action[@"url"];
  104. // __weak typeof(self) weakSelf = self;
  105. // dispatch_async(dispatch_get_global_queue(0, 0), ^{
  106. //
  107. //
  108. // NSString *path = [RANetwork download_file:params url:URL];
  109. // weakSelf.documentPath = path;
  110. //
  111. // if (path) {
  112. // dispatch_async(dispatch_get_main_queue(), ^{
  113. // [weakSelf.navigationController pushViewController:weakSelf.quickLook animated:NO];
  114. // });
  115. // }
  116. //
  117. // });
  118. // }
  119. // else if([module isEqualToString:@"kv_detail"])
  120. // {
  121. // NSMutableDictionary *params = [NSMutableDictionary new];
  122. // params[@"query_id"] = self.params[@"query_id"];
  123. //
  124. // NSMutableDictionary *criteria =[NSMutableDictionary new];
  125. // for(NSString* key in [action[@"params"] allKeys] )
  126. // {
  127. // NSArray* item =self.content_data[[NSString stringWithFormat:@"item_%ld",indexPath.row]];
  128. // int idx =[action[@"params"][key] intValue];
  129. // criteria[key]=item[ idx];
  130. // }
  131. //
  132. //
  133. // params[@"criteria"]=[RAConvertor dict2string:criteria] ;
  134. //
  135. //
  136. //
  137. // UIAlertView * waitalert = [RAUtils waiting_alert:@"Loading..." title:@"Please wait"];
  138. //
  139. //
  140. // __weak typeof(self) weakself = self;
  141. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  142. //
  143. //
  144. //
  145. // NSDictionary* json =[RANetwork kv_detail:params];
  146. //
  147. // dispatch_async(dispatch_get_main_queue(), ^{
  148. // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  149. //
  150. //
  151. // if([[json valueForKey:@"result"] intValue]==2)
  152. // {
  153. // KVViewController *vc = [[UIStoryboard storyboardWithName:@"Detail" bundle:nil] instantiateViewControllerWithIdentifier:@"KVViewController"];
  154. // vc.content = [json mutableCopy];
  155. // [weakself.navigationController pushViewController:vc animated:NO];
  156. //
  157. // // weakself.content_data_download = [weakself CopyDirty:weakself.content_data_download to:[editor_json mutableCopy]];//[editor_json mutableCopy];
  158. // //
  159. // // weakself.content_data_control = [weakself translate_json:weakself.content_data_download changed: weakself.changed_data];
  160. // //
  161. // //
  162. // // [weakself download_success];
  163. // // [weakself.editorTable reloadData];
  164. //
  165. // }
  166. // else
  167. // {
  168. // [RAUtils message_alert:json[@"msg"] title:nil controller:weakself] ;
  169. // }
  170. //
  171. //
  172. //
  173. //
  174. // });
  175. // });
  176. // }
  177. // else if([module isEqualToString:@"order_detail"])
  178. // {
  179. //
  180. // OrderDetailViewController *vc = [[UIStoryboard storyboardWithName:@"Detail" bundle:nil] instantiateViewControllerWithIdentifier:@"OrderDetailViewController"];
  181. //
  182. //
  183. // vc.url_type = URL_REMOTE;
  184. // vc.request_url=URL_ORDER_DETAIL;
  185. //
  186. //
  187. // vc.params = [NSMutableDictionary dictionary];
  188. // // NSString *path = [[NSBundle mainBundle] pathForResource:@"search_ui.json" ofType:nil];
  189. // // NSData *data = [NSData dataWithContentsOfFile:path];
  190. // // vc.content_data_download = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil] mutableCopy];
  191. // [self.navigationController pushViewController:vc animated:NO];
  192. // return;
  193. // }
  194. // }
  195. // else
  196. // {
  197. //
  198. // UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"" message:nil preferredStyle:UIAlertControllerStyleAlert];
  199. // //block代码块取代了delegate
  200. //
  201. // __weak typeof(self) weakSelf = self;
  202. // for(int i=0;i<self.content_action.count;i++)
  203. // {
  204. // NSMutableDictionary* json = self.content_action[i];
  205. // NSString* title =json[@"title"];
  206. // NSString* module = json[@"module"];
  207. // NSMutableDictionary* add_params = json[@"params"];
  208. // UIAlertAction *actioni = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  209. //
  210. //
  211. // if([module isEqualToString:@"quick_look"])
  212. // {
  213. //
  214. // // +(NSString*)download_file : (NSMutableDictionary*) params url:(NSString*) url
  215. // NSMutableDictionary *params = json[@"params"];
  216. // NSString* URL =json[@"url"];
  217. // __weak typeof(self) weakSelf = self;
  218. // dispatch_async(dispatch_get_global_queue(0, 0), ^{
  219. //
  220. //
  221. // NSString *path = [RANetwork download_file:params url:URL];
  222. // weakSelf.documentPath = path;
  223. //
  224. // if (path) {
  225. // dispatch_async(dispatch_get_main_queue(), ^{
  226. // [weakSelf.navigationController pushViewController:weakSelf.quickLook animated:NO];
  227. // });
  228. // }
  229. //
  230. // });
  231. // }
  232. // else if([module isEqualToString:@"kv_detail"])
  233. // {
  234. // NSMutableDictionary *params = json[@"params"];
  235. //
  236. //
  237. // UIAlertView * waitalert = [RAUtils waiting_alert:@"Loading..." title:@"Please wait"];
  238. //
  239. //
  240. // __weak typeof(self) weakself = self;
  241. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  242. //
  243. //
  244. //
  245. // NSDictionary* json =[RANetwork kv_detail:params];
  246. //
  247. // dispatch_async(dispatch_get_main_queue(), ^{
  248. // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  249. //
  250. //
  251. // if([[json valueForKey:@"result"] intValue]==2)
  252. // {
  253. // KVViewController *vc = [[UIStoryboard storyboardWithName:@"Detail" bundle:nil] instantiateViewControllerWithIdentifier:@"KVViewController"];
  254. // vc.content = [json mutableCopy];
  255. // [weakself.navigationController pushViewController:vc animated:NO];
  256. //
  257. // // weakself.content_data_download = [weakself CopyDirty:weakself.content_data_download to:[editor_json mutableCopy]];//[editor_json mutableCopy];
  258. // //
  259. // // weakself.content_data_control = [weakself translate_json:weakself.content_data_download changed: weakself.changed_data];
  260. // //
  261. // //
  262. // // [weakself download_success];
  263. // // [weakself.editorTable reloadData];
  264. //
  265. // }
  266. // else
  267. // {
  268. // [RAUtils message_alert:json[@"msg"] title:nil controller:weakself] ;
  269. // }
  270. //
  271. //
  272. //
  273. //
  274. // });
  275. // });
  276. // }
  277. // else if([module isEqualToString:@"order_detail"])
  278. // {
  279. //
  280. // OrderDetailViewController *vc = [[UIStoryboard storyboardWithName:@"Detail" bundle:nil] instantiateViewControllerWithIdentifier:@"OrderDetailViewController"];
  281. //
  282. //
  283. // vc.url_type = URL_REMOTE;
  284. // vc.request_url=URL_ORDER_DETAIL;
  285. //
  286. //
  287. // vc.params = [NSMutableDictionary dictionary];
  288. // // NSString *path = [[NSBundle mainBundle] pathForResource:@"search_ui.json" ofType:nil];
  289. // // NSData *data = [NSData dataWithContentsOfFile:path];
  290. // // vc.content_data_download = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil] mutableCopy];
  291. // [self.navigationController pushViewController:vc animated:NO];
  292. // return;
  293. // }
  294. // // DebugLog(@"Cancel");
  295. //// if([actiontype isEqualToString:@"download"])
  296. //// {
  297. //// [weakSelf Export:add_params];
  298. //// }
  299. //// else if([actiontype isEqualToString:@"save"])
  300. //// {
  301. //// }
  302. // }];
  303. // [alertControl addAction:actioni];
  304. // }
  305. //
  306. // UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  307. // // DebugLog(@"Cancel");
  308. //
  309. // }];
  310. // [alertControl addAction:actionCancel];
  311. //
  312. //
  313. //
  314. //
  315. //
  316. // [self presentViewController:alertControl animated:NO completion:nil];
  317. //
  318. // }
  319. //
  320. // }
  321. //
  322. //
  323. //
  324. //}
  325. -(void)labelTapAction:(UIGestureRecognizer*)gestureRecognizer
  326. {
  327. NSLog(@"label touched");
  328. MDHTMLLabel* label = (MDHTMLLabel*)[gestureRecognizer view];
  329. self.label.htmlText = label.htmlText;
  330. [self.label sizeToFit];
  331. if(self.label.frame.size.width>label.frame.size.width||self.label.frame.size.height>label.frame.size.height)
  332. {
  333. FullyShowViewController *fullVC = [[UIStoryboard storyboardWithName:@"Result" bundle:nil] instantiateViewControllerWithIdentifier:@"fullyShowViewController"];
  334. fullVC.htmlText = label.htmlText;
  335. fullVC.preferredContentSize = CGSizeMake(200, 240);
  336. fullVC.modalPresentationStyle=UIModalPresentationPopover;
  337. UIPopoverPresentationController* popPc = fullVC.popoverPresentationController;
  338. popPc.permittedArrowDirections = UIPopoverArrowDirectionAny;
  339. popPc.sourceView = label;
  340. popPc.delegate = self;
  341. [self presentViewController:fullVC animated:true completion:nil];
  342. }
  343. }
  344. - (void)didReceiveMemoryWarning {
  345. [super didReceiveMemoryWarning];
  346. // Dispose of any resources that can be recreated.
  347. }
  348. - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  349. }
  350. -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
  351. {
  352. // auto resizing
  353. // self.scrollview.contentSize=self.tableview.frame.size;
  354. // auto layout
  355. // int width=[self.content_layout[@"header"][@"width"] intValue];
  356. // if(width<self.view.frame.size.width)
  357. // width=self.view.frame.size.width;
  358. // self.w_constraint.constant = width - CGRectGetWidth(self.view.bounds);
  359. // self.h_constraint.constant = 0;
  360. [self updateTableFrame];
  361. }
  362. #pragma mark - TableView DataSource
  363. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  364. return [self.content_data[@"count"] intValue];
  365. // return 0;
  366. }
  367. //- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  368. //{
  369. // return 1;
  370. //}
  371. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  372. {
  373. int height=[self.content_layout[@"header"][@"height"] intValue];
  374. if(height==0)
  375. height=44;
  376. return height;
  377. }
  378. - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  379. {
  380. NSArray* arr_col=self.content_layout[@"header"][@"col"];
  381. NSString* value =self.content_layout[@"header"][@"f_color"];//[DefaultAppearance get_noneappearance_value:@"DefaultTableHeaderView" valuename:@"title_text_color"];
  382. if(value==nil)
  383. value=@"";
  384. unsigned long fcolor = strtoul([value UTF8String],0,16);
  385. value =self.content_layout[@"header"][@"bg_color"];//[DefaultAppearance get_noneappearance_value:@"DefaultTableHeaderView" valuename:@"title_text_color"];
  386. if(value==nil)
  387. value=@"0xFFFFFF";
  388. unsigned long bgcolor = strtoul([value UTF8String],0,16);
  389. UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  390. myView.backgroundColor = UIColorFromRGB(bgcolor);
  391. myView.layer.shadowPath =[UIBezierPath bezierPathWithRect:myView.bounds].CGPath;
  392. myView.layer.shadowColor = [UIColor blackColor].CGColor;
  393. myView.layer.shadowOffset = CGSizeMake(0, 0);
  394. myView.layer.shadowOpacity = 0.5;
  395. myView.layer.shadowRadius = 2.0;
  396. int x=0;//[self.content_layout[@"header"][@"margin_l"] intValue];
  397. int y=0;//[self.content_layout[@"header"][@"margin_t"] intValue];;
  398. int height = [self.content_layout[@"header"][@"height"] intValue];
  399. for(int i=0;i<arr_col.count;i++)
  400. {
  401. int width = [arr_col[i][@"width"] intValue];
  402. UILabel *headerlabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
  403. headerlabel.textColor=UIColorFromRGB(fcolor);//UIColor.blackColor;
  404. headerlabel.backgroundColor = [UIColor clearColor];
  405. headerlabel.text=arr_col[i][@"name"];
  406. headerlabel.textAlignment= [self get_TextHAlign:arr_col[i][@"h_align"] ];
  407. headerlabel.font = [UIFont boldSystemFontOfSize:20];
  408. // [headerlabel sizeToFit];
  409. headerlabel.layer.borderWidth=0.3;
  410. [myView addSubview:headerlabel];
  411. x+=width;
  412. }
  413. myView.autoresizesSubviews=true;
  414. // [myView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin];
  415. // modellabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  416. // pricelabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  417. // timelabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  418. myView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  419. //
  420. return myView;
  421. }
  422. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  423. {
  424. int height=[self.content_layout[@"row"][@"height"] intValue];
  425. if(height==0)
  426. height=44;
  427. return height;
  428. }
  429. -(NSTextAlignment) get_TextHAlign:(NSString*)textHAlign
  430. {
  431. if([textHAlign.lowercaseString isEqualToString:@"center"])
  432. return NSTextAlignmentCenter;
  433. else if([textHAlign.lowercaseString isEqualToString:@"left"])
  434. return NSTextAlignmentLeft;
  435. else if([textHAlign.lowercaseString isEqualToString:@"right"])
  436. return NSTextAlignmentRight;
  437. return NSTextAlignmentLeft;
  438. }
  439. //-(RTTextAlignment) get_RTTextHAlign:(NSString*)textHAlign
  440. //{
  441. //
  442. //
  443. //
  444. //
  445. // if([textHAlign.lowercaseString isEqualToString:@"center"])
  446. // return RTTextAlignmentCenter;
  447. // else if([textHAlign.lowercaseString isEqualToString:@"left"])
  448. // return RTTextAlignmentLeft;
  449. // else if([textHAlign.lowercaseString isEqualToString:@"right"])
  450. // return RTTextAlignmentRight;
  451. //
  452. // return RTTextAlignmentLeft;
  453. //}
  454. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  455. {
  456. {
  457. NSString *CellIdentifier = @"tCell";
  458. UITableViewCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  459. for(UIGestureRecognizer* g in cell.gestureRecognizers)
  460. [ cell removeGestureRecognizer:g];
  461. // cell gesture
  462. // [cell removeGestureRecognizer:self.cellDoubleTap];
  463. for(UIView* v in cell.subviews)
  464. {
  465. for(UIGestureRecognizer* g in v.gestureRecognizers)
  466. [ v removeGestureRecognizer:g];
  467. [v removeFromSuperview];
  468. }
  469. // cell.detailTextLabel.text=@"detail";
  470. // cell.textLabel.text=[NSString stringWithFormat:@"%ld",indexPath.row];
  471. UITapGestureRecognizer *cellDoubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellDoubleTapAction:)];
  472. cellDoubleTap.numberOfTouchesRequired = 1; //手指数
  473. cellDoubleTap.numberOfTapsRequired = 2; //tap次数
  474. [cell addGestureRecognizer:cellDoubleTap];
  475. // 3D Touch
  476. if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
  477. DebugLog(@"3D Touch 可用!");
  478. //给cell注册3DTouch的peek(预览)和pop功能
  479. [self registerForPreviewingWithDelegate:self sourceView:cell];
  480. } else {
  481. DebugLog(@"3D Touch 无效");
  482. }
  483. NSArray* arr_col=self.content_layout[@"header"][@"col"];
  484. NSArray* item =self.content_data[[NSString stringWithFormat:@"item_%ld",indexPath.row]];
  485. NSString* value =self.content_layout[@"row"][@"f_color"];//[DefaultAppearance get_noneappearance_value:@"DefaultTableHeaderView" valuename:@"title_text_color"];
  486. if(value==nil)
  487. value=@"";
  488. unsigned long fcolor = strtoul([value UTF8String],0,16);
  489. value =self.content_layout[@"row"][@"color_0"];//[DefaultAppearance get_noneappearance_value:@"DefaultTableHeaderView" valuename:@"title_text_color"];
  490. if(value==nil)
  491. value=@"";
  492. unsigned long color0 = strtoul([value UTF8String],0,16);
  493. value =self.content_layout[@"row"][@"color_1"];//[DefaultAppearance get_noneappearance_value:@"DefaultTableHeaderView" valuename:@"title_text_color"];
  494. if(value==nil)
  495. value=@"";
  496. unsigned long color1 = strtoul([value UTF8String],0,16);
  497. int x=0;//[self.content_layout[@"header"][@"margin_l"] intValue];
  498. int y=0;//[self.content_layout[@"header"][@"margin_t"] intValue];;
  499. int height = [self.content_layout[@"row"][@"height"] intValue];
  500. for(int i=0;i<arr_col.count;i++)
  501. {
  502. int width = [arr_col[i][@"width"] intValue];;
  503. MDHTMLLabel *vallabel = [[MDHTMLLabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
  504. vallabel.textColor=UIColorFromRGB(fcolor);//UIColor.blackColor;
  505. vallabel.backgroundColor = [UIColor clearColor];
  506. NSString* text=@"";
  507. if(i>item.count-1)
  508. text=@"row data missing";
  509. else
  510. text=item[i];
  511. // RTTextLineBreakModeWordWrapping = kCTLineBreakByWordWrapping,
  512. // RTTextLineBreakModeCharWrapping = kCTLineBreakByCharWrapping,
  513. // RTTextLineBreakModeClip = kCTLineBreakByClipping,
  514. // NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:text];
  515. // vallabel.attributedText = attrStr;
  516. vallabel.htmlText = text;
  517. vallabel.textAlignment= [self get_TextHAlign:arr_col[i][@"h_align"] ];
  518. vallabel.layer.borderWidth=0.3;
  519. // UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(labelTapAction:)];
  520. UITapGestureRecognizer* labelTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTapAction:)];
  521. labelTap.numberOfTouchesRequired = 1; //手指数
  522. labelTap.numberOfTapsRequired = 1; //tap次数
  523. [vallabel addGestureRecognizer:labelTap];
  524. [labelTap requireGestureRecognizerToFail:cellDoubleTap];
  525. // [vallabel addGestureRecognizer:self.cellDoubleTap];
  526. vallabel.userInteractionEnabled = YES;
  527. // [vallabel becomeFirstResponder];
  528. // vallabel.delegate = self;
  529. // [statuslabel sizeToFit];
  530. [cell addSubview:vallabel];
  531. x+=width;
  532. }
  533. if(indexPath.row%2==0)
  534. cell.backgroundColor=UIColorFromRGB(color0);//[UIColor whiteColor];
  535. else
  536. cell.backgroundColor=UIColorFromRGB(color1);//[UIColor lightGrayColor];
  537. return cell;
  538. }
  539. }
  540. //#pragma mark - TouchLabel Delegate
  541. //- (void)touchLabel:(TouchLabel *)touchLabel touchesWtihTag:(NSInteger)tag
  542. //{
  543. //
  544. //}
  545. //-(void) empty
  546. //{
  547. //
  548. //}
  549. #pragma mark - UIPopoverPresentationControllerDelegate Delegate
  550. //实现该代理方法,返回UIModalPresentationNone值,可以在iPhone设备实现popover效果
  551. -(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{
  552. return UIModalPresentationNone;//不适配(不区分ipad或iPhone)
  553. }
  554. #pragma mark - TableView Delegate
  555. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  556. {
  557. NSLog(@"cell selected");
  558. }
  559. - (UITableViewCellEditingStyle)tableView:(UITableView *)tv editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  560. return UITableViewCellEditingStyleDelete;
  561. }
  562. #pragma mark - Public
  563. - (void)setQueryParams:(NSDictionary *)params {
  564. self.params = params;
  565. }
  566. #pragma mark - Private
  567. - (void)setupTableRefreshView {
  568. __weak typeof(self) weakSelf = self;
  569. self.tableview.jl_header = [[JLRefreshHeader alloc] init];
  570. self.tableview.jl_header.refreshDelegate = self;
  571. self.tableview.jl_header.clear = YES;
  572. self.tableview.jl_header.offsetBlock = ^(CGFloat offset) {
  573. if (offset <= 0) {
  574. weakSelf.header_top_constraint.constant = -offset;
  575. }
  576. };
  577. self.tableview.jl_header.stateChangeBlock = ^(JLRefreshState state) {
  578. NSString *title = [weakSelf.tableview.jl_header refreshTitleForState:state];
  579. weakSelf.refreshHeaderTitleLb.text = title;
  580. };
  581. self.tableview.jl_footer = [[JLRefreshFooter alloc] init];
  582. self.tableview.jl_footer.refreshDelegate = self;
  583. self.tableview.jl_footer.clear = YES;
  584. self.tableview.jl_footer.offsetBlock = ^(CGFloat offset) {
  585. if (offset <= 0) {
  586. weakSelf.footer_top_constraint.constant = offset;
  587. }
  588. };
  589. self.tableview.jl_footer.stateChangeBlock = ^(JLRefreshState state) {
  590. NSString *title = [weakSelf.tableview.jl_footer refreshTitleForState:state];
  591. weakSelf.refreshFooterTitleLb.text = title;
  592. };
  593. }
  594. - (void)setupNavigationBar {
  595. if(self.content_menu==nil)
  596. {
  597. self.navigationItem.rightBarButtonItem=nil;
  598. return;
  599. }
  600. UIBarButtonItem *menuItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ic_menu"] style:UIBarButtonItemStylePlain target:self action:@selector(MenuClick:)];
  601. self.navigationItem.rightBarButtonItem = menuItem;
  602. }
  603. - (void)MenuClick:(UIBarButtonItem *)sender {
  604. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"menu", nil) message:nil preferredStyle:UIAlertControllerStyleAlert];
  605. //block代码块取代了delegate
  606. __weak typeof(self) weakSelf = self;
  607. for(int i=0;i<self.content_menu.count;i++)
  608. {
  609. NSMutableDictionary* json = self.content_menu[i];
  610. NSString* title =json[@"title"];
  611. NSString* actiontype = json[@"action"];
  612. NSString* url = json[@"url"];
  613. // NSMutableDictionary* add_params = json[@"params"];
  614. UIAlertAction *actioni = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  615. // DebugLog(@"Cancel");
  616. if([actiontype isEqualToString:@"download"])
  617. {
  618. [weakSelf Export:url];
  619. }
  620. else if([actiontype isEqualToString:@"save"])
  621. {
  622. [weakSelf save];
  623. }
  624. }];
  625. [alertControl addAction:actioni];
  626. }
  627. UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  628. // DebugLog(@"Cancel");
  629. }];
  630. [alertControl addAction:actionCancel];
  631. [self presentViewController:alertControl animated:NO completion:nil];
  632. }
  633. - (void)save {
  634. }
  635. -(void)Export:(NSString*) url
  636. {
  637. NSAssert(true, ([NSString stringWithFormat:@"%s Export not impl",object_getClassName([self class])]));
  638. // if(self.download_query )
  639. // {
  640. // self.documentPath = self.download_query;
  641. // [self.navigationController pushViewController:self.quickLook animated:NO];
  642. // return;
  643. // }
  644. //
  645. // __weak typeof(self) weakSelf = self;
  646. // dispatch_async(dispatch_get_global_queue(0, 0), ^{
  647. //
  648. //// NSMutableDictionary *params = self.params.mutableCopy;
  649. //// if(add_params!=nil)
  650. //// [params addEntriesFromDictionary:add_params];
  651. // NSString *path = [RANetwork download_query:url];
  652. // weakSelf.download_query = path;
  653. // weakSelf.documentPath = path;
  654. //
  655. // if (path) {
  656. // dispatch_async(dispatch_get_main_queue(), ^{
  657. // [weakSelf.navigationController pushViewController:weakSelf.quickLook animated:NO];
  658. // });
  659. // }
  660. //
  661. // });
  662. }
  663. - (void)downloadItemClick:(UIBarButtonItem *)sender {
  664. NSAssert(true, ([NSString stringWithFormat:@"%s downloadItemClick not impl",object_getClassName([self class])]));
  665. //
  666. //
  667. // if(self.download_query )
  668. // {
  669. // self.documentPath = self.download_query;
  670. // [self.navigationController pushViewController:self.quickLook animated:NO];
  671. // return;
  672. // }
  673. //
  674. // __weak typeof(self) weakSelf = self;
  675. // dispatch_async(dispatch_get_global_queue(0, 0), ^{
  676. //
  677. // NSMutableDictionary *params = self.params.mutableCopy;
  678. // NSString *path = [RANetwork download_query:params];
  679. // weakSelf.download_query = path;
  680. // weakSelf.documentPath = path;
  681. //
  682. // if (path) {
  683. // dispatch_async(dispatch_get_main_queue(), ^{
  684. // [weakSelf.navigationController pushViewController:weakSelf.quickLook animated:NO];
  685. // });
  686. // }
  687. //
  688. // });
  689. //
  690. }
  691. - (NSInteger)resultItemCount {
  692. return [[self.content_data objectForKey:@"count"] integerValue];
  693. }
  694. - (void)loadContent {
  695. [self loadContentWithOption:0 Complete:nil];
  696. }
  697. /**
  698. * 0: init load
  699. * 1: refresh
  700. * 2: load more
  701. */
  702. - (void)loadContentWithOption:(int)option Complete:(void (^)(int result,int count))finish {
  703. NSAssert(true, ([NSString stringWithFormat:@"%s loadContentWithOption not impl",object_getClassName([self class])]));
  704. // if (self.params) {
  705. //
  706. // NSMutableDictionary *dic = self.params.mutableCopy;
  707. // switch (option) {
  708. // case 0:
  709. // case 1: {
  710. // self.offset = 0;
  711. // self.content_layout =nil;
  712. // self.content_action = nil;
  713. // self.content_menu = nil;
  714. // self.content_data = nil;
  715. // }
  716. // break;
  717. // case 2: {
  718. // self.offset = [self resultItemCount];
  719. // }
  720. // break;
  721. // }
  722. //
  723. //// dic[@"criteria"] = self.params;
  724. //
  725. // [dic setObject:@(self.offset) forKey:@"offset"];
  726. // [dic setObject:@(delta) forKey:@"limit"];
  727. //// dic[@"criteria"] = [NSArray arrayWithObjects:@"aaa",@"bbb", nil];
  728. //
  729. // __weak typeof(self) weakSelf = self;
  730. // dispatch_async(dispatch_get_global_queue(0, 0), ^{
  731. // NSDictionary *contentDic = [RANetwork query:dic];
  732. // NSInteger result = [[contentDic objectForKey:@"result"] integerValue];
  733. // dispatch_async(dispatch_get_main_queue(), ^{
  734. // if (weakSelf) {
  735. // __strong typeof(weakSelf) strongSelf = weakSelf;
  736. // if (result == RESULT_TRUE) {
  737. // strongSelf.content_layout = [contentDic objectForKey:@"layout"];
  738. // strongSelf.content_action = [contentDic objectForKey:@"row_action"];
  739. // strongSelf.content_menu = [[contentDic objectForKey:@"menu"] mutableCopy];
  740. // [strongSelf setupNavigationBar];
  741. //
  742. // UILabel * headerview=(UILabel *)self.tableview.tableHeaderView;
  743. // headerview.text = contentDic[@"table_title"];
  744. // [self resize_tableHeader];
  745. //
  746. // if(strongSelf.content_data==nil)
  747. // strongSelf.content_data = [NSMutableDictionary new];
  748. // NSMutableDictionary* newdata = [[contentDic objectForKey:@"data"] mutableCopy];
  749. // for(int dc = 0;dc<[newdata[@"count"] intValue];dc++)
  750. // {
  751. // NSMutableDictionary * newdata_item = newdata[[NSString stringWithFormat:@"item_%d",dc]];
  752. //
  753. // strongSelf.content_data[[NSString stringWithFormat:@"item_%d",[strongSelf.content_data[@"count"] intValue] ]] = newdata_item;
  754. // strongSelf.content_data[@"count"] = @([strongSelf.content_data[@"count"] intValue]+1);
  755. // }
  756. //
  757. //// strongSelf.content_data = [contentDic objectForKey:@"data"];
  758. // [strongSelf updateTableFrame];
  759. // [strongSelf.tableview reloadData];
  760. // } else {
  761. // [RAUtils message_alert:[contentDic objectForKey:@"err_msg"] title:@"Warning" controller:strongSelf];
  762. // }
  763. //
  764. // if (finish) {
  765. // finish((int)result,(int)[self resultItemCount]);
  766. // }
  767. //
  768. // }
  769. //
  770. // });
  771. // });
  772. // }
  773. }
  774. - (void)updateTableFrame {
  775. // auto resizing
  776. // int width=[self.content_layout[@"header"][@"width"] intValue];
  777. // if(width<self.tableview.frame.size.width)
  778. // width=self.tableview.frame.size.width;
  779. //
  780. // CGRect frame = CGRectMake(self.tableview.frame.origin.x, self.tableview.frame.origin.y, width, self.tableview.frame.size.height);
  781. // self.tableview.frame=frame;
  782. // self.scrollview.contentSize=self.tableview.frame.size;
  783. // auto layout
  784. int width=[self.content_layout[@"header"][@"width"] intValue];
  785. if(width<self.view.frame.size.width)
  786. width=self.view.frame.size.width;
  787. self.w_constraint.constant = width - CGRectGetWidth(self.view.bounds);
  788. self.h_constraint.constant = 0;
  789. // 约束修改后立即重新布局
  790. [self.view layoutIfNeeded];
  791. if (@available(iOS 11.0,*)) {
  792. UIEdgeInsets edgeInsets = [self.view safeAreaInsets];
  793. CGFloat left = edgeInsets.left;
  794. CGFloat right = edgeInsets.right;
  795. self.lead_constraint.constant = left;
  796. self.trail_constraint.constant = right;
  797. /*
  798. * lead 和 trail改变之前
  799. * scrollview.size.width = self.view.size.width;
  800. * 改变后
  801. * scrollview.size.width = self.view.size.width - left - right;
  802. * 由于w_contraint本质上是根据scrolliew.size.width决定,实际上使用self.view.size.width计算得到
  803. * 所以w_contraint需要 +left +right补偿偏差。
  804. */
  805. self.w_constraint.constant += left + right;
  806. // 约束修改后立即重新布局
  807. [self.view layoutIfNeeded];
  808. }
  809. }
  810. #pragma mark - RefreshDelegate
  811. - (void)jl_pullRefresh:(JLRefreshBasis *)refresh state:(JLRefreshState)state percentage:(float)percentage {
  812. // NSString *title = [refresh refreshTitleForState:state];
  813. // CGFloat offset = refresh.jl_height * percentage;
  814. if ([refresh isEqual:self.tableview.jl_header]) {
  815. // self.refreshHeaderTitleLb.text = title;
  816. // self.header_top_constraint.constant = offset;
  817. }
  818. if (refresh == self.tableview.jl_footer) {
  819. // self.refreshFooterTitleLb.text = title;
  820. // self.footer_top_constraint.constant = -offset;
  821. }
  822. }
  823. - (void)jl_endRefresh:(JLRefreshBasis *)refresh {
  824. // state == idle
  825. // refresh UI
  826. [self.tableview reloadData];
  827. if (refresh == self.tableview.jl_footer && refresh.state == JLRefreshStateNoMore) {
  828. }
  829. }
  830. - (void)jl_beginRefresh:(JLRefreshBasis *)refresh {
  831. // state == refreshing
  832. // load data
  833. // finish loading data
  834. int option = 0;
  835. if ([refresh isEqual:self.tableview.jl_header]) {
  836. option = 1;
  837. }
  838. if (refresh == self.tableview.jl_footer) {
  839. if(refresh.state==JLRefreshStateNoMore) //数据取完不再加载
  840. return;
  841. option = 2;
  842. }
  843. __weak typeof(self) weakSelf = self;
  844. [self loadContentWithOption:option Complete:^(int result, int count) {
  845. if ([refresh isEqual:weakSelf.tableview.jl_header]) {
  846. [weakSelf.tableview.jl_header performSelector:@selector(endRefresh)];
  847. }
  848. if (refresh == weakSelf.tableview.jl_footer) {
  849. [weakSelf.tableview.jl_footer performSelector:@selector(endRefresh)];
  850. if (result == 2 && count < delta) {
  851. [weakSelf.tableview.jl_footer performSelector:@selector(noMoreData)];
  852. }
  853. }
  854. }];
  855. }
  856. #pragma mark - Quick Look
  857. - (QLPreviewController *)quickLook {
  858. if (!_quickLook)
  859. {
  860. _quickLook = [[QLPreviewController alloc] init];
  861. _quickLook.delegate = self;
  862. _quickLook.dataSource = self;
  863. }
  864. else
  865. {
  866. [_quickLook reloadData];
  867. }
  868. return _quickLook;
  869. }
  870. #pragma mark - QuickLook Data Source
  871. - (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
  872. return 1;
  873. }
  874. - (id <QLPreviewItem>) previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
  875. return [NSURL fileURLWithPath:self.documentPath];
  876. }
  877. #pragma mark - QuickLook Delegate
  878. - (void) previewControllerDidDismiss:(QLPreviewController *)controller {
  879. }
  880. /**
  881. * 文件内部链接点击不进行外部跳转
  882. */
  883. - (BOOL) previewController:(QLPreviewController *)controller shouldOpenURL:(NSURL *)url forPreviewItem:(id<QLPreviewItem>)item {
  884. return NO;
  885. }
  886. #pragma mark - customize editor
  887. - (UIView*)get_tableHeader
  888. {
  889. UILabel * headerview = [UILabel new];
  890. headerview.frame = CGRectMake(0, 0, self.tableview.frame.size.width, 44);
  891. headerview.numberOfLines = 0;
  892. headerview.textAlignment = NSTextAlignmentCenter;
  893. headerview.text = @"";
  894. return headerview;
  895. }
  896. -(void) resize_tableHeader
  897. {
  898. //利用systemLayoutSizeFittingSize:计算出真实高度
  899. CGFloat height = [self.tableview.tableHeaderView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
  900. CGRect headerFrame = self.tableview.tableHeaderView.frame;
  901. headerFrame.size.height = height+20;//20是留的上下边距
  902. //修改tableHeaderView的frame
  903. self.tableview.tableHeaderView.frame = headerFrame;
  904. }
  905. #pragma mark - 3D Touch Delegate (PreviewingDelegate)
  906. // peek (预览)
  907. - (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {
  908. // [previewingContext sourceView]即为所按压的视图
  909. UITableViewCell *view = (UITableViewCell *)[previewingContext sourceView];
  910. //
  911. // // 设置不被虚化的范围
  912. // CGRect rect = view.bounds;
  913. // [previewingContext setSourceRect:rect];
  914. //
  915. // NSIndexPath *indexPath = [self.tableview indexPathForCell:view];
  916. // // peek (预览界面)
  917. // UIViewController *tvc = [self viewControllerFor3DTouch:indexPath];
  918. //
  919. // tvc.preferredContentSize = CGSizeMake(0, 480); // 预览图的大小
  920. //
  921. // return tvc;
  922. NSIndexPath *indexPath = [self.tableview indexPathForCell:view];
  923. [self presses3DTouchForIndexPath:indexPath];
  924. return nil;
  925. }
  926. // pop (在预览基础上更用力按压)
  927. - (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
  928. // [self showViewController:viewControllerToCommit sender:self];
  929. }
  930. @end