My table view is in editing mode allowing my cells to be moved around. Is it possible to also add swipe to delete, NOT using the red-circle-delete button.
I want to be able to show just the move cell image, allowing moving, and also have a swipe to delete.
Is this possible?
This is what I currently have going on.
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 1) {
return UITableViewCellEditingStyleDelete;
}
else
return UITableViewCellEditingStyleNone;
}
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 1)
return YES;
else
return NO;
}
- (BOOL)tableView:(UITableView *)tableview shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
It is definitely possible, though not as simple as you'd probably like. There are three parts to this:
The "swipe" gesture
The UI animation
The delete action
The "swipe" gesture should probably not be a UISwipeGestureRecognizer. UIPanGestureRecognizer will update as the user moves their finger around and matches what the user expects.
The animation should have two parts: update and complete. Update should simply pan the cell view inside of the cell to follow along with the user's finger. Once the gesture has reached a point of "completion" then animate the cell view going off screen.
Lastly, the delete action should be familiar if you've used the UITableViewDelegate methods. If not, here's a link to a helpful answer on the subject.
I would actually implement these out of order. First, programmatically test deleting a row. When that works, add an UIPanGestureRecognizer to call the delete row. Then move the cell view around with the pan gesture. Finally, animate the completion of the cell view moving around.
Related
I have added a tableview inside a scrollview. The tableview returns true for canEditRowAtIndexPath. Issue is, the swipe does not work at once in the tableview. However, didSelectRowAtIndexPath gets called at any given time. When tried to swipe a several times it seems to work. What may be causing the issue? Any solutions to overcome this?
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
I have my table view that displays cells with some content and a custom accessory button.
When i tap a cell, a 5s work is launched. I don't want the user to tap a cell while the background work is not finished, so i do:
self.tableView.allowsSelection = NO;
It works fine, the user can't tap a cell.
I used to do:
self.tableView.userInteractionEnabled = NO;
But i want to scroll my table view while working in background. The issue is that i also can tap on the cell's accessory button.
How to avoid that not losing the table view scroll?
I could do:
- (void)didSelectAccessoryButton:(UIButton *)pButton onEvent:(id)event{
if(self.tableView.allowsSelection){
//Usual accessory button code
}
}
But the accessory button highlight would still be there, meaning at some point i'd need to do in cellForRowAtIndexPath:
[accessoryButton setShowsTouchWhenHighlighted:NO];
I just wish the allowsSelection = NO avoids the tap on accessory button too... So what's the better way ?
The best way i've found is to combine this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//...
tableView.allowsSelection = NO;
[tableView reloadData];
//...
}
with this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//...
accessoryButton.userInteractionEnabled = tableView.allowsSelection;
//or cell.accessoryView.userInteractionEnabled = tableView.allowsSelection;
//
}
And when the job is done, self.tableView.allowsSelection = YES;.
What bothers me with this solution is the need to check inside cellForRowAtIndexPath as to do so i also need to reload data; but in the end it's just 3 lines.
Thank you #virus for the simple "userInteractionEnabled to accessoryButton" idea.
I gave a look on QuizUp and I would like to make a similar menu on my iphone application.
I don't understand how they made the menu. It looks like a table view controller, but a new menu appears under the cell when the user tap on it.
Can someone give me some clues. Is it possible to do that with the native iOS SDK ? How ?
According to the developer of Quiz Up, the app was made using Cocos2D.
That being said, it is also possible to do it using a UITableView. Theres just a couple things you would need to do. The first is get tableView:heightForRowAtIndexPath: to return a different height for the row(s) you want to be expanded. For example, if you only want one row expanded you could use the following.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[tableView indexPathForSelectedRow] isEqual:indexPath]) {
return 100.0f;
}
return 44.0f;
}
Then what you need to do is call beginUpdates and endUpdates on your tableview whenever you want the height to update.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView beginUpdates];
[tableView endUpdates];
}
After that, you just need to layout your cell so that when it's expanded, it shows the additional view and when its collapsed it should hide that view.
I would like to make use of the drag and drop reordering of tableview cells, but I have one problem:
I would like to keep my first row in place!
Here is the canMoveRowAtIndexPath delegate function:
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row==0) {
return NO;
}
return YES;
}
If I do it like this, the first row can not be dragged, BUT you can still drag another row on top of it! Is there any way to prevent cells from dropping 'above' this row?
Your answer is hear,
You can use these step.
Examples of Moving a Row
check if it is a drag and cancel the scrolling if it is = [uitableview setScrollEnabled:NO]
read this thread, it will help you i hope
Tutorial on How to drag and drop item from UITableView to UITableView
I want a superView respond to the slide event, but seems tableViewCell will catch this event first and make the cell enter the editing mode. Anyone has a solution?
I use a UITableViewController as a childViewController, I want the parentViewController to respond to the slide event.
if you do not want the table view cell to respond to the swipe at all then add a swipe gesture to the view.
UISwipeGestureRecognizer * guesture = [[UISwipeGestureRecognizer alloc]initWithTarget: action:#selector()];
then add it to your view.
[self.view addGestureRecogniser:guesture];
[guesture release];
Override tableView delegate method
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return ([tableView isEditing]) ? UITableViewCellEditingStyleDelete : UITableViewCellEditingStyleNone;
}
I've added a transparent UIButton to the UITableViewCell before and it catches the touch event before the UITableViewCell receives the touch event, so it might work for swipes as well. Just add the UISwipeGesture to the button subview to call your method.