标签:
一.整体布局
1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 4 self.title = @"推荐关注"; 5 self.automaticallyAdjustsScrollViewInsets = NO; 6 _categoryTableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0); 7 _userTableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0); 8 // 分类tableView注册cell 9 [_categoryTableView registerNib:[UINib nibWithNibName:@"XMGCategoryCell" bundle:nil] forCellReuseIdentifier:categoryID]; 10 // 用户tableView注册cell 11 [_userTableView registerNib:[UINib nibWithNibName:@"XMGSubTagCell" bundle:nil] forCellReuseIdentifier:userID]; 12 // 请求分类数据 13 [self loadCategoryData]; 14 // 添加上下拉刷新 15 [self setupRefreshView]; 16 } 17 - (void)setupRefreshView 18 { 19 // 下拉刷新 20 // 当松手,并且下拉刷新完全显示的时候,就会触发下拉刷新 21 MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewUserData)]; 22 header.automaticallyChangeAlpha = YES; 23 self.userTableView.mj_header = header; 24 25 // 上拉刷新 26 MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreUserData)]; 27 footer.automaticallyHidden = YES; 28 self.userTableView.mj_footer = footer; 29 } 30 31 - (void)loadCategoryData 32 { 33 AFHTTPSessionManager *mgr = [AFHTTPSessionManager xmg_manager]; 34 35 NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; 36 parameters[@"a"] = @"category"; 37 parameters[@"c"] = @"subscribe"; 38 39 [mgr GET:XMGBaseUrl parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) { 40 NSArray *dictArr = responseObject[@"list"]; 41 42 _categorys = [XMGCategoryItem mj_objectArrayWithKeyValuesArray:dictArr]; 43 44 [self.categoryTableView reloadData]; 45 46 // 默认选中第0个cell 47 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 48 [self.categoryTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; 49 50 [self tableView:self.categoryTableView didSelectRowAtIndexPath:indexPath]; 51 52 } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 53 }]; 54 } 55 56 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 57 { 58 if (tableView == _categoryTableView) { // 显示分类TableView 59 return _categorys.count; 60 } 61 return _selectCategoryItem.users.count; 62 } 63 64 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 65 { 66 if (tableView == _categoryTableView) { // 显示分类TableView 67 XMGCategoryCell *cell = [tableView dequeueReusableCellWithIdentifier:categoryID]; 68 cell.item = _categorys[indexPath.row]; 69 return cell; 70 } 71 XMGSubTagCell *cell = [tableView dequeueReusableCellWithIdentifier:userID]; 72 cell.user = _selectCategoryItem.users[indexPath.row]; 73 return cell; 74 } 75 76 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 77 { 78 if (tableView == _categoryTableView) { 79 return 44; 80 } 81 return 60 + 1; 82 } 83 // 点击cell就会调用 84 // 必须用户手动点击cell才会触发 85 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 86 { 87 if (tableView == _categoryTableView) { 88 // 记录选中分类模型 89 _selectCategoryItem = _categorys[indexPath.row]; 90 // 点击分类cell 91 // 判断之前有没有数据 92 if (_selectCategoryItem.users.count) { 93 [self.userTableView reloadData]; 94 self.userTableView.mj_footer.hidden = _selectCategoryItem.page > _selectCategoryItem.total_page; 95 return; 96 } 97 // 请求右边用户数据 98 [self loadNewUserData]; 99 } 100 } 101 102 // 没有更多数据的时候,隐藏上拉刷新控件 103 // 判断当前分类用户组 有没有更多用户组 104 // 加载更多用户数据 105 - (void)loadMoreUserData 106 { 107 [self.mgr.tasks makeObjectsPerformSelector:@selector(cancel)]; 108 109 NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; 110 parameters[@"a"] = @"list"; 111 parameters[@"c"] = @"subscribe"; 112 parameters[@"category_id"] = _selectCategoryItem.id; 113 parameters[@"page"] = @(_selectCategoryItem.page); 114 115 [self.mgr GET:XMGBaseUrl parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) { 116 117 [self.userTableView.mj_footer endRefreshing]; 118 119 _selectCategoryItem.page++; 120 NSArray *dictArr = responseObject[@"list"]; 121 122 NSArray *users = [XMGUserItem mj_objectArrayWithKeyValuesArray:dictArr]; 123 124 // 取出数组中所有元素,添加到新数组 125 // [_selectCategoryItem.users addObject:users]; 126 [_selectCategoryItem.users addObjectsFromArray:users]; 127 128 [self.userTableView reloadData]; 129 130 // 控制上拉控件是否显示,一定要在reloadData之后 131 self.userTableView.mj_footer.hidden = _selectCategoryItem.page > _selectCategoryItem.total_page; 132 } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 133 }]; 134 } 135 136 // 加载更新用户数据 137 - (void)loadNewUserData 138 { 139 _selectCategoryItem.page = 1; 140 [self.mgr.tasks makeObjectsPerformSelector:@selector(cancel)]; 141 142 NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; 143 parameters[@"a"] = @"list"; 144 parameters[@"c"] = @"subscribe"; 145 parameters[@"category_id"] = _selectCategoryItem.id; 146 147 [self.mgr GET:XMGBaseUrl parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) { 148 149 _selectCategoryItem.page++; 150 151 // 记录当前分类总页码数 152 _selectCategoryItem.total_page = [responseObject[@"total_page"] integerValue]; 153 154 // 结束刷新 155 [self.userTableView.mj_header endRefreshing]; 156 157 NSArray *dictArr = responseObject[@"list"]; 158 159 _selectCategoryItem.users = [XMGUserItem mj_objectArrayWithKeyValuesArray:dictArr]; 160 161 [self.userTableView reloadData]; 162 163 self.userTableView.mj_footer.hidden = _selectCategoryItem.page > _selectCategoryItem.total_page; 164 165 } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 166 }]; 167 }
标签:
原文地址:http://www.cnblogs.com/xiaotian666/p/5764520.html