IOS 集合视图指南2:集合视图基础

2023-05-16

Collection View Basics(集合视图基础)

To present its content onscreen, a collection view cooperates with many different objects. Some objects are custom and must be provided by your app. For example, your app must provide a data source object that tells the collection view how many items there are to display. Other objects are provided by UIKit and are part of the basic collection view design.

Like tables, collection views are data-oriented objects whose implementation involves a collaboration with your app’s objects. Understanding what you have to do in your code requires a little background information about how a collection view does what it does.

为了在屏幕上展示内容,集合视图要和很多不同的对象协作。一些对象是自定义的,必须由你的应用提供。举个例子,你的应用必须提供数据源告诉集合视图要展示多少个元素。其他对象是UIKit框架提供的作为集合视图的设计基础
和表视图类似,集合视图是数据导向的对象,完成时候设计了你的应用对象之间的协作。理解你要在代码中怎样做,需要了解集合视图的运作方法的知识。

A Collection View Is a Collaboration of Objects(集合视图是对象之间的协作)

The design of collection views separates the data being presented from the way that data is arranged and presented onscreen. Although your app is strictly responsible for managing the data to be presented, its visual presentation is managed by many different objects. Table 1-1 lists the collection view classes in UIKit and organizes them by the roles they play in implementing a collection view interface. Most of the classes are designed to be used as is without any need for subclassing, so you can usually implement a collection view with very little code. And when you want to go beyond the provided behavior, you can subclass and provide that behavior

集合视图的设计分开了展示数据和屏幕上呈现的内容。尽管你的应用严格按照数据来展现内容,但是展示的内容由多个不同对象决定。表1.1列出了UIKit中的集合视图类以及他们是如何按照在集合视图界面中扮演的角色来组织他们的。这些类中的大部分被设计成不用子类化就可以直接使用的,所以你通常可以用很少的代码完成一个集合视图。当你想要做超过系统提供的行为的时候,你可以子类化,并且提供你的自定义行为。

Table 1-1  The classes and protocols for implementing collection views  实现集合视图的类和协议

Purpose

目的

Classes/Protocols

类/协议

Description/ 描述

Top-level containment and management

最高级的包含和管理

UICollectionView

UICollectionViewController

UICollectionView object defines the visible area for your collection view’s content. This class descends from UIScrollView and can contain a large scrollable area as needed. This class also facilitates the presentation of your data based on the layout information it receives from its layout object.


UICollectionView为你的集合视图内容定义了可视区域。这个类继承自UIScrollView可以按照需要包含一片挺大的滚动区域。这个类很便于展示你的基于从布局对象获取的布局信息的数据。

UICollectionViewController object provides view controller–level management support for a collection view. Its use is optional.

UICollectionViewController 对象为集合视图提供了视图控制器级别的管理,他的使用是可选的。

Content management

内容管理

UICollectionViewDataSource protocol

UICollectionViewDelegate protocol

The data source object is the most important object associated with the collection view and is one that you must provide. The data source manages the content of the collection view and creates the views needed to present that content. To implement a data source object, you must create an object that conforms to the UICollectionViewDataSource protocol.

数据源对象是和集合视图有关的最重要的对象同事你必须完成他。数据源管理着集合视图的内容同时创建内容上的视图。为了实现数据源对象,你需要创建一个遵守数据源协议的对象。

The collection view delegate object lets you intercept interesting messages from the collection view and customize the view’s behavior. For example, you use a delegate object to track the selection and highlighting of items in the collection view. Unlike the data source object, the delegate object is optional.

集合视图协议对象让你可以拦截集合对象的有趣的信息,同时自定义视图行为。比如,你可以使用这个协议对象来跟踪选择,同时突出被选择的元素。和数据源对象不同的是,delegate的完成是可选的。

For information about how to implement the data source and delegate objects, see Designing Your Data Source and Delegate.

更多关于数据源和协议的资料,请见《设计你的数据源和协议》

Presentation

展示

UICollectionReusableView

UICollectionViewCell

All views displayed in a collection view must be instances of theUICollectionReusableView class. This class supports a recycling mechanism in use by collection views. Recycling views (instead of creating new ones) improves performance in general and especially improves it during scrolling.

所有集合视图中展示的视图都必须是UICollectionReusableView类的实例,这个类提供了集合视图中使用的回收机制。回收视图(替代创建新视图)一般可以提高新能,尤其是在滚动视图的时候

UICollectionViewCell object is a specific type of reusable view that you use for your main data items.

UICollectionViewCell对象是特定的一种重用视图,你用它作为你主要的数据项。

Layout

布局

UICollectionViewLayout

UICollectionViewLayoutAttributes

UICollectionViewUpdateItem

Subclasses of UICollectionViewLayout are referred to as layout objects and are responsible for defining the location, size, and visual attributes of the cells and reusable views inside a collection view.

UICollectionViewLayout的子类作为布局对象同时负责定义位置,大小,集合视图中的单元格与复用视图的视觉属性

During the layout process, a layout object creates layout attribute objects (instances of theUICollectionViewLayoutAttributes class) that tell the collection view where and how to display cells and reusable views.

布局的过程中,布局对象创建布局属性对象(UICollectionViewLayoutAttributes 的实例)告诉集合视图在什么位置,以什么形式展示单元格和复用视图。

The layout object receives instances of the UICollectionViewUpdateItem class whenever data items are inserted, deleted, or moved within the collection view. You never need to create instances of this class yourself.

不论何时集合视图中的数据项被插入,删除,移动,布局对象都会接受一个UICollectionViewUpdateItem类的实例。你不需要自己创建类的实例。

For more information about the layout object, see The Layout Object Controls the Visual Presentation.

更多关于布局对象的信息,请见《布局对象控制的视觉表现》

Flow layout

流动布局

UICollectionViewFlowLayout

UICollectionViewDelegateFlowLayoutprotocol

The UICollectionViewFlowLayout class is a concrete layout object that you use to implement grids or other line-based layouts. You can use the class as-is or in conjunction with the flow delegate object, which allows you to customize the layout information dynamically.

UICollectionViewLayout类是一个实现网格和其他线性布局的布局对象。你可以直接使用这个类或者实现流动代理对象,这样你就可以实现自定义动态布局。

Figure 1-1 shows the relationship between the core objects associated with a collection view. The collection view gets information about the cells to display from its data source. The data source and delegate objects are custom objects provided by your app and used to manage the content, including the selection and highlighting of cells. The layout object is responsible for deciding where those cells belong and for sending that information to the collection view in the form of one or more layout attribute objects. The collection view then merges the layout information with the actual cells (and other views) to create the final visual presentation.

Figure 1-1  Merging content and layout to create the final presentation


图1-1展示了集合视图的相关核心类之间的关系。集合视图从数据源获取单元格展示信息。数据源和委托对象是由系统提供的自定义对象被用来管理内容,包括单元格的选中和强调状态。布局对象负责选择单元格的位置和用布局属性对象的形式给集合视图发出相关信息。然后集合视图会混合布局信息和实际的单元格(和其他视图)来创建最终的界面。


When creating a collection view interface, you first add a UICollectionView object to your storyboard or nib file. Think of the collection view as the central hub, from which all other objects emanate. After adding that object, you can begin to configure any related objects, such as the data source or delegate. All configurations are centered around the collection view itself. For example, you never create a layout object without also creating a collection view object.

当你创建一个集合视图界面的时候,首先你应该在你的故事版或者xib文件上添加结合对象。把集合视图想象成一个中心枢纽,作为其他对象的起点。添加一个集合视图对象之后,你可以开始设置相关类,比如数据源和代理。所有的设置的是围绕集合视图本身展开的。比如,你不能当你没有创建集合视图对象的时候你不能创建一个布局对象。

Reusable Views Improve Performance(重用视图来提高性能)

Collection views employ a view recycling program to improve efficiency. As views move offscreen, they are removed from view and placed in a reuse queue instead of being deleted. As new content is scrolled onscreen, views are removed from the queue and repurposed with new content. To facilitate this recycling and reuse, all views displayed by the collection view must descend from theUICollectionReusableView class.

集合视图使用了一个视图复用程序来提高性能。随着视图移除屏幕,视图被移出而不是被删除,同时移出的视图之余复用队列中。随着屏幕上滑动出新的内容,复用队列中会取出视图应用到新的内容中。为了实现回收和复用,所有集合视图中展示的视图必须继承UICollectionReusableView类。

Collection views support three distinct types of reusable views, each of which has a specific intended usage:

  • Cells present the main content of your collection view. The job of a cell is to present the content for a single item from your data source object. Each cell must be an instance of theUICollectionViewCell class, which you may subclass as needed to present your content. Cell objects provide inherent support for managing their own selection and highlight state. To actually apply a highlight to a cell, you must write some custom code. For information on implementing cell highlighting/selecting, see Managing the Visual State for Selections and Highlights.

  • Supplementary views display information about a section. Like cells, supplementary views are data driven. Unlike cells, supplementary views are not mandatory, and their usage and placement is controlled by the layout object being used. For example, the flow layout supports headers and footers as optional supplementary views.

  • Decoration views are visual adornments that are wholly owned by the layout object and are not tied to any data in your data source object. For example, a layout object might use decoration views to implement a custom background appearance.

集合视图支持三种冥想不同的复用视图,每一种都有特定的用途。
  • 单元格展示集合视图的主要内。单元格的主要作用是展示数据源中的单个数据项。每个单元格都必须是UICollectionViewCell类的实例化,你需要做的是按照展示的需求来继承这个类。单元格对象为选中和强调状态提供了内在的支持。当实际是想一个单元格的突出状态的时候,你需要写一些自定义代码。更多关于实现单元格选中/强调状态的信息,请看《 Managing the Visual State for Selections and Highlights.
  • 增补视图展示关于节的信息。和单元格类似,增补视图也是数据驱动的。和单元格视图不同的是,增补视图不是强制要求的,他们的使用和位置受控于布局对象。比如,流动布局支持页眉和页脚作为增补视图。
  • 装饰视图是完全受控于布局对象的诗句装饰,和你的数据源对象里面的任何数据都没有关系。比如,一个布局对象,可以使用装饰视图来实现一个自定义背景对象。

Unlike table views, collection views impose no specific style on the cells and supplementary views provided by your data source. Instead, the basic reusable view classes are blank canvases for you to modify. For example, you can use them to build small view hierarchies, to display images, or even to draw content dynamically.

Your data source object is responsible for providing the cells and supplementary views used by its associated collection view. However, the data source never creates views directly. When asked for a view, your data source dequeues a view of the desired type using the methods of the collection view. The dequeueing process always returns a valid view, either by retrieving one from a reuse queue or by using a class, nib file, or storyboard you provide to create a new view.

For information about how to create and configure views from your data source, see Configuring Cells and Supplementary Views.

和表示图不同,集合视图不对数据源支持的单元格和增补视图提供任何强制方式。相反的,基础重用视图类就像是一块供你修改的空白画布。比如,你可以使用他们来构建更小的视图层次,来展示图像,甚至于构建动态的内容。
你的数据源对象负责提供相关集合视图的单元格和增补视图的数据。尽管如此,数据源不会直接创建视图。当你需要一个视图的时候,你的数据源对象会列出一个使用集合视图方法来确定状态的视图。列出的过程返回一个可用的视图,你可以通过检索复用队列,或者使用类来创建,或者nib文件,故事版文件来创建这个视图。
关于如何通过你的数据源来创建单元格和增补视图的信息,参见《 Configuring Cells and Supplementary Views

The Layout Object Controls the Visual Presentation(布局对象控制视觉展示)

The layout object is solely responsible for determining the placement and visual styling of items within the collection view. Although your data source object provides the views and the actual content, the layout object determines the size, location, and other appearance-related attributes of those views. This separation of responsibilities makes it possible to change layouts dynamically without changing any of the data objects managed by your app.

The layout process used by collection views is related to, but distinct from, the layout process used by the rest of your app’s views. In other words, do not confuse what a layout object does with thelayoutSubviews method used to reposition child views inside a parent view. A layout object never touches the views it manages directly because it does not actually own any of those views. Instead, it generates attributes that describe the location, size, and visual appearance of the cells, supplementary views, and decoration views in the collection view. It is then the job of the collection view to apply those attributes to the actual view objects.

There are no limits to how a layout object can affect the views in a collection view. A layout object can move some views but not others. It can move views only a little bit, or it can move them randomly around the screen. It can even reposition views without any regard for the surrounding views. For example, a layout object can stack views on top of each other if it wants. The only real limitation is how the layout object affects the visual style you want your app to have.

Figure 1-2 shows how a vertically scrolling flow layout arranges its cells and supplementary views. In a vertically scrolling flow layout, the width of the content area remains fixed and the height grows to accommodate the content. To compute the area, the layout object places views and cells one at a time, choosing the most appropriate location for each. In the case of the flow layout, the size of the cells and supplementary views are specified as properties, either on the layout object or by using a delegate. Computing the layout is just a matter of using those properties to place each view.

布局对象的唯一职责就是决定集合视图中项的位置的视觉表现形式。尽管你的数据源支持视图和实际内容,布局对象决定了视图的大小,位置,和其他视图的布局相关的属性。这种责任的分离让你可以在不改变你应用的数据源的情况下动态的布局。

集合视图布局的过程和你的应用其余视图的布局过程相关,但又有冥想区别。换句话说,不要混用布局对象的layoutSubviews方法(这个方法在俯视图中布局子视图)。布局对象并不直接作用于视图,因为它并不实际拥有这些视图。相反的,它产程描述单元格,增补视图,装饰视图的位置,大小和视觉表现形式的属性。然后集合视图回把这些属性应用到真实的视图对象上

布局对象作用在集合视图上没有什么限制。布局对象可以移动一些视图,但是不能移动其他的。布局对象可以只挪动视图一点点,也可以把他们移动到屏幕上的随机位置。它甚至可以把视图放到和周围视图没有关系的位置。比如,只要你想,你可以让布局对象把视图们排成一排。真正能限制布局对象的是你的应用到底想要哪种展示方式?

图2-1展示了一个垂直滚动刘布局安排单元格和增补视图。在垂直滚动布局中,内容视图的宽是固定的,高根据内容视图来调整以容纳内容视图。布局对象通过计算区域来一次性布局所有的视图和单元格,并为所有的视图选好合适的位置。在这个流布局中,单元格和增补视图的大小被指定为属性,不管是在布局对象,或者是代理。计算布局就是使用这些属性来确定每个视图的位置。

Figure 1-2  The layout object provides layout metrics

Layout objects control more than just the size and position of their views. The layout object can specify other view-related attributes, such as its transparency, its transform in 3D space, and its visibility (if any) above or below other views. These attributes let you create more interesting layouts. For example, you might create stacks of cells by placing the views on top of one another and changing their z-ordering, or you might use a transform to rotate them on any axis.

For detailed information about how a layout object fulfills its responsibilities to the collection view, see Creating Custom Layouts.

布局对象控制的不仅仅是大小和位置。布局对象可以指定其他的视图相关属性,比如它的透明度,它在3D空间的变化,它的能见度(如果有的话)高于或者低于其他视图。这些属性让你可以创建更多有趣的布局。比如,你可以创建一个通过一个单元格放在另一个单元格的顶部构成的栈同时更改他们的z-ordering,或者让他们绕着任意轴旋转。
更多关于布局对象融合履行它对集合视图的责任的信息,请见《创建自定义布局》。

Collection Views Initiate Animations Automatically (集合视图启动自动动画)

Collection views build in support for animations at a fundamental level. When you insert (or delete) items or sections, the collection view automatically animates any views impacted by the change. For example, when you insert an item, items after the insertion point are usually shifted to make room for the new item. The collection view can create these animations because it detects the current position of items and can calculate their final positions after the insertion takes place. Thus, it can animate each item from its initial position to its final position.

In addition to animating insertions, deletions, and move operations, you can invalidate the layout at any time and force it to recalculate its layout attributes. Invalidating the layout does not animate items directly; when you invalidate the layout, the collection view displays the items in their newly calculated positions without animating them. Instead in a custom layout, you might use this behavior to position cells at regular intervals and create an animated effect.

集合视图对动画有基本水平的支持。当你插入(或者删除)元素或者节的时候,集合根据视图的改变发生自动动画。比如呢,当你插入一个元素的时候,插入点后面的元素通常移动到新元素的位置。集合视图可以生成动画,因为可以通过查询现在元素的位置计算出插入后的最终位置。这样呢它就可以展示元素从初始位置到最终位置之间的动画了。
除了插入,删除,移动操作,你可以在任何时候无效化布局并强制重新计算布局属性。无效果布局没有动画。当你无效化布局的时候,集合视图展示元素的新位置同时没有动画。相反的时一个自定义布局,你可以使用这个行为在定位单元格位置的合适时间间隔发生动画。
本文原创,转载请注明出处:http://blog.csdn.net/zhenggaoxing/article/details/44083049

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

IOS 集合视图指南2:集合视图基础 的相关文章

随机推荐

  • Apache配置中文说明

    APACHE的配置 xff08 中文 xff09 基于 NCSA 服务的配置文件 这是Apache服务器主要配置文件 它包含服务器的影响服务器运行的配置指令 参见以取得关于这些指令的详细信息 不要只是简单的阅读这些指令信息而不去理解它 这里
  • ubunut 18.04安装xrdp及pulseaudio-module-xrdp

    1 桌面环境 sudo apt install xfce4 2 xrdp服务 sudo apt install xrdp 参考 xff1a xrdp 3 远程声音重定向 确认pulseaudio版本为11 1 pulseaudio vers
  • ios8 新的AlertView

    转自cocoachina OS 8的新特性之一就是让接口更有适应性 更灵活 xff0c 因此许多视图控制器的实现方式发生了巨大的变化 全新的UIPresentationController在实现视图控制器间的过渡动画效果和自适应设备尺寸变化
  • Debian 系统时间配置(NTP)

    系统时间NTP和RTC同步 xff0c Debian的时区配置 系统及架构 xff1a Linux Matrix 061001 5 10 42 yocto standard 1 Thu Jun 3 07 00 52 UTC 2021 arm
  • ubuntu之VScode

    文章目录 0 安装1 配置中文2 插件 xff08 推荐使用 xff09 3 conda4 matlab5 fira字体设置6 字符显示配置7 terminal自定义颜色配置8 环境变量 xff1a terminal runner问题9 复
  • PPT中一打开输入法后就卡死

    解决办法 C Program Files Common Files xff08 x86 xff09 Microsoft Shared OFFICE12 Office Setup Controller xff0c 把这个文件夹删除即可
  • LaTex 使用plt.savefig保存矢量图并插入论文中

    问题描述 今天开始给实验结果画图了 xff0c 总觉得使用png格式的图片糊糊的 xff0c 于是决定研究一下如何将矢量图插入Latex中 解决方案 由于我需要插入的图片是使用python绘制的并通过plt savefig保存下来 通过参考
  • AD18中制作自己的原理图模板

    新建原理图 xff0c 在原理图上使用画线等工具做出自己想要的样式 xff0c xff08 如果要在模板中加入图片 xff0c 可以直接将想要的图片用鼠标直接拖到原理图中 xff09 xff0c 制作好后将原理图另存为后缀 SchDot x
  • STM32 RTC晶振不起振原因

    今天下载程序后发现程序像死机一样 xff0c 然后仿真发现 xff0c 程序一直在等待RTC晶振就绪 xff0c 最终超时死机 然后检查的电路看都没问题 xff0c 最后通过查阅资料和咨询厂家了解到可能是晶振匹配电容的原因 如下为记录 xf
  • NO Cortex-M Device found in JTAG chain常见问题及解决方法.

    昨晚调试程序时 xff0c 由于是在别人程序基础上修改的程序 xff0c 没有对照自己的硬件原理图 xff0c 导致程序下载过后就不能再继续下载 xff0c 但那个源程序 xff08 即楼主参考的程序 xff09 还是可以继续下载 xff0
  • STM32系统时钟硬件仿真查看

    前几天回校调试基于407的程序 xff0c 以前都是在别人的程序基础上面改写只要能实现想要的功能不会管其他的 xff0c 结果基本就没用用过硬件JLINK的硬件仿真 xff0c 那晚蔡师姐帮忙一直弄到夜里12点多 xff0c 真的很感谢她
  • keil里面不能修改程序或加入程序

    楼主后知后觉 xff0c 今天打开一个工程文件 xff0c 发现不能在其中的文件 xff08 含有 xff09 里添加注释和修改 几经周折后 xff0c 发现这个问题与Keil没有关系 xff0c 这个带有锁的文件属性是 xff1a 只读
  • Alutium Designer中原理图库设计时如何设置鼠标移动元器件的最小间隔

    1 点击图中菜单栏中的高亮部分 43 43 43 43 43 2 点进去后 xff0c 选择 Set snap Grid xff0c 最小设置为1mil 完成
  • app定位、地图、坐标系的那些坑

    原文地址 xff1a http www jianshu com p f8224779ca63 开发App时会遇到各种坑 xff0c 本文分享我们在iOS Android系统中定位和地图中遇到的坑 xff0c 以及携程App的解决方案 定位
  • VMware Workstation 12安装windows server 2003

    VMware Workstation 12安装windows server 2003 无法显示图形界面报N多错问题 问题描述 xff1a 在VMware Workstation 12 xff08 下文简称VM 12 xff09 安装wind
  • 一步错,步步错,python或pip'不是内部或外部命令”你的 Path环境变量格式对了吗?实测有效!

    如果你遇到 python或 pip 不是内部或外部命令 可能是你的 Path环境变量格式输入有误 xff01 一步错 xff0c 步步错 xff01 很多人可能在安装配置 Python环境时被 python 不是内部或外部命令 也不是可运行
  • 【Cousera】北京大学 | 计算导论与C语言基础习题_05:第二个重复出现的数

    05 第二个重复出现的数 总时间限制 1000ms 内存限制 65536kB 描述 给定一个正整数数组 xff08 元素的值都大于零 xff09 xff0c 输出数组中第二个重复出现的正整数 xff0c 如果没有 xff0c 则输出字符串
  • E: 无法获取 dpkg 前端锁 (/var/lib/dpkg/lock-frontend),是否有其他进程正占用它? 解决方案

    问题描述 操作系统 xff1a Ubuntu18 04 今天在执行以下指令时 xff1a sudo apt upgrade 出现了以下错误 xff1a E 无法获得锁 var lib dpkg lock frontend open 11 资
  • 用栈实现表达式转换

    中缀转后缀 xff1a 从左到右依次扫描中缀表达式 xff0c 遇到操作数就直接写出来 xff08 顺序是从前往后写 xff09 xff0c 遇到运算符时 xff0c 判断当前运算符与栈顶运算符的优先级 xff0c 如果当前运算符的优先级小
  • IOS 集合视图指南2:集合视图基础

    Collection View Basics xff08 集合视图基础 xff09 To present its content onscreen a collection view cooperates with many differe