美文网首页
iOS复杂的列表布局处理

iOS复杂的列表布局处理

作者: HCL黄 | 来源:发表于2019-09-29 16:16 被阅读0次

如图 269F0E2B-6B63-42B1-970D-354414CB54B1.png

我们先把他拆分几个部分

  • 顶部对应一个cell 91A2FD14-8056-4E11-830E-EE1FA90EF555.png
  • sectionView BF76E3DF-E6EE-4EB4-B247-FAD0D4DB8965.png
  • 岗位列表对应的cell DB2F2C1C-0FF5-4F0A-9FA1-BCA6E3BBE39D.png
  • 无岗位列表时对应的cell 65601F1E-CDFB-4017-AE5E-962E2C9DA8C8.png
  • 应聘对话对应的cell A0936199-B0C8-44EA-BE47-4ECE0BEA018A.png
  • 无应聘对话时对应的cell 9A909110-E555-4B5E-9D6C-AF3789AEE39B.png

对应的我们定义一下枚举

typedef enum : NSUInteger {
    LATypeTop, // 默认顶部
    LATypeJobs, // 招聘岗位
    LATypeNoJobs, // 没有发布岗位
    LATypeChat, // 应聘对话
    LATypeNoChat, // 没有应聘对话
} LAType;

同样的我们也定义对应的数据数组

注意初始化这些数组

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *groupM;
@property (nonatomic, strong) NSMutableArray *topM;
@property (nonatomic, strong) NSMutableArray *jobsM;
@property (nonatomic, strong) NSMutableArray *noJobsM;
@property (nonatomic, strong) NSMutableArray *chatM;
@property (nonatomic, strong) NSMutableArray *noChat;

创建我们的列表,注册对应的cell

- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight = 100;
        _tableView.backgroundColor = LAColorBackground;
    }
    return _tableView;
}
[self.view addSubview:self.tableView];
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
      make.top.equalTo(self.view);
      make.left.right.bottom.equalTo(self.view);
 }];
[self.tableView registerNib:[LATopCell nib] forCellReuseIdentifier:[LATopCell reuseIdentifier]];
[self.tableView registerNib:[LAEmptyCell nib] forCellReuseIdentifier:[LAEmptyCell reuseIdentifier]];
[self.tableView registerNib:[LAJobsCell nib] forCellReuseIdentifier:[LAJobsCell reuseIdentifier]];
[self.tableView registerNib:[LAChatCell nib] forCellReuseIdentifier:[LAChatCell reuseIdentifier]];

完成tableView代理数据源的配置

#pragma mark - UITableViewDelegate/UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.groupM.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSDictionary *dict = self.groupM[section];
    if ([[dict allKeys] containsObject:@(LATypeTop)]) {
        return self.topM.count > 0 ? 1 : 0;
    } else if ([[dict allKeys] containsObject:@(LATypeNoJobs)]) {
        return self.noJobsM.count > 0 ? 1 : 0;
    } else if ([[dict allKeys] containsObject:@(LATypeNoChat)]) {
        return self.noChat.count > 0 ? 1 : 0;
    } else if ([[dict allKeys] containsObject:@(LATypeJobs)]) {
        return self.jobsM.count;
    } else if ([[dict allKeys] containsObject:@(LATypeChat)]) {
        return self.chatM.count;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 250;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = self.groupM[indexPath.section];
    if ([[dict allKeys] containsObject:@(LATypeTop)]) {
        LATopCell *cell = [tableView dequeueReusableCellWithIdentifier:[LATopCell reuseIdentifier]];
        return cell;
    } else if ([[dict allKeys] containsObject:@(LATypeNoJobs)]) {
        LAEmptyCell *cell = [tableView dequeueReusableCellWithIdentifier:[LAEmptyCell reuseIdentifier]];
        cell.emptyImgStr = @"recruit_bg_noJob";
        return cell;
    } else if ([[dict allKeys] containsObject:@(LATypeNoChat)]) {
        LAEmptyCell *cell = [tableView dequeueReusableCellWithIdentifier:[LAEmptyCell reuseIdentifier]];
        cell.emptyImgStr = @"recruit_bg_noChat";
        return cell;
    } else if ([[dict allKeys] containsObject:@(LATypeJobs)]) {
        LAJobsCell *cell = [tableView dequeueReusableCellWithIdentifier:[LAJobsCell reuseIdentifier]];
        return cell;
    } else if ([[dict allKeys] containsObject:@(LATypeChat)]) {
        LAChatCell *cell = [tableView dequeueReusableCellWithIdentifier:[LAChatCell reuseIdentifier]];
        return cell;
    }
    
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    LAMRBGSectionView *sectionView = [LAMRBGSectionView loadFromNib];
    NSDictionary *dict = self.groupM[section];
    @weakify(self);
    if ([[dict allKeys] containsObject:@(LATypeJobs)]||
        [[dict allKeys] containsObject:@(LATypeNoJobs)]) {
        sectionView.titleString = @"招聘岗位";
        sectionView.showMoreBtn = self.jobsM.count != 0;
        sectionView.rightBlock = ^{
            @strongify(self);
        };
        return sectionView;
    } else if ([[dict allKeys] containsObject:@(LATypeChat)]||
        [[dict allKeys] containsObject:@(LATypeNoChat)]) {
        sectionView.titleString = @"应聘对话";
        sectionView.showMoreBtn = self.chatM.count != 0;
        sectionView.rightBlock = ^{
            @strongify(self);
        };
        return sectionView;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    NSDictionary *dict = self.groupM[section];
    if ([[dict allKeys] containsObject:@(LATypeJobs)]||
        [[dict allKeys] containsObject:@(LATypeNoJobs)]||
        [[dict allKeys] containsObject:@(LATypeChat)]||
        [[dict allKeys] containsObject:@(LATypeNoChat)]) {
        return 58;
    }
    return 0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    @weakify(self);
    NSDictionary *dict = self.groupM[indexPath.section];
    if ([[dict allKeys] containsObject:@(LATypeJobs)]) {
        LAXXXXXXVc *vc = [[LAXXXXXXVc alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([[dict allKeys] containsObject:@(LATypeChat)]) {
        LAModel *m = self.chatM[indexPath.row];
        LAXXXXXXVc *chatVc = [[LAXXXXXXVc alloc] init];
        [self.navigationController pushViewController:chatVc animated:YES];
    }
}

接下来就是填充数据了

  • 先抽取几个方法
/** 删除group数组对应rBgType的数据 */
- (void)deleteRelatedDataWithArrayM:(NSMutableArray *)arrayM rBgType:(LABGType)rBgType {
    [arrayM removeAllObjects];
    for (NSDictionary *dict in self.groupM) {
        if ([[dict allKeys] containsObject:@(rBgType)]) {
            [self.groupM removeObject:dict];
            break;
        }
    }
}
/** 添加对应rBgType的数据:如顶部单独的cell */
- (void)addRelatedDataWithArrayM:(NSMutableArray *)arrayM rBgType:(LABGType)rBgType {
    [self deleteRelatedDataWithArrayM:arrayM rBgType:rBgType];
    [arrayM addObject:@"1"];
    NSDictionary *dict = @{@(rBgType) : arrayM};
    [self.groupM addObject:dict];
}
/** 添加对应rBgType的数据:如应聘对话列表的cell */
- (void)addRelatedDataWithArrayM:(NSMutableArray *)arrayM rBgType:(LABGType)rBgType models:(NSArray *)models {
    [self deleteRelatedDataWithArrayM:arrayM rBgType:rBgType];
    [arrayM addObjectsFromArray:models];
    NSDictionary *dict = @{@(rBgType) : arrayM};
    [self.groupM addObject:dict];
}
  • 开始填充数据
// 默认添加顶部
[self addRelatedDataWithArrayM:self.topM rBgType:LATypeTop];
if (self.b236Model.list.count == 0) {
   // 添加招聘岗位空视图
   [self addRelatedDataWithArrayM:self.noJobsM rBgType:LATypeNoJobs];
} else {
   // 删除招聘岗位空视图
   [self deleteRelatedDataWithArrayM:self.noJobsM rBgType:LATypeNoJobs];
   // 添加招聘岗位
   [self addRelatedDataWithArrayM:self.jobsM rBgType:LATypeJobs models:self.b236Model.list];
 }
if (b233Model.list.count == 0) {
   // 添加应聘对话空视图
   [self addRelatedDataWithArrayM:self.noChat rBgType:LATypeNoChat];
} else {
  // 删除应聘对话空视图
  [self deleteRelatedDataWithArrayM:self.noChat rBgType:LATypeNoChat];
  // 添加应聘对话
 [self addRelatedDataWithArrayM:self.chatM rBgType:LATypeChat models:b233Model.list];
}
[self.tableView reloadData];

OK,到此为止了

相关文章

网友评论

      本文标题:iOS复杂的列表布局处理

      本文链接:https://www.haomeiwen.com/subject/lcpfpctx.html