ResultViewController.m 45 KB

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