ResultViewController.m 42 KB

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