getting indexpath while reordering cells in tableview? - iphone

I want change the cells order in my tableview
I am using following code
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
[tableView moveRowAtIndexPath:path1 toIndexPath:path2];
}
here path1 indexpath of row which user selected now I want find the indexpath of the row the user drop that cell so how can I find that toIndexPath(path2).

I think you're confused with the purpose of this method - this is a callback function of the UITableViewDataSource that's designed to notify the data model of changes in the table. From the docs:
Tells the data source to move a row at a specific location in the table view to another location.
The UITableView object sends this message to the data source when the user presses the reorder control in fromRow.
This means that what you need to do here is to make sure your model (Usually an array) is rearranged to sync properly with these changes.
Hope this gives you a good direction.

You should implement:
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
& also:
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
//do whatever you want after the row has been moved
}
Here is an example of drag N drop:
https://github.com/coderDove/UITableView-Drag-n-Drop
Hope this Helps!!!

Related

How to move a tablview row?

I am making an app in which I want to move the row.So I can do it by
[self.tableList setEditing:YES animated:YES];
where "tableList" is my table's name but whenever I am running this line.I am not able to get the icon for move which usually comes at right side for moving a cell instead I am getting the icon to delete a row which comes on left side of a cell.
Please help me to get my cell move and ask if you need any clarification of my question..
Thanks.
You need to implement the below two methods
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
//do code according to your requirements of app
//if you do not write ant thing in the method then also your row will be moved.
NSString *stringToMove = [[self.dataArray objectAtIndex:sourceIndexPath.row] retain];
[self.dataArray removeObjectAtIndex:sourceIndexPath.row];
[self.dataArray insertObject:stringToMove atIndex:destinationIndexPath.row];
[stringToMove release];
[tableView reloadData];
}
use this link of aple and scrol down to apple how to move or scrol rows. cheerz :)
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html
– beginUpdates
– endUpdates
– insertRowsAtIndexPaths:withRowAnimation:
– deleteRowsAtIndexPaths:withRowAnimation:
– moveRowAtIndexPath:toIndexPath:
– insertSections:withRowAnimation:
– deleteSections:withRowAnimation:
– moveSection:toSection:
– scrollToRowAtIndexPath:atScrollPosition:animated:
– scrollToNearestSelectedRowAtScrollPosition:animated:
use these two method for row move
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath*)indexPath
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

What is the meaning of this sign?

I am new for iphone developing. I get code from any where but i can nit understand what is use of this sign (=)which is shown in below image.
It means that you can move the row up or down in the table.
If you want to respond to those actions, take a look at your UITableViewDataSource. The section on "Reordering Table Rows" is the relevant part.
This sign show that u can move row up and down for this in UItable Class u will get function
which is define below.
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
//write your code
}
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}

UITableView, Catching touches on delete?

I wonder if someone would be so kind as to help me out with a query I have regarding deleting rows from a UITableView. I have implemented tableView:commitEditingStyle:forRowAtIndexPath: and now get the [DELETE] button appearing when I swipe on a cell, my question is which method do I need to use to catch a user pressing that button (i.e. [DELETE]).
Also when I do delete a row, can I delete the row from my dataSource and refresh the table. OR should I delete the row from the dataSource and then call some other method on the tableView?
This is what I was after, the callback for the swipe and the callback when the user selects [DELETE].
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Swipe ...");
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Delete Pressed.");
}
Delete from the data source and call reloadData on the table. Or do you have like a million rows in the table?

move row inside a UITableView

I have a UITableView that contain 3 sections, It is possible in the edit mode to change the row position just inside his section and not to another section?Actually I can move the cell to any section.
Thanks
Many correct answers but no one presented a complete and well presented reply.
Use the table view delegate method tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath: as shown below.
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
if (proposedDestinationIndexPath.section != sourceIndexPath.section)
{
return sourceIndexPath;
}
return proposedDestinationIndexPath;
}
Do not forget to do another check in your tableView:moveRowAtIndexPath:toIndexPath: data source method as shown below, you do not want to run your application logic when the destination is similar to the source.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
if(sourceIndexPath == destinationIndexPath)
{
return;
}
//application logic
}
If I understand your question correctly, then yes: use
- (NSIndexPath*) tableView: (UITableView*) tableView targetIndexPathForMoveFromRowAtIndexPath: (NSIndexPath*) sourceIndexPath toProposedIndexPath: (NSIndexPath*) proposedDestinationIndexPath
like Steven and Narayanan said, you have to implement the method
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
of your UITableView delegate.
In this method, you check the sourceIndexPath.section:
If it's the section you allow to be reordered, you return the proposedDestinationIndexPath.
If not, you return the sourceIndexPath so your tableView will not move the row.
I didn't write the code so you'll have the pleasure to practice instead of copy'n'paste ;)
(edit: adding list style for the options)
In
moveRowAtIndexPath:toIndexPath:
add this check:
if ( fromIndexPath.section != toIndexPath.section ) return; // prevent moving to another section.
Try this
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
if( fromIndexPath == toIndexPath ) {
return;
}

How to show the reorder controls in UITableView with delete buttons

I read the documentation about how to manage deletion and reordering of rows in a UITableView. I created the edit button and I'm able to delete rows. I would like the user to be able to reorder the rows as well. It seems simple, but I can't understand how to tell the cells that they can be moved.
To tell the rows they can be deleted I use the editingStyleForRowAtIndexPath, but how do I tell the cell it can also be moved and where do I set the showsReorderControl? I tried to place in cellForRowAtIndexPath, but nothing is shown.
Thanks!
You have to say that rows can be moved:
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
and implement this delegate to update your data source:
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
See Managing the Reordering of Rows of Table View Programming Guide for iOS
In my case I have implemented all the required UITableViewDelegate methods as mentioned in the Apple document and in the answers here, but still cannot see the reorder control. Eventually I found out it's because I overrode the layoutSubviews method without calling the super's default implementation. After I added the [super layoutSubviews], my reorder control finally appears.
The reason why we need to call [super layoutSubviews] is because when we toggle the table's editing property it would call the cell's layoutSubviews method, and the system provided controls such as the reorder control is displayed within UITableViewCell's default layoutSubviews method. Once you realize this you can also modify your layoutSubviews implementation to change the appearance of your cell depending on whether it is being edited or not to make it less clumsy when the reorder control appears.
So here is a checklist for the row reordering:
make sure the delegate methods tableView:canMoveRowAtIndexPath and tableView:moveRowAtIndexPath:toIndexPath are implemented
make sure the tableView's editing property is set to YES
If you have a custom UITableViewCell, make sure you call
[super layoutSubviews] if you override this method
Adding to #benoit answer above. If your model happens to be a mutable array, something like this would suffice for the tableView:moveRowAtIndexPath:toIndexPath:
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
id objectToMove = [_objects objectAtIndex:fromIndexPath.row];
[_objects removeObjectAtIndex:fromIndexPath.row];
[_objects insertObject:objectToMove atIndex:toIndexPath.row];
[tableView reloadData];
[self saveObjects]; // A method of your own to make new positions persistent
}
try this . . .this will handle arranging and updating of cell in case of simple tableview
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
[tableData insertObject: [tableData objectAtIndex:sourceIndexPath.row] atIndex:destinationIndexPath.row];
[tableData removeObjectAtIndex:(sourceIndexPath.row + 1)];
}