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); 
} 

//当前屏幕截图

enter image description here

+0

你的意思是你想的tableview高度应动态(屏幕高度的最大值的一半及应降低高度,如果项目是小于那个)的CollectionView高度应根据该被调整对?你可以添加屏幕截图,这两个条件将有助于了解.. –

+0

@Mukesh我有共享屏幕截图 – ChenSmile

+0

@Mukesh我有,共享屏幕截图和图像,你可以检查它 – ChenSmile

如果您正在使用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]; 

    } 



    } 

enter image description here

约束的的tableView

  • 前置,顶部

约束的collectioView

  • 前置,底部,身高

现在给一个更constaraint,

  • 中的tableView &

    之间的CollectionView 0

现在采取高度约束出口从故事板的垂直空间&管理它。

无需管理tableHeight

+0

,即时通讯使用自动布局和内部设置相等的高度和我不得不使用什么逻辑来修复它, – ChenSmile

+0

不要给高度约束...而是给高度约束和tableview高度约束都应该大于等于&collectioview高度constarint应该小于等于&take outlet从你的storybaord到.h文件并设置'self.tableHeightConstraint.constant = kCellHeight * numberOfCells'&相同的collectionView,希望这会帮助你。 –

+0

可以更新我必须使用这些常数, – ChenSmile

UICollectionView和UITableView的是UIScrollView的子视图,您可以使用setContentOffset做到这一点。

首先,添加一个观察者来获取collectionView的contentOffset。然后,使用 setContentOffset到tableView。