ios - UICollectionView CoreData. Missplaced cells after update -


i have collectionview 1 section showing grid of images. problem when updates , result count smaller initial item count. cells placed randomly in "old" empty spots want collectionview place them 1 after other starting top left.

im using code when doing updates:

// fetch request above code [_collectionview performbatchupdates:^{     [_collectionview reloadsections:[nsindexset indexsetwithindex:0]]; } completion:nil]; [_collectionview reloaddata]; 

i have no custom flowlayout. i'm using this:

uicollectionviewflowlayout *layout = [[uicollectionviewflowlayout alloc] init]; layout.minimuminteritemspacing = 0; layout.minimumlinespacing = 0; 

should delete cells in animation block?

is there way force collectionview reload completely?

edit:

- (uicollectionviewcell *)collectionview:(uicollectionview *)collectionview   cellforitematindexpath:(nsindexpath *)indexpath {     ftrecipeindexcell *cell = [collectionview dequeuereusablecellwithreuseidentifier:cellidentifier forindexpath:indexpath];  if (!cell) {     cell = [[ftrecipeindexcell alloc] init]; } recipe *recipe = [fetchedresultscontroller objectatindexpath:indexpath]; cell.imageview.image = [uiimage imagewithdata:recipe.thumbnail]; float size = _collectionview.bounds.size.width / 6; [cell setframe:cgrectmake(0, 0, size, size)];    return cell; }  -(nsinteger)collectionview:(uicollectionview *)collectionview numberofitemsinsection:(nsinteger)section { return [fetchedresultscontroller.fetchedobjects count]; } 

got work. setting cellframe incorrectly resulting in wild west positioning of cells. since use imageview on cell replaced custom cell code.

- (uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath {     uicollectionviewcell *cell = [collectionview dequeuereusablecellwithreuseidentifier:cellidentifier forindexpath:indexpath];     recipe *recipe = [fetchedresultscontroller objectatindexpath:indexpath];     uiimageview *imageview = [[uiimageview alloc] initwithimage:[uiimage imagewithdata:recipe.thumbnail]];      float size = _collectionview.bounds.size.width / 6;     [imageview setframe:cgrectmake(0, 0, size, size)];    [cell addsubview:imageview];  return cell; } 

edit code:

    if (!cell) {     cell = [[ftrecipeindexcell alloc] init]; } 

delete code block. not needed, can cause problems if

ftrecipeindexcell *cell = [collectionview dequeuereusablecellwithreuseidentifier:cellidentifier forindexpath:indexpath]; 

for reason wouldn't return cell.

if change content count in section, uicollectionview has change through delegate method:

- (nsinteger)collectionview:(uicollectionview *)collectionview numberofitemsinsection:(nsinteger)section 

meaning, if don't supply record count per section dynamically here, uicollectionview assume old count still valid , dequeue cells old content don't want more.

if implement correctly, should have no problems it, since have image view subview cell , have connected property outlet. adding content uicollectionviewcell programmatically, however, require additional tricks if want work , efficient.

edit:

 - (uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath {     uicollectionviewcell *cell = [collectionview   dequeuereusablecellwithreuseidentifier:cellidentifier forindexpath:indexpath];     recipe *recipe = [fetchedresultscontroller objectatindexpath:indexpath];     uiimageview *imageview = [[uiimageview alloc] initwithimage:[uiimage imagewithdata:recipe.thumbnail]];      float size = _collectionview.bounds.size.width / 6;     [imageview setframe:cgrectmake(0, 0, size, size)];    [cell addsubview:imageview];  return cell; } 

the code above not enough:

never add content view, contentview of cell need manage content more clever, when add cell programatically, @ end more clear, since don't spread logic between code , nib. suggest following:

- (uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath { uicollectionviewcell *cell = [collectionview dequeuereusablecellwithreuseidentifier:cellidentifier forindexpath:indexpath]; recipe *recipe = [fetchedresultscontroller objectatindexpath:indexpath]; uiimageview *imageview = [[uiimageview alloc] initwithimage:[uiimage imagewithdata:recipe.thumbnail]]; float size = _collectionview.bounds.size.width / 6;  [imageview setframe:cgrectmake(0, 0, size, size)];  // remove subviews, otherwise content overlap (uiview *current in [cell.contentview subviews]) {[current removefromsuperview];} // add contentview property [cell.contentview addsubview:imageview]; return cell; 

}


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -