ios - How to achieve UITableView row animation similar to Mail -
in mail, if manually refresh mailbox via pull refresh, if don't have new emails, view slides hide spinner appearance of existing cells don't alter - don't animate @ all. if did have new emails, animate in top. if had no emails listed before refresh, cells animate in quite nicely. also, if refresh , existing email had been deleted, disappearance animated none of other cells animate besides entire table moving take place of removed cell. accomplish same behavior in app.
currently, when info fetched first time, appears in table without animation. if info deleted , user manually refreshes, entire table instantly updated there no animation of cell disappearing, it's instantly replaced row underneath it. same behavior occurs if cells displayed , refreshing results in new info beingness added table - instantly appears.
how can implement animation appearance , disappearance of cells when reloaddata
? not animate existing cells if don't change.
my setup fetch data, parse json info construction (array or dictionary), phone call reloaddata
, , cellforrowatindexpath
gets info structure. when user refreshes, performs same steps again.
my attempts far haven't been successful. tried instead of calling reloaddata
, phone call [self.tableview reloadsections:[nsindexset indexsetwithindex:0] withrowanimation:uitableviewrowanimationtop];
animates first section , animates it, if none of info has changed. other effort animates it:
[self.tableview beginupdates]; [self.tableview deletesections:[nsindexset indexsetwithindex:0] withrowanimation:yes]; [self.tableview insertsections:[nsindexset indexsetwithindex:0] withrowanimation:yes]; [self.tableview endupdates];
when inserting rows, maintain index paths beingness inserted in array, then:
[self.tableview beginupdates]; [self.tableview insertrowsatindexpaths:arrayofinsertedindexpaths withrowanimation: uitableviewrowanimationtop]; [self.tableview endupdates];
i haven't confirmed directly, i'm pretty sure behave wish when insertions made.
edit - row-accurate animation going require row-accurate knowledge alter in model. how index paths beingness inserted depends on how you're getting data. in toughest scenario, you're getting new collection , need work out has changed. model objects need implement equivalence** can conclude different instances should treated same.
** in nsstring isequaltostring:
equivalence test, not equality test it's name might suggest.
we're comparing objects themselves, not row indexes, knowing deletions hard, no harder knowing insertions. example, here's category created nsmutablearray handles insertions (it's ideal email or other timeline sort of app that's expecting new stuff, not deletions).
#import "nsmutablearray+inserts.h" @implementation nsmutablearray (nsmutablearray_inserts) // insert elements of array receiver, sorting on key // reply array of index paths insertions happened - (nsarray *)insertfromarray:(nsarray *)array sortedon:(nsstring *)key ascending:(bool)ascending { // first insert array (preserving uniqueness according shallow equality) (id newelement in array) { nsinteger position = [self indexofobject:newelement]; if (position == nsnotfound) [self addobject:newelement]; } // sort using key [self sortusingdescriptors:[nsarray arraywithobject:[nssortdescriptor sortdescriptorwithkey:key ascending:ascending]]]; // find insertions using deep equality nsmutablearray *answer = [nsmutablearray array]; (id newelement in array) { nsinteger position = [self indexofobject:newelement]; id element = [self objectatindex:position]; if (element == newelement) { [answer addobject:[nsindexpath indexpathforrow:position insection:0]]; } } homecoming [nsarray arraywitharray:answer]; }
a couple of notes: 1) lazily hardcoded index path section 0. if model has multiple sections, you'll want phone call each , add together section parameter index paths back, 2) sorted insert feature of pertinent particular app, might not need it, , can remove 2 params , delete self sortusing... line. 3) 1 way business relationship deletions add together analogous method answers index paths of elements in receiver not in passed array.
ios objective-c uitableview animation
No comments:
Post a Comment