ios - UICollectionView move cells reverts after clicking a cell -
i have collectionview interactive cells, using long press gesture recognizer on cells allow user re-arrange them.
now re-arrange working issue have once cells have been re-arranged if click on 1 of cells revert positions in before re-arrange occured.
i have feeling has datasource im not sure.
this long press gesture re-arranges cells.
-(ibaction)longpressgesturerecognized:(id)sender { uilongpressgesturerecognizer *longpress = (uilongpressgesturerecognizer *)sender; uigesturerecognizerstate state = longpress.state; cgpoint location = [longpress locationinview:self.collectionview]; nsindexpath *indexpath = [self.collectionview indexpathforitematpoint:location]; static uiview *snapshot = nil; static nsindexpath *sourceindexpath = nil; switch (state) { case uigesturerecognizerstatebegan: { if (indexpath) { sourceindexpath = indexpath; uicollectionviewcell *cell = [self.collectionview cellforitematindexpath:indexpath]; snapshot = [self customsnapshotfromview:cell]; cgpoint center = cell.center; snapshot.center = center; snapshot.alpha = 0.0; [self.collectionview addsubview:snapshot]; [uiview animatewithduration:0.25 animations:^{ snapshot.center = center; snapshot.transform = cgaffinetransformmakescale(1.05, 1.05); snapshot.alpha = 0.98; cell.hidden = yes; } completion:nil]; } break; } case uigesturerecognizerstatechanged: { cgpoint center = snapshot.center; center.y = location.y; snapshot.center = center; if (indexpath && ![indexpath isequal:sourceindexpath]) { [people2 exchangeobjectatindex:indexpath.row withobjectatindex:sourceindexpath.row]; [self.collectionview moveitematindexpath:sourceindexpath toindexpath:indexpath]; sourceindexpath = indexpath; } break; } default: { uicollectionviewcell *cell = [self.collectionview cellforitematindexpath:sourceindexpath]; [uiview animatewithduration:0.25 animations:^{ snapshot.center = cell.center; snapshot.transform = cgaffinetransformidentity; snapshot.alpha = 0.0; cell.hidden = no; } completion:^(bool finished){ [snapshot removefromsuperview]; snapshot = nil; }]; sourceindexpath = nil; break; } }
}
i using nsnotification reload cells on click event.
-(void)handlereload:(nsnotification *)notification { nslog(@"notification recieved"); people2 = [databaseclass getdata]; [self.collectionview reloaddata];
}
any hugely appreciated.
edit: [databaseclass getdata] method -
+(nsmutablearray*)getdata { [self databaseinit]; peoplearray = [[nsmutablearray alloc] init]; if (sqlite3_open(dbpath, &peopledb)== sqlite_ok) { nsstring *selectsql = @"select id, name, points, colour people"; sqlite3_prepare_v2(peopledb,[selectsql utf8string],-1,&statement,null); while (sqlite3_step(statement)== sqlite_row) { personobject *newperson = [[personobject alloc]init]; newperson.id = [[nsstring stringwithutf8string:(char *)sqlite3_column_text(statement, 0)]integervalue]; newperson.name = [nsstring stringwithutf8string:(char *)sqlite3_column_text(statement, 1)]; newperson.points = [[nsstring stringwithutf8string:(char *)sqlite3_column_text(statement, 2)] integervalue]; newperson.colour = [nsstring stringwithutf8string:(char *)sqlite3_column_text(statement, 3)]; [peoplearray addobject:newperson]; } } sqlite3_finalize(statement); sqlite3_close(peopledb); return peoplearray;
}
you don't show how getting data, [databaseclass getdata];
suggests database table. if re-reading database people2
array order of data in array revert order returned database query.
Comments
Post a Comment