ios - Getting the order of reordered UITableViewCells -


i'm trying reordered index of cells array.

in viewdidload

cell.showsreordercontrol = yes; arraytag = [nsmutablearray array]; 

//saving indexpath.row cell.tag while creating cell.

- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath   {    .....     cell.tag = indexpath.row;    nsstring *strcelltag = [nsstring stringwithformat:@"%d",cell.tag];    if(![arraytag containsobject:strcelltag])    {       [arraytag addobject:strcelltag];    }    return cell;  } 

//i'm not sure if right way, below i'm trying save changes array.

- (nsindexpath *)tableview:(uitableview *)tableview targetindexpathformovefromrowatindexpath:(nsindexpath *)sourceindexpath toproposedindexpath:(nsindexpath *)proposeddestinationindexpath {     [arraytag replaceobjectatindex:sourceindexpath.row withobject:[nsstring stringwithformat:@"%i",proposeddestinationindexpath.row]];     [arraytag replaceobjectatindex:proposeddestinationindexpath.row withobject:[nsstring stringwithformat:@"%i",sourceindexpath.row]];      return proposeddestinationindexpath; } 

to verify nslog:

- (ibaction)donebutton:(id)sender {     nslog (@"number of objects in array %i", arraytag.count);      (nsstring *obj in arraytag){         nslog(@"from arraytag obj: %@", obj);     }    } 

nslog without moving cells results expected.

2013-07-10 17:50:47.291 myapp[1634:c07] number of objects in array 7 2013-07-10 17:50:47.292 myapp[1634:c07] arraytag obj: 0 2013-07-10 17:50:47.292 myapp[1634:c07] arraytag obj: 1 2013-07-10 17:50:47.292 myapp[1634:c07] arraytag obj: 2 2013-07-10 17:50:47.292 myapp[1634:c07] arraytag obj: 3 2013-07-10 17:50:47.293 myapp[1634:c07] arraytag obj: 4 2013-07-10 17:50:47.293 myapp[1634:c07] arraytag obj: 5 2013-07-10 17:50:47.293 myapp[1634:c07] arraytag obj: 6 

nslog after moving/shuffling cells appears wrong. i'm not getting unique values.

2013-07-10 17:51:55.329 myapp[1634:c07] number of objects in array 7 2013-07-10 17:51:55.330 myapp[1634:c07] arraytag obj: 4 2013-07-10 17:51:55.330 myapp[1634:c07] arraytag obj: 5 2013-07-10 17:51:55.330 myapp[1634:c07] arraytag obj: 5 2013-07-10 17:51:55.330 myapp[1634:c07] arraytag obj: 5 2013-07-10 17:51:55.331 myapp[1634:c07] arraytag obj: 6 2013-07-10 17:51:55.331 myapp[1634:c07] arraytag obj: 6 2013-07-10 17:51:55.331 myapp[1634:c07] arraytag obj: 4 

q: why not getting new unique values after reordering? how new order of cells saved in array?

delegate , datasource clarification

you shouldn't modify data behind tableview during:

-tableview:targetindexpathformovefromrowatindexpath:toproposedindexpath: 

this method gets called when user hovering cell above others, , thus, causes multiple calls function while cell being dragged.

the key here that, method part of uitableviewdelegate, it's not meant modify data, change appearence of tableview, , it's there "sliding" animation when cell hovering. returned value determines cell should animated to.

what should do, perform changes when user has commited them, happens when call uitableviewdatasource protocol method:

-tableview:moverowatindexpath:toindexpath: 

this when user has released cell , it's on place, , occurs once per drag & drop.

as general rule of thumb, whatever on uitableviewdelegate shows, , what's on uitableviewdatasource real data.

modifying model

swapping

swapping or exchanging, easiest case ([a, b, c, d] => [d, b, c, a]), , there's convenient method that:

[arraytag exchangeobjectatindex:fromindexpath.row withobjectatindex:toindexpath.row]; 

swift

let f = fromindexpath.row, t = toindexpath.row (array[f], array[t]) = (array[t], array[f]) 

insertion

insertion little more tricky , verbose ([a, b, c, d] => [b, c, d, a]). important thing making changes don't affect each other.

id obj = [arraytag objectatindex:fromindexpath.row]; [arraytag removeobjectatindex:fromindexpath.row]; [arraytag insertobject:obj atindex:toindexpath.row]; 

swift

let obj = array.removeatindex(fromindexpath.row) array.insert(newelement: obj, atindex: toindexpath.row) 

Comments