2017.03.05

1.推荐关注页面,推荐类别列表

2017.03.05_第1张图片
关注类别.gif

左边的tableView用Xib创建,给定宽度约束.列表中的数据由网络请求所得.

/**
 请求左边的类别数据
 */
- (void)loadCategories {
    
    // 请求参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"a"] = @"category";
    params[@"c"] = @"subscribe";
    
    // 发送请求
    __weak typeof(self) weakSelf = self;
    [self.manager GET:@"http://api.budejie.com/api/api_open.php" parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        // 字典数组 -> 模型数组
        weakSelf.categories = [LXXRecommendCatory mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
   
        [weakSelf.categoryTableView reloadData];
        [SVProgressHUD dismiss];
        
        //默认选中第零行
        [self.categoryTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
        
    }];
}

实现相应的数据源方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.categories.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //取出cell
    LXXRecommendCategoryCell *cell = [self.categoryTableView dequeueReusableCellWithIdentifier:LXXCategoryId];
    
    //设置模型
    cell.category = self.categories[indexPath.row];
    
    return cell;
}

tableView的cell使用xib创建.
cell中的白色分隔线是将tableView自带的分隔线去掉,然后在xib中添加一个白色的view,再在cell的layoutSubView方法中重新布局子控件,使新添加的view显示出来.

- (void)layoutSubviews {
    [super layoutSubviews];
    
    //重新调整内部textLabel的frame,从而使添加的分隔线显示出来
    self.textLabel.y = 2;
    self.textLabel.height -= 2 * self.textLabel.y;
}

cell左边的红色指示器也是在xib中创建,然后再cell的setSelected方法中控制指示器的显示和cell中的textLabel文字颜色.

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    //指示器的显示
    self.selectedIndicatorView.hidden = !selected;
    //textLabel文字的颜色
    self.textLabel.textColor = selected ? LXXColor(219, 21, 26) : LXXColor(78, 78, 78);
}

2.关注用户列表

2017.03.05_第2张图片
关注用户.gif

用户列表的tableView同样在xib中设置,在相关的数据源方法中分别根据不同的列表设置相应的数据.

/**
 设置tableView相关
 */
- (void)setUpTableView {
    
    //注册cell
    [self.categoryTableView registerNib:[UINib nibWithNibName:NSStringFromClass([LXXRecommendCategoryCell class]) bundle:nil]forCellReuseIdentifier:LXXCategoryId];
    [self.userTableView registerNib:[UINib nibWithNibName:NSStringFromClass([LXXRecommendUserCell class]) bundle:nil]forCellReuseIdentifier:LXXUserId];
    
    self.userTableView.rowHeight = 70;
    
    //取消系统自带的调整,手动设置两个tableView的内边距
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.categoryTableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
    self.userTableView.contentInset = self.categoryTableView.contentInset;
}

- (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) {
        
        //取出cell
        LXXRecommendCategoryCell *cell = [self.categoryTableView dequeueReusableCellWithIdentifier:LXXCategoryId];
        
        //设置模型
        cell.category = self.categories[indexPath.row];
        
        return cell;
        
    }else {
        //取出cell
        LXXRecommendUserCell *cell = [self.userTableView dequeueReusableCellWithIdentifier:LXXUserId];
        
        //设置模型
        cell.user = self.users[indexPath.row];
        
        return cell;
    
    }
}

3.存在问题
1)每次点击左边cell都会发送请求,即重复请求问题
2)目前只能显示第一页
3)网速慢的情况下的一系列问题

你可能感兴趣的:(2017.03.05)