MJExampleViewController.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // 代码地址: https://github.com/CoderMJLee/MJRefresh
  2. // MJExampleViewController.m
  3. // MJRefreshExample
  4. //
  5. // Created by MJ Lee on 15/3/5.
  6. // Copyright (c) 2015年 小码哥. All rights reserved.
  7. //
  8. #import "MJExampleViewController.h"
  9. #import "MJTableViewController.h"
  10. #import "MJWebViewViewController.h"
  11. #import "MJCollectionViewController.h"
  12. #import "MJExample.h"
  13. #import "UIViewController+Example.h"
  14. #import "MJRefresh.h"
  15. #import "MJHorizontalCollectionViewController.h"
  16. #import "MJRefreshExample-Swift.h"
  17. static NSString *const MJExample00 = @"UITableView + 下拉刷新";
  18. static NSString *const MJExample10 = @"UITableView + 上拉刷新";
  19. static NSString *const MJExample20 = @"UICollectionView";
  20. static NSString *const MJExample30 = @"UIWebView";
  21. static NSString *const MJExample40 = @"WKWebView";
  22. @interface MJExampleViewController()
  23. @property (strong, nonatomic) NSArray *examples;
  24. @end
  25. @implementation MJExampleViewController
  26. - (NSArray *)examples
  27. {
  28. if (!_examples) {
  29. MJExample *exam0 = [[MJExample alloc] init];
  30. exam0.header = MJExample00;
  31. exam0.vcClass = [MJTableViewController class];
  32. exam0.titles = @[@"默认", @"动画图片", @"隐藏时间", @"隐藏状态和时间", @"自定义文字", @"自定义刷新控件"];
  33. exam0.methods = @[@"example01", @"example02", @"example03", @"example04", @"example05", @"example06"];
  34. MJExample *exam1 = [[MJExample alloc] init];
  35. exam1.header = MJExample10;
  36. exam1.vcClass = [MJTableViewController class];
  37. exam1.titles = @[@"默认", @"动画图片", @"隐藏刷新状态的文字", @"全部加载完毕", @"禁止自动加载", @"自定义文字", @"加载后隐藏", @"自动回弹的上拉01", @"自动回弹的上拉02", @"自定义刷新控件(自动刷新)", @"自定义刷新控件(自动回弹)"];
  38. exam1.methods = @[@"example11", @"example12", @"example13", @"example14", @"example15", @"example16", @"example17", @"example18", @"example19", @"example20", @"example21"];
  39. MJExample *exam2 = [[MJExample alloc] init];
  40. exam2.header = MJExample20;
  41. exam2.vcClass = [MJCollectionViewController class];
  42. exam2.titles = @[@"上下拉刷新"];
  43. exam2.methods = @[@"example21"];
  44. MJExample *exam3 = [[MJExample alloc] init];
  45. exam3.header = MJExample30;
  46. exam3.vcClass = [MJWebViewViewController class];
  47. exam3.titles = @[@"下拉刷新"];
  48. exam3.methods = @[@"example31"];
  49. MJExample *exam4 = [[MJExample alloc] init];
  50. exam4.header = MJExample40;
  51. exam4.vcClass = [MJWKWebViewController class];
  52. exam4.titles = @[@"下拉刷新"];
  53. exam4.methods = @[@"example41"];
  54. MJExample *exam5 = [[MJExample alloc] init];
  55. exam5.header = MJExample20;
  56. exam5.vcClass = [MJHorizontalCollectionViewController class];
  57. exam5.titles = @[@"左拉刷新"];
  58. exam5.methods = @[@"example42"];
  59. self.examples = @[exam0, exam1, exam2, exam3, exam4, exam5];
  60. }
  61. return _examples;
  62. }
  63. - (void)viewDidLoad
  64. {
  65. [super viewDidLoad];
  66. __unsafe_unretained UITableView *tableView = self.tableView;
  67. // 下拉刷新
  68. tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  69. // 模拟延迟加载数据,因此2秒后才调用(真实开发中,可以移除这段gcd代码)
  70. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  71. // 结束刷新
  72. [tableView.mj_header endRefreshing];
  73. });
  74. }];
  75. // 设置自动切换透明度(在导航栏下面自动隐藏)
  76. tableView.mj_header.automaticallyChangeAlpha = YES;
  77. // 上拉刷新
  78. tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
  79. // 模拟延迟加载数据,因此2秒后才调用(真实开发中,可以移除这段gcd代码)
  80. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  81. // 结束刷新
  82. [tableView.mj_footer endRefreshing];
  83. });
  84. }];
  85. }
  86. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  87. {
  88. return self.examples.count;
  89. }
  90. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  91. {
  92. MJExample *exam = self.examples[section];
  93. return exam.titles.count;
  94. }
  95. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  96. {
  97. static NSString *ID = @"example";
  98. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
  99. MJExample *exam = self.examples[indexPath.section];
  100. cell.textLabel.text = exam.titles[indexPath.row];
  101. cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ - %@", exam.vcClass, exam.methods[indexPath.row]];
  102. return cell;
  103. }
  104. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  105. {
  106. MJExample *exam = self.examples[section];
  107. return exam.header;
  108. }
  109. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  110. {
  111. MJExample *exam = self.examples[indexPath.section];
  112. UIViewController *vc = [[exam.vcClass alloc] init];
  113. vc.title = exam.titles[indexPath.row];
  114. [vc setValue:exam.methods[indexPath.row] forKeyPath:@"method"];
  115. [self.navigationController pushViewController:vc animated:YES];
  116. }
  117. @end