簡體   English   中英

UICollectionView內部的UiviewControllers

[英]UiviewControllers inside UICollectionViewCell

我在UIViewController中有一個UICollectionView。 collectionView包含來自故事板的8個視圖控制器標識符的數組。 數組中的每個視圖控制器都繼承自保存collectionView的UIViewController。

這是代碼:

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return _contentPageRestorationIDs.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

BaseContentViewController *contentViewController = (BaseContentViewController *)[self.storyboard instantiateViewControllerWithIdentifier:self.contentPageRestorationIDs[indexPath.row]];

// Set any data needed by the VC here
contentViewController.rootViewController = self;
contentViewController.view.frame = cell.bounds;
[cell addSubview:contentViewController.view];

return cell;
}

所以我的問題是我的集合視圖滑動性能非常慢。 如何改進UICollectionView的性能? 我的視圖控制器是否在顯示時被“殺死”?

編輯:

在這里的建議后,我已經將我的數組更改為持有視圖控制器而不是標識符nsstring。

在ViewDidLoad中:

_collectionView.delegate=self;
_collectionView.dataSource=self;
PRzeroVC  *zeroVC    = [[PRzeroVC alloc]init];
PRoneVC   *oneVC     = [[PRoneVC alloc]init];
PRtwoVC   *twoVC     = [[PRtwoVC alloc]init];
PRthreeVC *threeVC   = [[PRthreeVC alloc]init];
PRfourVC  *fourVC    = [[PRfourVC alloc]init];
PRfiveVC  *fiveVC    = [[PRfiveVC alloc]init];

_base           = [[BaseContentViewController alloc]init];

_pages = [[NSArray alloc]initWithObjects:zeroVC,oneVC,twoVC,threeVC,fourVC,fiveVC, nil];

[_collectionView reloadData];



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = (UICollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
_base = (BaseContentViewController *)_pages[indexPath.row];

_base.rootViewController = self;
_base.view.frame = cell.bounds;
[cell addSubview:_base.view];

return cell;
}

我的單元格沒有顯示視圖任何想法為什么?

通過使用隊列來保持和重用一定數量的實例,我能夠獲得包含每個子視圖控制器的單元集合的平滑滾動性能。

GitHub列出了許多重用隊列實現; 並且,在對幾乎每一個人進行相當徹底的審查之后,我可以自信地推薦這個:

https://github.com/acoomans/ACReuseQueue

根據自述文件:重用隊列是在對象分配和初始化耗時時快速重用對象的一種方法。

您的視圖未顯示的原因是您沒有從單元子類中實例化子視圖控制器,也沒有從cellForItemAtIndexPath中添加視圖。 這是關於范圍的,你已經倒退了。

這里提供了向子單元添加子視圖控制器的完美說明:

http://khanlou.com/2015/04/view-controllers-in-cells/

instantiateViewControllerWithIdentifier:的文檔instantiateViewControllerWithIdentifier: -

每次調用時,此方法都會創建指定視圖控制器的新實例

你最好存儲一個UIViewController數組,這樣它們就可以被重用,而不是每次調用tableView:cellForItemAtIndexPath:都存儲ID並實例化一個新的視圖控制器。

此外,由於重新使用了UICollectionViewCell ,因此每次調用tableView:cellForItemAtIndexPath:時,繼續向單元格添加子視圖是tableView:cellForItemAtIndexPath: 相反,請考慮使用mainView屬性創建自己的UICollectionViewCell子類。 在設置新的框架(或添加布局約束)並將其添加為子視圖之前,您可以覆蓋setMainView:從其超級視圖中刪除舊的mainView。

您還應該實現適當的UIViewController包含的方法。

使用Swift更新(在collectionview的單元格中添加viewcontrollers)

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
    let cellContentVC = self.storyboard?.instantiateViewController(withIdentifier: screens[indexPath.row])
    cellContentVC?.view.frame = cell.bounds
    cell.addSubview(cellContentVC!.view)

    return cell

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM