TableView 和 CollectionView:调整 tableView 和 collectionView 的高度
Posted
技术标签:
【中文标题】TableView 和 CollectionView:调整 tableView 和 collectionView 的高度【英文标题】:TableView and CollectionView: Adjust height of tableView and collectionView 【发布时间】:2017-02-24 06:52:12 【问题描述】:我有一个视图控制器,其中有 tableView 和集合视图。我为 collectionView 和 tableView 设置了相同的高度。
前半部分是 tableView,后半部分是 collection view。我有一个服务调用来重新加载 tableview 和 collection view。
所以,我想根据其数据源调整集合视图的高度,然后根据集合视图自动调整表格视图。
注意- TableView 高度不依赖于其数据源,仅集合视图高度取决于其数据源。
//集合视图流布局
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
return CGSizeMake((self.collectionView.frame.size.width/2)-4, 68);
//当前屏幕截图
【问题讨论】:
你的意思是你想让tableview高度应该是动态的(屏幕高度的最大一半&如果项目小于那个高度应该降低高度)&collectionView的高度应该根据那个来调整吗?您可以为这两种情况添加屏幕截图吗?将有助于理解.. @Mukesh 我分享了屏幕截图 @Mukesh 我有,分享屏幕截图和图像,你能检查一下吗 是的,我正在检查等待某个时间。你的意思主要是你想设置相等的高度,但集合项目较少我的意思是项目只有 2 个或 4 个,那么集合视图高度应该相应降低 & 表视图应该增加? @Mukesh 是的,这实际上是我需要的,所以无论是什么集合视图,如果基于表格视图的高度或多或少应该增加和减少,而集合视图中没有任何空白空间 【参考方案1】:如果您使用的是auto layout
,那么您需要通过height constraints
或tableView
和collectionView
来管理它,如果您使用的是框架,那么您需要根据单元格和单元格高度设置框架动态。
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
[self adjustCollectionViewHeightForCollection:collectionView withNumberOfCollectionCells:dynamicCollectionCellArray.count];
return dynamicCollectionCellArray.count;
-(void) adjustCollectionViewHeightForCollection:(UICollectioView *)collectionView withNumberOfCollectionCells:(NSUInteger)numberOfCollectionCell
//Adjust CollectionView height constraint
int height = kConstantCollectionCellHeight * (numberOfCollectionCell /2 + numberOfCollectionCell %2);
if(height < self.view.frame.height/2)
self.collectionViewHeightConstraint.constant = kConstantCollectionCellHeight * (numberOfCollectionCell /2 + numberOfCollectionCell %2); //(Make sure height constant manage properly coz here you are managing items in the collectionView so there are two items at each row)
[self.yourCollectionView layoutIfNeeded];
[self.yourCollectionView setNeedsLayout];
else
self.collectionViewHeightConstraint.constant = self.view.frame.height/2; //(Make sure height constant manage properly coz here you are managing items in the collectionView so there are two items at each row)
[self.yourCollectionView layoutIfNeeded];
[self.yourCollectionView setNeedsLayout];
tableView 的约束
前导、尾随、顶部collectioView 的约束
前导、尾随、底部、高度现在再给出一个约束,
tableView 和 collectionView 之间的垂直空间0
现在从情节提要中获取高度约束出口并管理它。
无需管理tableHeight
【讨论】:
,我正在使用自动布局并最初设置了相等的高度以及我必须使用什么逻辑来修复它, 不要给相等的高度约束...而不是给两者的高度约束&tableview高度约束应该大于等于&collectioview高度约束应该小于等于&从你的故事板中取出出口到.h 文件并设置self.tableHeightConstraint.constant = kCellHeight * numberOfCells
和 collectionView 相同,希望现在对您有所帮助。
你能更新我必须使用这些常量的地方吗,
我已经更新了答案,您只需要以相同的方法处理相同高度的情况。好的。
通过你的代码你的表视图高度不会增加如果集合视图有 2 个项目并且如果在表数据源中没有计数我想要表的默认大小,现在集合视图项目在表视图之上并且如果集合视图中有 r 2 个项目,则底部的集合视图中的空白空间仍然存在【参考方案2】:
UICollectionView 和 UITableView 是 UIScrollView 的子视图,你可以使用 setContentOffset 来做到这一点。
首先,添加一个观察者来获取collectionView的contentOffset。然后,使用 setContentOffset 到 tableView。
【讨论】:
以上是关于TableView 和 CollectionView:调整 tableView 和 collectionView 的高度的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 UIImageView 的内容模式 UIViewContentModeScaleAspectFit 保持高度不变来避免丢失图像质量?
TableView 和 CollectionView:调整 tableView 和 collectionView 的高度