UICollectionView和UITableView自動計算cell大小

UITableView在iOS 7.0引入了 estimatedRowHeight 只要給cell設置好足夠的約束,系統就可以自動計算出cell的大小,再也不用費勁寫計算了.

之后在iOS8.0 UICollectionView也加入了這種機制,只要設置 estimatedItemSize 一個非zero的值就可以通過約束來自動計算大小.

和UITableView不同的是,UICollectionView只要設置 estimatedItemSize 的值和添加正確的約束就可以 不需要其他設置,而UITableView還需要在代理方法 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; 中返回一個 UITableViewAutomaticDimension 常量

雖然少了一步,但是UICollectionView自動計算大小的約束卻有點不一樣 因為UICollectionCell除了約束高度還需要設置寬度的約束,下面放出tableViewCell和collectionCell約束的設置對比

collectionCell
tableViewCell

tableViewCell約束設置比較簡單,只需要約束好四周的邊距就可以了,而collectionCell因為要約束寬度 所以比較麻煩,圖中我設置了collectionCell的寬度是小于等于100 上下左右邊距為0 其中右邊距約束等級小于其他約束,對于tableViewCell我只設置了上下左右的邊距,右邊距間距為100
下面放出效果圖

collectionView
tableView

然后是代碼
數據源

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString * str = @"asdasdjaslkdjaslkdj阿斯達所大所大所大撒所多撒奧所多alksjdlajlsjdasjdklasjd";
    NSMutableArray *array = [NSMutableArray array];
    for (int i = 0; i < 100; i++ ) {
        NSString *newStr = [str substringToIndex:(int)(arc4random() % (str.length - 1))];
        [array addObject:newStr];
    }
    self.dataList = array.copy;
    [self setTableView];
}

tableView

- (void)setTableView{
    UITableView *tab = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:tab];
    tab.estimatedRowHeight  =100; // 這里只要不是0就可以 設置什么值無所謂
    tab.delegate = self;
    tab.dataSource = self;
    [tab registerNib:[UINib nibWithNibName:NSStringFromClass([TableViewCell class]) bundle:nil] forCellReuseIdentifier:@"tabCell"];
    
}

#pragma mark - <tableView>
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tabCell" forIndexPath:indexPath];
    
    cell.lable.text = [self.dataList objectAtIndex:indexPath.row];
    return cell;
}

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

collectionView

- (void)setCollection{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.estimatedItemSize = CGSizeMake(100, 100); // 這里只要不是0就可以 設置什么值無所謂
    UICollectionView *vc =[[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    vc.delegate = self;
    vc.dataSource = self;
    [self.view addSubview:vc];
    vc.backgroundColor = [UIColor whiteColor];
    [vc registerNib:[UINib nibWithNibName:NSStringFromClass([CollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:@"cell"];
}
#pragma mark - <collection>

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CollectionViewCell * cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor =[UIColor yellowColor];
    NSString *newStr = [self.dataList objectAtIndex:indexPath.row];
    cell.label.text = newStr;
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataList.count;
}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容