ios - Reordering tableview cells -
i trying re order cells based on priority (an enum). have double checked enum values correct. here enum
enum priority{ case low case neutral case high case urgent } as can see, lower priority has lower hash values. trying compare 2 cells; cell , cell above it. if cell's value greater 1 above it, shift upwards. obtain indexpath of cell above using
var newindexpath = indexpath(row: indexpath.row - 1 , section: indexpath.section) after comparing enum values using
if(comparepriority.hashvalue < priorityvalue.hashvalue) i attempt switch order using
tableview.moverow(at: indexpath, to: newindexpath) the result interesting, use pictures explain. note of code in cellforrowat function , cells given priority correspond name except "test", these pictures taken in slow motion meaning user wouldn't able see switch happening.
just sort array of items according these priorities , table reload.
in way index 0 first item etc. , you'll simple code that's not confusing on fly sorting/comparing on place.
sorting (after you've made enum of type int , renamed priorityvalue priority suggest do):
enum priority: int { case low = 0 case neutral = 1 case high = 2 case urgent = 3 } items.sorted(by: { $0.priority < $1.priority }) 

Comments
Post a Comment