• UICollectionView-计算高度和数据处理


    UICollectionView高度计算

    思路:collectionView高度需要根据内容去计算=>有数据了再计算高度。(collectionView不需要滚动)

    ①高度计算

    拿到数据源之后计算:

    static NSString * const ID = @"cell";
    static NSInteger const cols = 4;
    static CGFloat const margin = 1;
    #define itemWH (SCREEN_WIDTH - (cols - 1) * margin) / cols
    // 处理数据
    [self resloveData];
    //计算collectionView高度 = rows * itemWH
    //Rows = (count - 1) / cols + 1 NSInteger count = self.squareItems.count;
    NSInteger rows = (count - 1) / cols + 1;
    // 设置collectioView高度
     self.collectionView.height = rows * itemWH;
     self.tableView.tableFooterView = self.collectionView;
    // 设置tableView滚动范围:自己计算
    //self.tableView.contentSize = CGSizeMake(0, CGRectGetMaxY(self.collectionView.frame));
    // 刷新表格
    [self.collectionView reloadData];

    ②数据处理(判断缺几个)

    #pragma mark - 处理请求完成数据
    - (void)resloveData
    {
        // 判断下缺几个
        // 3 % 4 = 3 cols - 3 = 1
        // 5 % 4 = 1 cols - 1 = 3
        NSInteger count = self.squareItems.count;
        NSInteger exter = count % cols;
        if (exter) {
            exter = cols - exter;
            for (int i = 0; i < exter; i++) {
                HKSquareItem *item = [[HKSquareItem alloc] init];
                [self.squareItems addObject:item];
            }
        }
        if ([self.squareItems containsObject:@[]]) {
            [self.squareItems removeObject:@[]];
        }
    }

    效果图:

     

     

     

     

  • 相关阅读:
    webstorm破解
    macos-WebStorm安裝
    CocoaPods安装与使用
    前端之HTML
    python调用百度语音(语音识别-斗地主语音记牌器)
    MySQL学习笔记(二)
    MySQL学习笔记(一)
    python网络编程
    计算机网络基础
    python之hashlib、configparser、logging模块
  • 原文地址:https://www.cnblogs.com/StevenHuSir/p/CalculateCollectionHeight.html
Copyright © 2020-2023  润新知