ios - 带有整页单元格的 UICollectionView VS 带有子视图控制器的 UIScrollView
问题描述
我正在尝试创建一个具有可滑动(类似 android 的标签)页面的 ViewController。这些页面本身将在其中包含滚动视图(垂直)和多个视图,这些视图将根据响应类型(每个页面的不同网络调用)动态添加。我不能使用 PageViewController 因为我希望页面只占据屏幕的一半。
CollectionView 的问题 -
- 如果单元格将被重用(或从内存中删除),我将如何维护单元格 UI 的状态并存储该数据(对于视图尤其困难,因为每个页面中可能有不同类型的视图)
ScrollView 的问题 -
- 我担心如果所有页面视图控制器都在内存中并且每个视图都在内存中,是否会出现内存问题
PS - 每个页面中的数据将是 4-10 个堆栈视图,每个堆栈视图包含 2-10 个图像/标签或只有一个集合视图
PSS - 选项卡总数不超过 10,最少为 1
解决方案
我用collectionView实现了它,因为它应该更有效地利用资源。但是我们需要缓存视图控制器的状态。这是示例
假设您有一个控制器A
,其中包含 collectionView 和您的子控制器的单元格。然后在单元格中为行
....
var childrenVC: [Int: UIViewController] = [:]
....
// cell for row
let cell: ChildControllerCell = collectionView.dequeueReusableCell(for: indexPath)
if let childController = childrenVC[indexPath.row] {
cell.contentView.addSubview(childController.view)
childController.view.frame = cell.contentView.frame
} else {
let childViewController = ChildViewController()
addChildViewController(childViewController)
childViewController.didMove(toParentViewController: self)
cell.contentView.addSubview(childController.view)
childController.view.frame = cell.contentView.frame
childrenVC[indexPath.row] = childViewController
cell.childVC = childViewController
}
return cell
....
class ChildControllerCell: UICollectionViewCell {
var childVC: UIViewController?
override func prepareForReuse() {
super.prepareForReuse()
if !contentView.subviews.isEmpty {
childVC?.willMove(toParentViewController: nil)
childVC?.view.removeFromSuperview()
childVC?.removeFromParentViewController()
}
}
}
推荐阅读
- spring-boot - 春天假装与仇恨
- reporting - 有没有地方可以通过 API 获取更精细的报告数据?
- karate - 我可以使用独立 JAR 并行运行空手道吗?
- python-3.x - 如何根据另一个数据框补充数据框?
- json - 如何将值发送到json中的父表列
- php - PHP PDO 和 Oracle DB:一般错误:933
- tkinter - Tkinter 按钮无法正常工作
- java - 在Java中执行return语句
- kubeflow - 使用 Kubeflow Pipelines kfp.ParallelFor 时聚合结果
- xamarin - Xamarin - 页面冻结而不是显示活动指示器