简体   繁体   English

在UIScrollView中的何处添加子视图

[英]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: 从我的角度来看,关注点分离会像这样分解:

  • the 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. 有很多观点认为, UIViewControllerUIView两者都被混淆和交织在一起,从而使它们都成为概念性“视图”的一部分(从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. 这是您可以做的。

  1. Create a UICollectionView, it is the subclass of UIScrollView. 创建一个UICollectionView,它是UIScrollView的子类。 SO there is no need of UIScrollview. 因此,不需要UIScrollview。
  2. Since you need image and description both for each item, I will recommend delete the UICollectionViewItem from UICollectionView. 由于每个项目都需要图像和描述,因此建议从UICollectionView删除UICollectionViewItem。
  3. Create a seperate custom UICollectinViewItem class, define UICollectionItem in .xib file (UIImageVIew and UILabel). 创建一个单独的自定义UICollectinViewItem类,在.xib文件(UIImageVIew和UILabel)中定义UICollectionItem。
  4. When you define the datasource method "– collectionView:cellForItemAtIndexPath:" define the UICollectionVIewItem class here. 当您定义数据源方法“ – collectionView:cellForItemAtIndexPath:”时,请在此处定义UICollectionVIewItem类。 Exactly we defines custom UITableVIewCell. 确实,我们定义了自定义UITableVIewCell。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM