动态计算TableView和CollectionView的高度

整体思路:

根据网络请求数据量的大小,在请求成功的回调中计算 TableView 的高度,将计算救过通过Block或代理传递给TableView所在控制器,更新tableView 的约束即可;

.h 文件(tableView 所造控制器) 定义Block

@interface MineTableViewController : UITableViewController

/**
 *  数据加载完成之后,动态计算TableView 的高度
 */
@property(nonatomic,copy)  void(^contentSizeForScrollView)(CGSize size);
@end

.m 文件(tableView 所造控制器) 实现

#pragma mark - 计算TableView 的高度
-(CGSize)calculateTableViewSizeWithCellsCount{
   __block CGFloat height;
    
    [self.groupArray enumerateObjectsUsingBlock:^(NSArray * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        height += obj.count * 44;                     // 所有 Cell 高度
    }];
    
    height += self.groupArray.count * k_HEADER_HEIGHT;  // 计算组头总间距
    return CGSizeMake(kScreenWidth, height);
}
#pragma mark - \网络请求
-(void)requestAddedItemsData{
[PublicMethod XMHDPostWithURL:@"mine/module/list" parameters:dict success:^(id objc) {
       
        NSArray *arr = [objc objectForKey:@"object"];
        for (NSDictionary *dic in arr) {
            DDMineModel *model = [[DDMineModel alloc]init];
            model.MenuName = [dic objectForKey:@"menuName"];
            model.MenuDesc = [dic objectForKey:@"menuDesc"];
            [list addObject:model];
        }

        // 加锁, 避免self.groupArray 数据竞争
        @synchronized (self) {
            [self.groupArray insertObject:list.copy atIndex:0];
        }
         self.hadLoadNewItems = YES;
         if (self.contentSizeForScrollView) {  // 根据网络请求数据量 计算高度
                  self.contentSizeForScrollView([self calculateTableViewSizeWithCellsCount]);
          }
        
      } failure:^(NSError *error) {
         self.failedLoadData = YES;

    }];

TableView 要加载TableView的目标控制器,代码片段

 // 数据请求完成之后动态计算ScrollView 的ContentSize 和 TableView的高度;
    [mineTableViewController setContentSizeForScrollView:^(CGSize size) {
        
        CGFloat width = size.width;
        CGFloat height = size.height + 2*k_ROW_HEIGHT + 3*k_LINE_MARGIN + k_HEADER_HEIGHT;
        _acountScrollView.contentSize = CGSizeMake(width, height + 40);  // 底部预留40间距

        //       remakeConstraints  特别注意这里使用remakeConstraints或undateConstraints
        [_mineTableView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_plusView.mas_bottom);
            make.left.right.mas_equalTo(_backGroundView);
            make.height.mas_equalTo(size.height);
        }];
    }];

CollectionView高度计算

总体思路与TableView 基本相似,但是要在CollectionView的

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;

方法中得到最后一个item所在的frame,根据frame 计算CollectionView整体的高度;

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    XMHDTradeRecordViewItem *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdItem forIndexPath:indexPath];
    if (indexPath.item == self.modelArray.count) {
         CGRect rect = [cell convertRect:cell.frame toView:self.view];
       [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(self.view);
        make.height.mas_equalTo(height + k_MARGIN);
    }];
    }
    return cell;
}

你可能感兴趣的:(动态计算TableView和CollectionView的高度)