2017-02-24 71 views
1

我有一個視圖控制器,其中,我有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

回答

2

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

0

UICollectionView和UITableView的是UIScrollView的子視圖,您可以使用setContentOffset做到這一點。

首先,添加一個觀察者來獲取collectionView的contentOffset。然後,使用 setContentOffset到tableView。

相關問題