[iOS advanced] uicollectionview to achieve waterfall streaming effect

Source: Internet
Author: User


Uicollectionview was introduced in 2012, is not a new technology, here just to do a simple implementation.

Collection View: Uicollectionview
Like Uicollectionview and UITableView, it is also the DataSource and delegate design pattern: DataSource provides the data source for the view, tells the view what to display, and how to show them, Delegate provides some styling details as well as the response of the user interaction.
In CollectionView, the layout of the cell is more complex, specifically, to make the. class to describe the layout and behavior of CollectionView, this is uicollectionviewlayout. Uicollectionviewlayout is an abstract base class that, when used, creates new objects with its subclasses. The most commonly used uicollectionviewlayout subclass is Uicollectionviewflowlayout (provided by Apple). Flow layout is simply a line-aligned layout, which can be done with a waterfall flow style like the Youku client. Of course Uicollectionviewlayout also has other interesting subcategories such as stacked layouts, circular layouts,Cover Flow layouts, and so on.

Here you use the uicollectionviewflowlayout to implement a video player layout. The data analysis of the people can ignore or add their own desired pictures, etc., mainly about the formation of layout.

Environment: Xcode6,arc, Pure code

Viewdidload method

    Register the class//header view that you want to use in Viewdidload    [_collectionview Registerclass:[headview class] Forsupplementaryviewofkind:uicollectionelementkindsectionheader withreuseidentifier:@ "header"];    Item (cell) with scrolling picture    [_collectionview Registerclass:[scrollcollectionviewcell class] forcellwithreuseidentifier:@ "Scrollcell"];    Single picture of item (cell)    [_collectionview Registerclass:[customcollectionviewcell class] Forcellwithreuseidentifier: @ "Cell"];

Other methods in Viewcontroller

#pragma mark Collection View//Returns the item number per partition-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView    Numberofitemsinsection: (Nsinteger) section{if (section==0) {return 1;    } nsstring*category=[_allcategory Objectatindex:section];    if ([_allvideodic objectforkey:category]) {return [[_allvideodic objectforkey:category] count]; }else return 0;} Generate item-(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: ( Nsindexpath *) indexpath{//The first partition has only one item and differs from other partitions if (indexpath.section==0) {Scrollcollectionviewcell*scroll        Cell=[collectionview dequeuereusablecellwithreuseidentifier:@ "Scrollcell" Forindexpath:indexpath];        [Scrollcell initwitharray:[_allvideodic objectforkey:@ "ad"];    return Scrollcell; }else{//Other partitions Customcollectionviewcell*cell=[collectionview dequeuereusablecellwithreuseidentifier:@ "Cell"        Forindexpath:indexpath]; Videomodel*video=[[_allvideodic objectforkey:[_allcategory ObjectatinDex:indexpath.section]]objectatindex:indexpath.row];    [Cell Initdatawith:video];    return cell; } return nil;} Returns the size of item-(cgsize) CollectionView: (Uicollectionview *) CollectionView layout: (Uicollectionviewlayout *) Collectionviewlayout Sizeforitematindexpath: (Nsindexpath *) indexpath{if (indexpath.row==0&&    indexpath.section==0) {return Cgsizemake (Width, 200); } return Cgsizemake (80, 130);} Number of partitions returned-(Nsinteger) Numberofsectionsincollectionview: (Uicollectionview *) collectionview{return [_allcategory Count ];} Returns the size of the Headerview-(cgsize) CollectionView: (Uicollectionview *) CollectionView layout: (Uicollectionviewlayout *) Collectionviewlayout referencesizeforheaderinsection: (Nsinteger) section{if (section==0) {return CGSizeMake (Wid    Th, 0); }else return Cgsizemake (Width, 20);} Returns the headerview-(Uicollectionreusableview *) of each partition CollectionView: (Uicollectionview *) CollectionView Viewforsupplementaryelementofkind: (NSString *) kind Atindexpath: (Nsindexpath *) indexpath{if (Indexpath.section!=0&&[kind Isequaltostring:uicollectionelementkindsectionheader]) {Stati        C nsstring *[email protected] "header"; Headview*view=[collectionview Dequeuereusablesupplementaryviewofkind:kind Withreuseidentifier:reuseidentifier        Forindexpath:indexpath];        Nsstring*title;                Switch (indexpath.section) {Case 1: [email protected] "Recommended Today";            Break                Case 2: [email protected] "domestic micro-shadow";            Break                Case 3: [email protected] "International micro-shadow";            Break        Default:break;        } View.title=title;    return view;    } return nil; }

:




[iOS advanced] uicollectionview to achieve waterfall streaming effect

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.