IOS6 new features: Uicollectionview introduction

Source: Internet
Author: User

http://blog.csdn.net/eqera/article/details/8134986

1.1. Collection View

Family:

Uicollectionview, UITableView, Nscollectionview

n is not directly equivalent to Nscollectionview

n also does not replace UITableView----brother

Why Use collection views?

n can highly customizable content presentation

n Best practices for managing data

n is very efficient even with large amounts of data

Let's start with the perceptual understanding of collection views, the following picture is a photo wall display with collection view.

1.1.1 Collection View Element

From here, we can see the overall composition of collection view, a total of three elements, as follows

n Cells (cells)

N Supplementary Views (supplementary view, equivalent to TableView header and footer)

n Decoration Views (decorative view, used to decorate the entire collectionview)

We can break down this photo wall to describe what the above three elements correspond to.

Cells such as each picture

Supplementaryviews as the white text part of the right

Decoration views such as

Finally, three elements constitute the photo wall, the following is the element composition diagram

1.1.2 Data Model and interactive data model (data provider Uicollectionviewdatasource)

Uicollectionviewdatasource is a proxy that is used primarily to provide data to collection view.

Main functions of Uicollectionviewdatasource:

N Number of sections

How many item is in the N section

N provides cell and supplementary view settings

Here we explain these features in turn.

1, Numberofsectionsincollectionview:

How many sections are there?

The answer is 2. That is, we return 2 in the Numberofsectionsincollectionview: method.

2, Collectionview:numberofitemsinsection:

How many items are there in the Section0?

The answer is 4. That is, we are in the Collectionview:numberofitemsinsection: the corresponding section 0 of the method returns 4.

3, Collectionview:cellforitematindexpath:

What should be displayed at the Section0 item 0 location?

The answer is to use the method Collectionview:cellforitematindexpath: Returns a cell, similar to a view.

4. Cell and view reuse

Here's a 5-step process to demonstrate cell and view reuse

1) For example, on the left is collection View, the right side is the cell and View of the reuse queue, just beginning, the left side of the data display content, the right of the reuse queue has no data.

2) Again, when the user shows the content below Collection view, some cell and view before Collection view is no longer displayed, this is how the system is handled?

3) Look here, the system will add unused cells and view to the reuse queue for later use.

4) How to use it again, see, when the user continues to look down on the content, the system will provide the queue in the presence of the cell and view for use.

5) End-use effects such as

5. In iOS6, cell reuse improves

In IOS6, we can use cell more conveniently, and the system always initializes the cell for us. We can use it directly. Simply follow the two-step walk:

1) Registration of the cell class must be done using the following method:

-(void) Registerclass:forcellwithreuseidentifier:

-(void) RegisterClass:forSupplementaryViewOfKind:withReuseIdentifier:

-(void) Registernib:forcellwithreuseidentifier:

-(void) RegisterNib:forSupplementaryViewOfKind:withReuseIdentifier:

2) Remove a cell from the queue in the following way:

-(ID) Dequeuereusablecellwithreuseidentifier:forindexpath:

-(ID) DequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:

Let's show you how to do cell reuse with actual code.

First step: Setting in Collection view (registration of the cell class)

In CollectionView Setup ...

[CollectionView Registerclass:[mycellclass]

forcellwithreuseidentifier:@ "my_cell_id"]

Step two: In the following function, remove a cell from the queue. The cell is no longer null-valued for additional initialization operations. All the initialization of the cell is done by the system for us. We just need to do some assignment to the cell.

-(uicollectionview*) CollectionView: (uicollectionview*) CV

Cellforitematindexpath: (nsindexpath*) Indexpath

{

MyCell *cell =[CV dequeuereusablecellwithreuseidentifier:@ "my_cell_id"];

if (!cell) {

Well, nothingreally. Never again

}

Configure Thecell ' s content

Cell.imageview.image= ...

return cell;

}

Interaction (Uicollectionviewdelegate)

Main functions of Uicollectionviewdelegate:

N Control Cell highlighting

N Control Cell Selection

N supports menu operations on the cell, such as

Both the selection and highlighting are improved in iOS, and the precise positioning of the highlight and flow is very well controlled.

The following is a list of commonly used methods, and developers can refer to the SDK's help documentation for more information.

1) Manage cell highlighting

Collectionview:shouldhighlightitematindexpath:

Collectionview:didhighlightitematindexpath:

Collectionview:didunhighlightitematindexpath:

This method Collectionview:shouldhighlightitematindexpath: The effect is as follows: note the values on the right selected and highlighted.

This method Collectionview:didhighlightitematindexpath: The effect is as follows: note the values on the right selected and highlighted.

2) Manage Cell selection

Collectionview:shouldselectitematindexpath:

Collectionview:didselectitematindexpath:

Collectionview:shoulddeselectitematindexpath:

Collectionview:diddeselectitematindexpath:

Collectionview:shouldselectitematindexpath: The effect is as

The following two methods

Collectionview:didunhighlightitematindexpath:

Collectionview:didselectitematindexpath: The effect is as follows:

Display of 1.1.3 content UicollectionviewcellStyles

No predefined cell style in IOS6

Collection View tracking cell selection and highlighting

By setting the cell's Highlight and selection properties (including child views)

If configured, this toggles background view and selected background view

Let's take a look at the following four pictures in order. Developers can understand the rules on their own.

is the Uicollectionview related class diagram, we can see

L Uicollectionview inherit from Uiscrollview,

L Zun Uicollectionviewdelegate and Uicollectionviewdatasource two protocols

L Management Uicollectionviewcell

What does it look like in the picture? By the way, there is a lack of layout. We need layout to lay out the cell and other view. Look again, the figure is more uicollectionviewlayout and uicollectionviewflowlayout.

Below we introduce the Uicollectionviewlayout

Use your own layout (uicollectionviewlayout)

Uicollectionviewlayout is an abstract base class, and you need to inherit from him to generate layout information for collection view. The function of the layout object is to determine the layout position of cells,supplementary views and decorationviews in collection view.

You need to calculate the layout property of the view below

N Cells

N Supplementary views

N Decoration views

The system also defines the layout attribute for us, uicollectionviewlayoutattributes, which mainly includes the following:

N Position

N Size

N Transparency

N ZIndex

n Transitions

The following two graphs are the layout of collection view.

1.2 Flow Layout1.2.1 Core Concepts

Uicollectionviewflowlayout is a specific layout object that is used to lay out the item in the grid and to select headers and footers. Items in collection view can be from one row or column flow to the next row or column (the row or column depends on the direction of scrolling). Each row will contain as many cells as possible, depending on the situation. Cells can be of the same size, or they can be of different sizes.

Here are some of the features of flow layout

N for linear layout

n can be configured as a grid

n A group of lines

N has headers and footers

1.2.2 Customizing Flow Layout

The main features that Flow layout can customize are as follows

n Item Size

N Line Spacing

n Inter Cell spacing

n Scrolling Direction

n Header and Footer size

N Section Inset

Let's take a detailed description of these customizable features.

Here's what you need to illustrate with global and delegate customization:

Almost all of the custom properties are in Uicollectionviewflowlayout, and delegate is actually the delegate of collection view. Uicollectionviewdelegateflowlayout just made some extensions to uicollectionviewdelegate.

Item size (size of each item)

1, can be global configuration, the following code

@property (cgsize) itemsize

Layout.itemsize= Cgsizemake (30,20);

2, can also be configured by delegate for each item, the following code

CollectionView:layout:sizeForItemAt

Indexpath:

{

return ...;

}

The effect is as shown

Line spacing (spacing per row)

1, can be global configuration, the following properties

@property (cgfloat) minimumlinespacing

2, can also be configured by delegate for each section, the following code

OllectionView:layout:minimumLineSpacingForSectionAtIndex:

Please look at the following three figures in order

Inter cell spacing (spacing per line of internal cell item)

1, can be global configuration, the following properties

@property (cgfloat) minimuminteritemspacing

2, can also be configured by delegate for each section, the following code

CollectionView:layout:minimumInteritemSpacingForSectionAtIndex:

Take a look at the following two figures, Blue is the actual item spacing, and the green is the minimum item spacing.

Scrolling direction (scrolling direction)

You can set the Scrolldirection property. Two values are as follows

Uicollectionviewscrolldirectionvertical

Uicollectionviewscrolldirectionhorizontal

Main functions:

n defines the basic behavior of flow layout

n controls the dimension of the header footer

Uicollectionviewscrolldirectionvertical effect as shown

Uicollectionviewscrolldirectionhorizontal effect as shown

Header and footer size (header and footer sizes)

Here are some explanations for the header and footer.

N also supplementary views

n provides the content by means of a data source, as follows

-collectionview:viewforsupplementaryelementofkind:atindexpath:

N Two constants (types)

Uicollectionelementkindsectionheader

Uicollectionelementkindsectionfooter

n also need to register a class and remove the view from the queue

-RegisterClass:forSupplementaryViewOfKind:withReuseIdentifier:

-registernib:forsupplementaryviewofkind:withreuseidentifier:

-dequeuereusablesupplementaryviewofkind:withreuseidentifier:forindexpath:

The size of the header and footer is configured as follows:

1) can be configured globally with the following properties

@property (cgsize) headerreferencesize

@property (cgsize) footerreferencesize

2) You can also configure each section via delegate, as shown in the following code

CollectionView:layout:referenceSizeForHeaderInSection:

CollectionView:layout:referenceSizeForFooterInSection:


The properties of the header footer are as follows

When vertical, you need to set the height, as

When level, you need to set the width, as

Section Inset

Let's start with two graphs to see what's going on with sections insets.

From the two graphs above, we probably know that section inset is the boundary range of the cell in a certain sections.

There are also two ways to configure Sectioninset

1, through the global configuration, the following properties

@propertyUIEdgeInsets Sectioninset;

2, also through the delegate to each section configuration, the following function

-(uiedgeinsets) CollectionView:layout:insetForSectionAtIndex:

DEMO


Top
8
Step

IOS6 new features: Uicollectionview introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.