iOS does not customize the Uicollectionviewcell directly in the Proxy method implementation

Source: Internet
Author: User

As we all know, CollectionView in the data source method, need to join the custom cell, sometimes a simple cell, only need very simple UI, this time customization, it seems superfluous, here, recommend a custom method. Can solve reuse problems and can be created quickly.

The Uicollectionview and Uicollectionviewcontroller classes are iOS6 newly introduced APIs for displaying collection views, more flexible layouts, and multiple-column layouts, similar to UITableView and Uitableviewcontroller class.

Using Uicollectionview must implement Uicollectionviewdatasource,uicollectionviewdelegate, Uicollectionviewdelegateflowlayout these three protocols

The code is as follows Copy Code

-(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) Indexpath
{
if (style ==1) {
Keepcarbrandcollectionviewcell *cell = (Keepcarbrandcollectionviewcell *) [CollectionView dequeuereusablecellwithreuseidentifier:@ "Keepcarbrandcollectionviewcell" Forindexpath:indexpath];
[Cell Setbackgroundcolor:kyellowcolor];
Nsurl *url = [Nsurl urlwithstring:[nsstring stringwithformat:@ "%@%@", Kimagebaseurl,_imagearray[indexpath.row]];

[Cell.iconimageview setimagewithurl:url placeholderimage:[uiimage imagenamed:@ "pic_common_carlogo_loading"]];
NSLog (@ "%@", url);
[Cell.iconimageview Setcontentmode:uiviewcontentmodescaleaspectfit];
[Cell.brandnamelabel Settext:_titlearray[indexpath.row]];
return cell;
}else if (style = = 2)
{
Uicollectionviewcell *cell = (Uicollectionviewcell *) [CollectionView dequeuereusablecellwithreuseidentifier:@] Cartypecell "Forindexpath:indexpath];
if (cell) {
[Cell.subviews makeobjectsperformselector: @selector (Removefromsuperview)];
}
Border view
UIView *borderview = [[UIView alloc] Initwithframe:cgrectmake (5, 5, cell.width-10, cell.height-10)];
[Borderview setborder:1 Color:klinecolor];
[Cell Addsubview:borderview];
Content Label
Uilabel *contentlabel = [[Uilabel alloc] initWithFrame:borderView.frame];
Contentlabel.font = Font (16);
if (_headerarray[indexpath.section]) {
Contentlabel.text = [[_contentdic objectforkey:[_headerarray[indexpath.section] objectforkey:@ "id"]][indexPath.row ] objectforkey:@ "name"];
}
Contentlabel.textalignment = Nstextalignmentcenter;
[Cell Addsubview:contentlabel];
return cell;
}else if (style ==3)
{
Uicollectionviewcell *cell = (Uicollectionviewcell *) [CollectionView dequeuereusablecellwithreuseidentifier:@] Outputcell "Forindexpath:indexpath];
if (cell) {
[Cell.subviews makeobjectsperformselector: @selector (Removefromsuperview)];
}
Border view
UIView *borderview = [[UIView alloc] Initwithframe:cgrectmake (5, 5, cell.width-10, cell.height-10)];
[Borderview setborder:1 Color:klinecolor];
[Cell Addsubview:borderview];
Content Label
Uilabel *contentlabel = [[Uilabel alloc] initWithFrame:borderView.frame];
Contentlabel.font = Font (16);
Contentlabel.text = _contentarray[indexpath.row];
Contentlabel.textalignment = Nstextalignmentcenter;
[Cell Addsubview:contentlabel];
return cell;
}
return nil;
}

Red Highlight area, is the proxy method inside the concrete implementation, in fact, when loading the cell, will all his child space removed, lest its reuse, the occurrence of duplicate subspace.

Related Article

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.