这次实现右边数据的显示
我们对上次的代码进行添加及改造
一.Controller
在xib中
把右边的的UITableView也添加上,并将关联到代码中
/** 服务器传来的数据数组 */
@property (nonatomic,strong) NSArray *categories;
/** 服务器传来的数据数组 */
@property (nonatomic,strong) NSArray *users;
@property (weak, nonatomic) IBOutlet UITableView *categoryTableView;
@property (weak, nonatomic) IBOutlet UITableView *userTableView;
在DDZRecommendViewController中
- (void)viewDidLoad {
[super viewDidLoad];
//初始化数据
[self setupTableView];
//左侧项目数据请求
[self dataLeftRequest];
}
//初始化数据
- (void)setupTableView {
self.view.backgroundColor = DDZGlobalBg;
self.title = @"推荐关注";
//隐藏滚动条
self.categoryTableView.showsVerticalScrollIndicator = NO;
//注册左侧cell
[self.categoryTableView registerNib:[UINib nibWithNibName:NSStringFromClass([DDZRecommendCategoryCell class]) bundle:nil] forCellReuseIdentifier:@"category"];
//注册右侧cell
[self.userTableView registerNib:[UINib nibWithNibName:NSStringFromClass([DDZRecommendUserCell class]) bundle:nil] forCellReuseIdentifier:@"user"];
//设置insert
self.automaticallyAdjustsScrollViewInsets = NO;
self.categoryTableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
self.userTableView.contentInset = self.categoryTableView.contentInset;
//设置右侧cell高度
self.userTableView.rowHeight = 70;
//设置背景蒙版
[SVProgressHUD show];
[SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
}
(1)设置insert的目的
是避免导航栏挡住tableview显示的内容
第一句是禁止自动设置insert
self.automaticallyAdjustsScrollViewInsets = NO;
第二句是设置上边距为64(导航栏加状态栏是64点)
self.categoryTableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
(2)设置右侧cell的高度
是因为cell的默认高度是44,不设置无法包含大一点的图片
//左侧数据请求
- (void)dataLeftRequest {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[@"a"] = @"category";
dict[@"c"] = @"subscribe";
[manager GET:@"http://api.budejie.com/api/api_open.php" parameters:dict progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[SVProgressHUD dismiss];
//回传数据成功(需要字典转数据模型框架)
self.categories = [DDZRecommendCategory mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
//刷新页面
[self.categoryTableView reloadData];
//默认选中首行
[self.categoryTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[SVProgressHUD showErrorWithStatus:@"加载推荐信息失败!"];
}];
}
设置委托协议
#pragma mark - <UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (tableView == self.categoryTableView) {
return self.categories.count;
}else {
return self.users.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == self.categoryTableView)