您的位置:首页 > 其它

推荐关注模块的实现2

2016-04-26 09:52 411 查看
这次实现右边数据的显示

我们对上次的代码进行添加及改造

一.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) {
DDZRecommendCategoryCell *cell = [tableView dequeueReusableCellWithIdentifier:@"category"];

cell.category = self.categories[indexPath.row];

return cell;
}else {
DDZRecommendUserCell *cell = [tableView dequeueReusableCellWithIdentifier:@"user"];

cell.user = self.users[indexPath.row];

return cell;
}

}


因为在一个Controller中有两个UITableView,所以需要复用

在协议中判断不同的tableview来设置各自的属性

#pragma mark - <,UITableViewDelegate>
//被选中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

DDZRecommendCategory *c = self.categories[indexPath.row];
//请求右侧数据
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[@"a"] = @"list";
dict[@"c"] = @"subscribe";
dict[@"category_id"] = @(c.id);
[manager GET:@"http://api.budejie.com/api/api_open.php" parameters:dict progress:^(NSProgress * _Nonnull downloadProgress) {

} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

//回传数据成功(需要字典转数据模型框架)
self.users = [DDZRecommendUser mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];

//刷新页面
[self.userTableView reloadData];

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

NSLog(@"%@",error);
}];
}


每点击一次左边的项目,都会发一次请求,用来更新数据

二.Model

多添加一个模型DDZRecommendUser

/** 头像地址header */
@property (nonatomic,copy) NSString *header;
/** 关注数fans_count */
@property (nonatomic,copy) NSString *fans_count;
/** 用户名screen_name */
@property (nonatomic,copy) NSString *screen_name;


三.View

多添加一个DDZRecommendUserCell用于自定义右边的数据的cell

/** 模型数据 */
@property (nonatomic,strong) DDZRecommendUser *user;


- (void)awakeFromNib {
[super awakeFromNib];

}

- (void)setUser:(DDZRecommendUser *)user {

_user = user;

[self.userImageView sd_setImageWithURL:[NSURL URLWithString:user.header] placeholderImage:[UIImage imageNamed:@"defaultUserIcon"]];
self.nameLabel.text = user.screen_name;
self.fansLabel.text = [NSString stringWithFormat:@"%@人关注",user.fans_count];
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: