UITableView does not go into "swipe to delete" mode - iphone

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.

Related

UITableViewController: Swipe to delete enabled if condition is satisfied

For UIViewController reuse purpose I want to allow the "Swipe to Delete" gesture only in case that a condition is satisfied. Is there a way to achieve that?
If I add the following UITableViewController Delegate method:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
the "Swipe to delete" is enabled but I'm not able to discriminate in which cases I want to disable this gesture
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
BOOL someCondition = // figure out whether you want swipe to be available
return (someCondition) ?
UITableViewCellEditingStyleDelete : UITableViewCellEditingStyleNone;
}
Comes from the end of this section of my book:
http://www.apeth.com/iOSBook/ch21.html#_deleting_table_items
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (UITableViewCellEditingStyleDelete==YES)
{
// here goes your code
}
}
Place your logic inside of the delegate method
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
For Example
To only allow editing on the odd rows:
-(UITableViewCellEditingStyle)tableView:(UITableView *)aTableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row % 2 == 1)
{
return UITableViewCellEditingStyleDelete;
}
else return UITableViewEditingStyleNone;
}
Percent Sign?!
This is something I love teaching just because it can be so useful in so many circumstances - in the block of code indexPath.row 2 == 1, were checking to see if the row of the indexPath is odd. Here's how it works: The percent sign is called a modulus. What is does is it performs the division much like you would on a piece of paper, then takes the remainder of the calculation - once you understand this, you see how immensely powerful it would be. You can check to see if a number is divisible by another number, for example.
In our example, we're looking at what the remainder is when we divide the row by 2. If it's 0, we know its divisible by two, therefore it's even. If it returns 1, though, the row is odd. It's an amazing tool.

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

issue with swiping a UITableViewCell

I would like to do some stuff when a user swipes to the right of a UITableViewCell, so I use
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
However I tried to slide to the right and this didn't get invoked, why is this? All of my UITableView delegates are invoked.
I also have this in my code:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
}
All I want to do is to add a subview when a swipe happens
Do you have a tableView:commitEditingStyle:forRowAtIndexPath: method?
To quote Apple:
Note: A swipe motion across a cell does not cause the display of a Delete button
unless the table view's data source implements the
tableView:commitEditingStyle:forRowAtIndexPath: method.
And, deleting that method in my project also causes the willBeginEditing not to be called.
// You missed this?
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)theTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
return #"Remove";
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
}
}
Hope that helps.
Try adding this method to your tableView delegate methods:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source.
[self.listTableView beginUpdates];
...
I assume because your other delegate methods are being called that you included in your .h file #interface line? If using IB did you right click on the tableview and wire up delegate and dataSource?
Set the property of table view
tableView.editing = YES;

Custom TableView Cell Indentation Issue

I created a custom UITableViewCell in my app and the default indent when the Delete button is present is not happening, below is a screenshot of my settings. Anyone have any idea as to what I'm doing wrong?
Also, here's a shot of the IB properties:
should happen by default, but in your UITableViewDelegate override
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
make sure you aren't doing anything strange in:
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
and that you are setting the cell's editingAccessoryType to UITableViewCellEditingStyleDelete or UITableViewCellEditingStyleInsert
and that you aren't returning No from :
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

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