Friday 15 March 2013

ios - Fixed Static Cell in UICollectionView -



ios - Fixed Static Cell in UICollectionView -

in ipad application, have used uicollectionview displays different notes saved user in core info through nsfetchedresultscontroller. according design, must have show cell command add together new note in core data. 1 time note added displayed 2nd cell in collection view.

i tried implement in next manner;

#pragma mark - uicollectionviewdatasource - (nsinteger)numberofsectionsincollectionview:(uicollectionview*)collectionview { homecoming self.fetchedresultscontroller.sections.count?:1; } - (nsinteger)collectionview:(uicollectionview *)collectionview numberofitemsinsection: (nsinteger)section { id<nsfetchedresultssectioninfo> sectioninfo = [[self.fetchedresultscontroller sections] objectatindex:section]; homecoming [sectioninfo numberofobjects]?:1; } - (uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath { if (indexpath.section == 0 && indexpath.item == 0) { addnewnotecell *cell = [collectionview dequeuereusablecellwithreuseidentifier:@"addnewnotecell" forindexpath:indexpath]; cell.delegate = self; homecoming cell; } else { notecell *cell = [collectionview dequeuereusablecellwithreuseidentifier:@"notecell" forindexpath:indexpath]; //code customization homecoming cell; } }

but approach causing problems. doesn't add together newly created note cell immediately. if adds, newly added cell displayed "add new cell". if pop view controller , load view controller 1 time again displays cells correctly.

please guide me accurate solution. possible?

assuming want "add new note" cell appear first item in first section, not in other sections, need modify numberofitemsinsection method add together 1 [sectioninfo numberofobjects] section 0. other sections, should homecoming numberofobjects:

- (nsinteger)collectionview:(uicollectionview *)collectionview numberofitemsinsection: (nsinteger)section { id<nsfetchedresultssectioninfo> sectioninfo = [[self.fetchedresultscontroller sections] objectatindex:section]; nsinteger items = [sectioninfo numberofobjects]; if (section == 0) items++; homecoming items; }

then in cellforitematindexpath: can (as have) treat item 0 in section 0 special case (i.e. give different cell subclass , configure accordingly). other cells, can configure them using indexpath details fetchedresultscontroller. however, in section 0, need adjust this: cell 0 "add new note" cell, want cell 1 configured details of item 0 fetchedresultscontroller. build new indexpath:

nsindexpath *fetchindexpath = [nsindexpath indexpathforitem:(indexpath.row -1) insection:0];

and utilize right info fetchedresultscontroller.

i assume "add new note" cell calls method in view controller add together new note object managedobjectcontext. if implement nsfetchedresultscontroller delegate methods (see docs), can update collectionview newly added object. must reverse above adjustment section 0:

- (void)controllerwillchangecontent:(nsfetchedresultscontroller *)controller { [self.collectionview beginupdates]; } - (void)controller:(nsfetchedresultscontroller *)controller didchangesection:(id <nsfetchedresultssectioninfo>)sectioninfo atindex:(nsuinteger)sectionindex forchangetype:(nsfetchedresultschangetype)type { switch(type) { case nsfetchedresultschangeinsert: [self.collectionview insertsections:[nsindexset indexsetwithindex:sectionindex]]; break; case nsfetchedresultschangedelete: [self.collectionview deletesections:[nsindexset indexsetwithindex:sectionindex]]; break; } } - (void)controller:(nsfetchedresultscontroller *)controller didchangeobject:(id)anobject atindexpath:(nsindexpath *)indexpath forchangetype:(nsfetchedresultschangetype)type newindexpath:(nsindexpath *)newindexpath { nsindexpath *adjustedindexpath; nsindexpath *adjustednewindexpath; if (indexpath.section == 0) { adjustedindexpath = [nsindexpath indexpathforitem:(indexpath.row+1) insection:0]; adjustednewindexpath = [nsindexpath indexpathforitem:(newindexpath.row+1) insection:0]; indexpath = adjustedindexpath; newindexpath = adjustednewindexpath; } switch(type) { case nsfetchedresultschangeinsert: [self.collectionview insertitemsatindexpaths:@[newindexpath]]; break; case nsfetchedresultschangedelete: [self.collectionview deleteitemsatindexpaths:@[indexpath]]; break; case nsfetchedresultschangeupdate: [self configurecell:[self.collectionview cellforitematindexpath:indexpath] atindexpath:indexpath]; break; case nsfetchedresultschangemove: [self.collectionview moveitematindexpath:indexpath toindexpath:newindexpath]; break; } } - (void)controllerdidchangecontent:(nsfetchedresultscontroller *)controller { [self.collectionview endupdates]; }

you should tailor above methods suit particular needs (eg. assume don't want permit section moved ahead of section 0).

ios objective-c uicollectionview

No comments:

Post a Comment