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 ElementFrom 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 UicollectionviewcellStylesNo 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. |