今天研究了用Masonry庫+UITableView(嵌套UICollectionView),實現(xiàn)高度動態(tài)自適應(yīng)。
線上動態(tài)效果圖如下:
----
主要代碼:
1. 初始化并設(shè)置tableView
????設(shè)置tableView.estimatedRowHeight = 100;//預(yù)估高度
? ? tableView.rowHeight = UITableViewAutomaticDimension;//高度自適應(yīng)
? ? tableView.tableFooterView = [[UIView alloc] init];//底部不會多出空白齐蔽。
2.? ?添加并約束tableView
? ? [self.view addSubview:tableView];
????[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { ? ? ? ? ? ? ? ? ??
????????????if(@available(iOS11.0, *)) {// iOS11之后,適應(yīng)SafeArea安全區(qū)域床估,masonry做的適配含滴。如此設(shè)置 即可適配iPhoneX 劉海屏系列
? ? ? ? ? ????? make.left.equalTo(weakSelf.view.mas_safeAreaLayoutGuideLeft);
? ? ? ? ????? ? make.top.equalTo(weakSelf.view.mas_safeAreaLayoutGuideTop);
? ? ? ? ? ????? make.right.equalTo(weakSelf.view.mas_safeAreaLayoutGuideRight);
? ? ? ? ????? ? make.bottom.equalTo(weakSelf.view.mas_safeAreaLayoutGuideBottom);
? ? ? ? ????? ?}else{
? ? ? ? ????? ? make.edges.equalTo(weakSelf.view);
? ? ? ????? }
3. 造數(shù)據(jù),實現(xiàn)tableView相關(guān)代理丐巫,構(gòu)建tableViewCell 然后初始化cell中的CollectionView并進(jìn)行約束
? ??CGFloat wh = (kScreenWidth - 20)/3.0;
? ? flowLayout.itemSize=CGSizeMake(wh, wh);//這里用固定的itemSize
? ? flowLayout.minimumLineSpacing = 10;//行間距
? ? flowLayout.minimumInteritemSpacing = 10;//列間距
? ???[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
?? ? ? ?make.edges.equalTo(weakSelf.contentView);//上左下右均約束到contentView的上左下右邊緣谈况。
? ? ? ? make.height.mas_equalTo(wh).priority(100);//特別的是:這里需要加個height的約束,并優(yōu)先級設(shè)置為低递胧。 沒加這條約束碑韵,初次顯示時會不正常。因為初次顯示此cell的內(nèi)容如果沒撐開contentView的話缎脾,就默認(rèn)顯示44高度祝闻。
? ? }];
4. 傳入數(shù)據(jù)到tableViewCell,點擊collectionView中的最后一個item遗菠,新造一條數(shù)據(jù)联喘,然后更新約束,適應(yīng)高度變化舷蒲。
? - (void)setImgs:(NSMutableArray*)imgs{
????? ? _imgs= imgs;
? ????? [self.collectionView reloadData];
}
?- (void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath{
?????if(indexPath.item==self.imgs.count-1) {
? ? ? ????? //點擊"+"耸袜,追加一條數(shù)據(jù)
? ? ????? ? [self.imgs insertObject:[self.imgs firstObject] atIndex:0];
? ? ????? ? //刷新collectionView
? ? ????? ? [self.collectionView reloadData];//此處必須要reloadData,以用最新的數(shù)據(jù)源刷新并撐開collectionView的ContentSize牲平。
? ? ? ????? CGFloat updateHeight = self.collectionView.collectionViewLayout.collectionViewContentSize.height;//這里就拿最新?lián)伍_的ContentSize去更新高度約束。
? ? ? ????? [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
? ? ? ? ? ????? make.height.mas_equalTo(updateHeight).priority(100);//這里必須要和前面的heigth約束語句一致域滥。 否則會被masonry認(rèn)為是新的一條約束纵柿,然后添加,導(dǎo)致Xcode輸出控制臺打印約束警告启绰。
? ? ? ????? }];
? ? ? ????? //collectionView更新約束昂儒,回調(diào)通知tableView reload,然后撐開了tableViewCell的contentView進(jìn)而撐開tableViewCell委可,實現(xiàn)高度自適應(yīng)渊跋。
? ? ????? ? if (self.addBlock) {
? ? ? ? ????? ? self.addBlock();
? ? ? ????? }
? ????? }
????}
?5. 在addBlock回調(diào)里
????cell.addBlock= ^() {
? ? ? ? [weakSelf.tableView reloadData];????
? ? ? ? //為了避免抖動腊嗡。
? ? ? ? [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];
?};
? ? }];