TableView和CollectionView:调整tableView和collectionView的高度
问题描述:
我有一个视图控制器,其中,我有tableView和collection视图。我为collectionView和tableView设置了相同的高度。TableView和CollectionView:调整tableView和collectionView的高度
上半年的tableView和下半年是集view.I有一个服务调用为reloding的tableview和集合视图。
所以,我想根据其数据源调整集合视图的高度,然后根据集合视图自动调整tableview。
注意 - TableView高度不取决于其数据源,只有集合视图高度取决于其数据源。
// Collection视图流布局
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake((self.collectionView.frame.size.width/2)-4, 68);
}
//当前屏幕截图
答
如果您正在使用auto layout
则需要通过两个tableView
& collectionView
& height constraints
来管理它,如果你使用的框架,那么你需要根据细胞&细胞高度设置帧动态。
- (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
- 前置,底部,身高
现在给一个更constaraint,
- 中的tableView &
之间的CollectionView
0
无需管理tableHeight
答
UICollectionView和UITableView的是UIScrollView的子视图,您可以使用setContentOffset做到这一点。
首先,添加一个观察者来获取collectionView的contentOffset。然后,使用 setContentOffset到tableView。
你的意思是你想的tableview高度应动态(屏幕高度的最大值的一半及应降低高度,如果项目是小于那个)的CollectionView高度应根据该被调整对?你可以添加屏幕截图,这两个条件将有助于了解.. –
@Mukesh我有共享屏幕截图 – ChenSmile
@Mukesh我有,共享屏幕截图和图像,你可以检查它 – ChenSmile