Swipe to delete cell with strange behavior - iphone

I have an application where the main root is a UIViewController with a similar behavior like the application of facebook or path, that is, swipe and show another view, on the left or right.
However, my problem is that sometimes the main UIViewController has UITableViewcells, which should be eliminated with swipe, yet this does not work correctly, sometimes it detects the gesture sometimes not.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[dataItems removeObjectAtIndex:indexPath.row];
[self.tableView reloadData];
}
}
I am adopting this library
enter link description here
to make the controller of the UIViewController's
Any idea how to solve this?
Thanks in advance.

The problem is the gesture you're using. If both the table cells and view are trying to intercept swipe events you are guaranteed to not get the behavior you're looking for. The way that I usually solve this problem is to make a button that changes the table into editing mode. That way you don't have a conflict.
[tableView setEditing:YES animated:YES];
The problem isn't program design or language capabilities, it's having the EXACT SAME gesture do two DIFFERENT things. This isn't physically possible to implement and always get what you want. iOS devices can't read minds, although I heard about a new API in iOS 7...
You could try using multi-finger gestures.

Related

Enabling both multi-edit and single-edit styles in a TableView

In the mail app, you can swipe to delete a message. You can also hit edit, and select multiple messages, and delete them.
I want to be able to do the same in my table views.
I already had swipe to delete, by implementing the required delegate method:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// squish it.
}
}
And then I found about this great new property apple introduced in iOS 5, tableView.allowsMultipleSelectionDuringEditing. So, in my viewDidLoad:
if ([self.tableView respondsToSelector:#selector(setAllowsMultipleSelectionDuringEditing:)]) {
self.tableView.allowsMultipleSelectionDuringEditing = YES;
// set up a little toolbar to commit edits
}
And it worked, and that was fun... until I tried to swipe to delete again.
For some reason, the multi-edit functionality was preventing the standard swipe to delete style edit. I want both. Anyone know how to get both working?
Clearly you can just set allowsMultipleSelectionDuringEditing to YES right before you enable editing mode on the tableview. Do this inside setEditing:animated:
Edit: Ah, it also looks like this has been answered already: How do I get swipe-to-delete working when tableView's allowsMultipleSelectionDuringEditing property is YES?

is there any if statement for showing/hiding a method in objC?

i use same uiviewcontroller's instance in different tabs.
there is a uitableview in viewcontroller.
in firstviewController instance, i dont wanna edit the uitableview.
in the second one i use edit mode for tableview.
thats why i want to show or hide this method:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
is it possible to make an if statement like this:
#if (editingOK)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
....some codes
}
#endif
editing OK is a BOOL property.
if you ask why i want it, because if the user swipes on the cell, it display Delete button.
i just want it if my editingOK=YES.
The #if/#endif syntax is used for conditional compilation: it lets you modify your program at compile time based on build configuration. Read about the "C preprocessor" to learn more.
If you are, as you say, using the same object instance as the delegate of different UITableViews, you must have some way to determine which table you are dealing with.
What you need to do is implement an additional method:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
That method is called when the user swipes the cell, and you can decide if a delete button should appear or not, then return the appropriate UITableViewCellEditingStyle constant.
Isn't editability controlled by calling the setEditing method of the UITableViewController? So you could set that depending on whether or not you want to enable editing, w/o this #ifdef ugliness.

Dynamically add and remove UITableViewCells to UITableView

I'm building an app where a user is able to provide different usernames that he/she has. The vision is, the user is able to add and remove UITableViewCell to enter a username.
Right now I have a grouped UITableView and on the right hand side of every UITableViewCell I have a UIButton that adds another cell to the table with a UITextField. After the first cell, every cell has a delete button. I'm trying to make the UIButton delete that row. I have the IBAction that removes the cell, the only problem is, it's not deleting the proper row.
What is the best way to do what I'm attempting to do? I don't know how to properly search for this on Google. I'm sure someone has done what I'm trying to do.
Thanks for any help in advance!
Similar to what Derek said above -- UITableViewController already provides functionality to delete rows.
To toggle editing a UITableView, do something like: [self.tableView setEditing:!self.tableView.editing animated:YES];
Override tableView:canEditRowAtIndexPath: with something like (since it sounds like you don't want your first row to be deletable):
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
if ([indexPath row] == 0) {
return NO;
}
return YES;
}
Also override tableView:commitEditingStyle:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.dataArray removeObjectAtIndex:[indexPath row] - 1];
// delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
}
}
Hope this helps!
It sounds like you are trying to code what a table view already has. Look into the built in editing facilities of a table view and you will find thaaat you don't need to have these unbuttons as table views and delegates have editwing facilities built in.
Your comment about deleting the wrong thing makes me think you have an issue with matching row numbers to indexes of your source data.

Deleting custom cell in Table View

I am using a Table View Cell to draw custom cell in table view. Now the problem is that when I try to delete the cell in editing style UITableViewCellEditingStyleDelete only the
the little red -ve sign bitton appear and when I click it nothing happen.
Please help me and tell me how to delete custom cell in table view
Thanks
Gyani its not at all possible that when you click on (-) sign nothing happens.
May be your tables user interaction is disabled. Try enabling tableView.userInteractionEnabled=YES
And still it does not work then post some code.
hAPPY cODING...
did you implement the appropriate delegate methods? Specifically
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
Did you implement this delegate method by following form?
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// remove the cell from UITableView
[tableView deleteRowsAtIndexPaths:indexPath withRowAnimation:YES];
// remove the data from self array or something else.
...
}
}
Do you have a subview in the cell that stops the user interaction? maybe located in negative x, so it is over the delete button.

iPhone Can't deselect a UITableViewCell

I have a RootViewController class which is inherited from UITableViewController.
When a cell is deselected by the user I want to enable/disable certain buttons on the toolbar.
How do I trap the deselect event?
-(void)tableView:(UITableView *)tableView deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated
{
if(indexPath.row <= rowNumber)
{
[viewButtton setEnabled:NO];
[editButtton setEnabled:NO];
}
}
I tried using this method but it doesn't seem to execute at all. Any ideas how cam this be done?
I do not think there is a deselectRowAtIndexPath event, there is a method that you can call to deselect the indexPath, but looking at the SDK I do not see an event for this in the UITableViewDelegate: http://developer.apple.com/iPhone/library/documentation/UIKit/Reference/UITableViewDelegate_Protocol/Reference/Reference.html.
Could you enable/disable certain buttons on the toolbar during the didSelectRowAtIndexPath: event?
-Rog
This is in the current beta SDK only which means it could be buggy / changed / unsupported...
I did noticed that your method declaration doesn't match the SDK (at least, the version I have).
Try removing animated:(BOOL)animated; I don't think it's applicable here.
See line ~345 in UITableView.h, and/or right click on didDeselectRowAtIndexPath and "Jump to Definition", where you'll probably find how the delegate method should be defined.
That said, if your goal is simply to "enable/disable certain buttons when a cell is selected",
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;
should work just fine. This will occur after they select the cell and before it's deselected. 'deselect' has to do more with animation than user interaction. The only reason I can think you would want to use deselect is maybe the aesthetic value of ensuring your event only occurs after the select cell is no no longer highlighted.