UITableView IndexPath.row out of scope - iphone

I am implementing the following tableview method:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
The problem is, when this gets hit, indexpath.row is showing up as out of scope. I am trying to handle the delete button for this row so I can delete the cell and delete the value out of the tableview's datasource.

What you are seeing is xCode's debugger being goofy. This happens a bunch. What I do is make a local variable like this and set my breakpoint there and the debugger will act better.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSIndexPath temp = indexPath; //Set Debugger after this and you can inspect the variable.
}

Related

Delegate on tap of - button on UITableViewCell

Is there any delegate that gets called when we tap on the "-" button and delete button comes on the cell on the right side. I want to disable some label on the cell once this button is tapped.
When you swipe on the row of UITableView it will call
(void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
after tapping on the delete button it will call
(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
and finally
(void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath

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;

Swipe to delete

I've looked and i can't seem to find any where on stack overflow some one that has had the same problem as me. So I use the following code:
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete);
}
and when i swipe the delete button appears, but when pressed it doesn't do anything, what have i forgotten to do?
You need to actually delete your data after the if statement. Currently, your if statement does nothing at all, because it just has a semi-colon after it.
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
//Code to delete data goes here.
//This could include removing an object from an array, deleting it from core data,
//and removing the selected row.
}
}

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

Swipe to Delete not working

The swipe to delete functionality is not working in my table view. I have implemented the commitEditingStyle delegate and the Edit button in the navigation bar. Hence when the user clicks the edit button, the delete and add buttons show up appropriately. However, on swiping, the delete button does not appear and it seems like it doesn't recognise the swipe as a call for the setEditing method.
I then implemented willBeginEditingRowAtIndexPath and didEndEditingRwoAtIndexPath delegates as follows:
-(void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"WILL BEGIN EDITING");
[self.tableView setEditing:YES animated:YES];
}
-(void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView setEditing:NO animated:YES];
}
However this does not have any effect either. What could be the possible issue? I have enabled multi-touch for the table view in the IB and each cell has an DetailDisclosureButton accessory.
For swipe-to-delete functionality, you have to implement
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
; // Delete.
}
EDIT: I didn't read the question well enough.
I have noticed that to do a swipe drag in the simulator I have to press and pause for a second while it selects the cell, and only then can I swipe right successfully.
You'll need to implement:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
With these three things you should be good to go. (I missed off the commitEditingStyle and it never worked.
If all other advices doesn't help try to check if you have pan gesture recognizer in your code. It will not work if you have things like this in some underlaying view.
I was doing the following in the tableView:editingStyleForRowAtIndexPath: delegate method :-
if (self.editing == NO || !indexPath)
{
return UITableViewCellEditingStyleNone;
}
This was supposed to return editing style none if no indexPath was selected. For some reason whenever a swipe action was performed, this particular if condition was getting executed. On commenting the above code snippet, the swipe to delete action worked fine.
Thanks all for your help.
The key to make it work is to implement:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
And remember to put it inside "UITableViewDataSource" instead of "UITableViewDelegate"
did you implement the tableView:editingStyleForRowAtIndexPath: method and have it return UITableViewCellEditingStyleDelete
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath delegate method also need to implement to work swipe functionality .