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.