[英]Where to add subviews in UIScrollView
Is there some "best-practice" way to add subviews to a UIScrollView
? 是否有一些“最佳实践”方式将子视图添加到UIScrollView
?
For Example: 例如:
I have a UIViewController
which loads images with corresponding description from a server. 我有一个UIViewController
,它从服务器加载具有相应描述的图像。 Those images+descriptions should be added in a horizontal scrollView. 这些图像+说明应添加到水平scrollView中。
When the data is loaded, should I create a method in my UIView
to handle this stuff, or is it more convenient to do this in my UIViewController
? 加载数据时,我应该在UIView
创建一个方法来处理这些问题,还是在UIViewController
执行此操作更方便?
Your view controller would be an most appropriate place to handle this, as controllers should connect your view (the scroll view) to the data (the images). 您的视图控制器将是处理此问题的最合适的位置,因为控制器应将您的视图(滚动视图)连接到数据(图像)。
If the scroll view's layout is simple, it could be done entirely with the controller. 如果滚动视图的布局很简单,则可以完全通过控制器来完成。 But, if the layout is complex, you may want to consider subclassing UIScrollView
and handling some of the layout there (similar to how UITableView
works). 但是,如果布局复杂,则可能需要考虑将UIScrollView
子类化并在那里处理一些布局(类似于UITableView
工作原理)。
For more info on standard application architecture, read Concepts in Objective-C Programming on Apple's developer site. 有关标准应用程序体系结构的更多信息,请阅读Apple开发人员网站上的Objective-C编程中的概念 。
By my way of looking at it, the separation of concerns would break down like this: 从我的角度来看,关注点分离会像这样分解:
UIViewController
is responsible for getting the images and handing them to the UIView
UIViewController
负责获取图像并将其交给UIView
UIView
is responsible for displaying them. UIView
负责显示它们。 So the -addSubview:
is a view implementation detail, should go in the view, and be opaque to the controller. 因此, -addSubview:
是视图实现的详细信息,应放入视图中,并且对于控制器不透明。
There is plenty of opinion that both UIViewController
and UIView
are confused and intertwined in such a way to make them both part of the conceptual "view" (in a MVC sense), but I think it's still cleaner this way. 有很多观点认为, UIViewController
和UIView
两者都被混淆和交织在一起,从而使它们都成为概念性“视图”的一部分(从MVC的角度来看),但是我认为这种方式仍然更加干净。
As you can see, opinions differ. 如您所见,意见不同。 :) Personally, I despair of MVC, and think Model-View-ViewModel to be a much clearer, cleaner, and more sensible way of approaching all this. :)就我个人而言,我对MVC感到绝望,并认为Model-View-ViewModel是一种更清晰,更清洁,更明智的方法。
UICollectionView is same as that of UITableView except it is meant to display Images and its description. 除了用于显示图像及其描述之外,UICollectionView与UITableView相同。
Here is what you can do. 这是您可以做的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.