move row inside a UITableView - iphone

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;
}

Related

getting indexpath while reordering cells in tableview?

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!!!

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

Don't allow cell to be moved to another section

How can I disable that the user can move cells to other sections? I don't want to show an alert each time ;)
Thanks :D
Implement the targetIndexPathForMoveFromRowAtIndexPath delegate method:
- (NSIndexPath *)tableView:(UITableView *)tableView
targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath
toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
if (proposedDestinationIndexPath.section != sourceIndexPath.section)
{
//keep cell where it was...
return sourceIndexPath;
}
//ok to move cell to proposed path...
return proposedDestinationIndexPath;
}

UITableView does not go into "swipe to delete" mode

My UITableView is linked with my UIViewController instance. The UIViewController instance is the table's UITableViewDelegate and UITableViewDataSource.
Whenever a cell is swiped in the simulator, it does not trigger the "swipe to delete" mode.
I tried to provide implementations for the methods to support "swipe to delete" and table cell editing. None of them get called (none of the NSLogs are logging).
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"tableView editingStyleForRowAtIndexPath");
return UITableViewCellEditingStyleDelete;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"tableView canEditRowAt...");
return YES;
}
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"tableView willBeginEditingRowAtIndexPath: %#", indexPath);
}
- (void)tableView:(UITableView *)tableView willEndEditingRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"tableView willEndEditingRowAtIndexPath: %#", indexPath);
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle {
NSLog(#"tableView commitEditingStyle");
}
- (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"tableView didEndEditingRowAtIndexPath");
}
There must be a simple thing I'm forgetting, but cannot figure out what. Any ideas?
You've implemented tableView:commitEditingStyle:, but the method you need is tableView:commitEditingStyle:forRowAtIndexPath:.
That's one of the challenges with implementing protocols. If you incorrectly specify or misspell optional methods, there's no way for the compiler to know.
Here are some tips that might help in the future:
Tip 1: Cut and paste protocol method declarations directly from the documentation to avoid spelling errors and missed arguments.
Tip 2: Create sets of correct protocol methods (with mostly empty bodies) and put them in a code snippet library. Then, always use the stored snippet when adopting a protocol.

How to disable the delete button for the particular row when we are using the table view delegate method commitEditingStyle in Iphone sdk?

Here I need a help from ur side that Im using the tableview delegate method commitEditing sytle to get the delete button for every cell when we swipe.But my problem here is I dont want the delete button for the first row in the tableview and swipe should not work and I dnt have any idea how to implemet this.
Anyone's help will be much appreciated.
Thank You,
Monish.
UITableViewCell has delete style by default. To change that you need to implement tableView:editingStyleForRowAtIndexPath: method in your table view delegate. Return UITableViewCellEditingStyleNone for the 1st row and UITableViewCellEditingStyleDelete for other rows.
Edit: Sorry for incomplete answer. You also need to implement tableView:canEditRowAtIndexPath: method (something like that):
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return indexPath.row > 0;
}
Hope that'll work
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(condition check)
return UITableViewCellEditingStyleDelete;
return UITableViewCellEditingStyleNone;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
//here your code
}
}