ios - UICollectionViewLayout. Where are all my items? -
when implement uicollectionview without custom layout object, 16 of reusable cells appear. is, have 1 section 16 items in section. when create custom layout object, 1 cell appears. if change # of sections 5, 5 cells appear. why collectionview draw sections when using layout object, , items when using default grid???? missing something?
i subclass uicollectionviewlayout here:
static nsstring * const kpadlayouttype = @"gridpads"; - (id)initwithcoder:(nscoder *)adecoder { self = [super initwithcoder:adecoder]; if(self) { [self setup]; } return self; } - (void)setup { self.iteminsets = uiedgeinsetsmake(22.0f, 22.0f, 13.0f, 22.0f); self.itemsize = cgsizemake(125.0f, 125.0f); self.interitemspacingy = 12.0f; self.numberofcolumns = 4; } # pragma mark _____layout - (void)preparelayout { nsmutabledictionary *newlayoutinfo = [nsmutabledictionary dictionary]; nsmutabledictionary *celllayoutinfo = [nsmutabledictionary dictionary]; nsinteger sectioncount = [self.collectionview numberofsections]; nsindexpath *indexpath = [nsindexpath indexpathforitem:0 insection:0]; for(nsinteger section = 0; section < sectioncount; section++) { nsinteger itemcount = [self.collectionview numberofitemsinsection:section]; for(nsinteger item = 0; item < itemcount; item++) { indexpath = [nsindexpath indexpathforitem:item insection:section]; uicollectionviewlayoutattributes *itemattributes = [uicollectionviewlayoutattributes layoutattributesforcellwithindexpath:indexpath]; itemattributes.frame = [self frameforpadatindexpath:indexpath]; celllayoutinfo[indexpath] = itemattributes; } } newlayoutinfo[kpadlayouttype] = celllayoutinfo; self.layoutinfo = newlayoutinfo; } - (cgsize)collectionviewcontentsize { return self.collectionview.frame.size; } - (nsarray *) layoutattributesforelementsinrect:(cgrect)rect { nsmutablearray *allattributes = [nsmutablearray arraywithcapacity:self.layoutinfo.count]; [self.layoutinfo enumeratekeysandobjectsusingblock:^(nsstring *elementidentifier, nsdictionary *elementsinfo, bool *stop) { [elementsinfo enumeratekeysandobjectsusingblock:^(nsindexpath *indexpath, uicollectionviewlayoutattributes *attributes, bool *innerstop) { if(cgrectintersectsrect(rect, attributes.frame)) { [allattributes addobject:attributes]; } }]; }]; return allattributes; } - (uicollectionviewlayoutattributes *)layoutattributesforitematindexpath:(nsindexpath *)indexpath { return self.layoutinfo[kpadlayouttype][indexpath]; } # pragma mark _____private - (cgrect)frameforpadatindexpath:(nsindexpath *)indexpath { nsinteger row = indexpath.section/ self.numberofcolumns; nsinteger column = indexpath.section % self.numberofcolumns; cgfloat spacingx = self.collectionview.bounds.size.width - self.iteminsets.left - self.iteminsets.right - (self.numberofcolumns * self.itemsize.width); if(self.numberofcolumns > 1) spacingx = spacingx / (self.numberofcolumns - 1); cgfloat originx = floorf(self.iteminsets.left + (self.itemsize.width + spacingx) * column); cgfloat originy = floor(self.iteminsets.top + (self.itemsize.height + self.interitemspacingy) * row); return cgrectmake(originx, originy, self.itemsize.width, self.itemsize.height); }
i invalidate layout in each setter.
i using collectionview within uiviewcontroller in viewcontroller register cell class use in viewdidload , setup following:
# pragma mark - uicollectionviewdatasource - (nsinteger)collectionview:(uicollectionview *)collectionview numberofitemsinsection:(nsinteger)section { return 16; } - (nsinteger)numberofsectionsincollectionview:(uicollectionview *)collectionview { return 1; } - (uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath { pad *mypad = [collectionview dequeuereusablecellwithreuseidentifier:kpadlayouttype forindexpath:indexpath]; return mypad; }
it seems me frameforpadatindexpath:
method return same frame each item within same section , varies position section.
changing
nsinteger row = indexpath.section/ self.numberofcolumns; nsinteger column = indexpath.section % self.numberofcolumns;
into
nsinteger row = indexpath.item/ self.numberofcolumns; nsinteger column = indexpath.item % self.numberofcolumns;
might give better results.
Comments
Post a Comment