UITableViewRowAction code is not executed - cocoa-touch

I'm trying to build a swipeable tablecell. Code of the following URL is working
https://github.com/vanyas/ActionRowTest
But when I implement similar code in my project, It doesn't work. In my code, the following snippet is not working, that's why I can't see the options after swiping.
UITableViewRowAction *actionRed =
[UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal
title:#"Delete"
handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
[weakSelf.tasklist removeObjectAtIndex:indexPath.row];
[weakSelf.tableView setEditing:YES animated:YES];
[weakSelf.tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}];
The whole snippet of my code can be found here.
https://gist.github.com/reza-ryte-club/a3d59b0a3f6c391b831e

Related

iOS - deleting row from UITableView doesn't animate

I have a very frustrating issue. I have an app with a UITableView.
When I am removing a cell from the table view, it is removed from the data model and then I call the following:
-(void)removeItem:(NSIndexPath *)indexPath {
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:#[indexPath]
withRowAnimation:UITableViewRowAnimationRight];
[self.tableView endUpdates];
}
My problem is, I've tried it like I do above, and I've tried without using animateWithDuration. I've even tried with a CATransaction, but however I do it, the animation doesn't happen.
I've got slow animations on in my Simulator and when I remove an item from the list, it removes correctly, but without animation.
It just disappears and leaves a blank space for a moment before the table view data is reloaded.
I've search all over SO and Google and I can't seem to find an answer.
Any ideas?
Does it perhaps have to do with the fact that I'm removing the object from the data model before calling the function above?
Edit: Removed the Animation Block as it is incorrect
According to THIS you don't need to use the animateWithDuration:animations: at all.
Just try it like this
-(void)removeItem:(NSIndexPath *)indexPath {
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:#[indexPath]
withRowAnimation:UITableViewRowAnimationRight];
[self.tableView endUpdates];
}
Okay, I don't know if it's just bad manners, but I feel I'm going to answer my own question here.
First, thanks for all the other answers, you were all correct of course, but none of the answers solved my problem.
It turns out that there is another area in the code that does a different check then calls one of the tableView delegate methods, that seems to cancel the animation.
So the answer is as follows:
When you're row is removing but the animations aren't working, make sure that you are not calling didSelectRowAtIndexPath:indexPath before the animation starts. This will cancel the animations.
If you're NOT having that problem, here's some really typical code for expanding, two lines in the example:
Note that facebookRowsExpanded is a class variable you must have:
if ( [theCommand isEqualToString:#"fbexpander"] )
{
NSLog(#"expander button......");
[tableView deselectRowAtIndexPath:indexPath animated:NO];
NSArray *deleteIndexPaths;
NSArray *insertIndexPaths;
facebookRowsExpanded = !facebookRowsExpanded;
// you must do that BEFORE, not AFTER the animation:
if ( !facebookRowsExpanded ) // ie, it was just true, is now false
{
deleteIndexPaths = [NSArray arrayWithObjects:
[NSIndexPath indexPathForRow:2 inSection:0],
[NSIndexPath indexPathForRow:3 inSection:0],
nil];
[tableView beginUpdates];
[tableView
deleteRowsAtIndexPaths:deleteIndexPaths
withRowAnimation: UITableViewRowAnimationMiddle];
[tableView endUpdates];
}
else
{
insertIndexPaths = [NSArray arrayWithObjects:
[NSIndexPath indexPathForRow:2 inSection:0],
[NSIndexPath indexPathForRow:3 inSection:0],
nil];
[tableView beginUpdates];
[tableView
insertRowsAtIndexPaths:insertIndexPaths
withRowAnimation: UITableViewRowAnimationMiddle];
[tableView endUpdates];
}
// DO NOT do this at the end: [_superTableView reloadData];
return;
}
NOTE: your code for numberOfRowsInSection must use facebookRowsExpanded
(it will be something like "if facebookRowsExpanded return 7, else return 5")
NOTE: your code for cellForRowAtIndexPath must use facebookRowsExpanded.
(it has to return the correct row, depending on whether or not you are expanded.)
First, you don't need to animate the change yourself.
Second, I think you need to make the changes to the datasource between the begin and end updates.
Your method should look something like this:
-(void)removeItemAtIndexPath:(NSIndexPath *)indexPath{
[self.tableView beginUpdates];
// I am assuming that you're just using a plain NSMutableArray to drive the data on the table.
// Delete the object from the datasource.
[self.dataArray removeObjectAtIndex:indexPath.row];
// Tell the table what has changed.
[self.tableView deleteRowsAtIndexPaths:#[indexPath]
withRowAnimation:UITableViewRowAnimationRight];
[self.tableView endUpdates];
}
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
indexPaths is an array of NSIndexPaths to be inserted or to be deleted in your table.
NSarray *indexPaths = [[NSArray alloc] initWithObjects:
[NSIndexPath indexPathForRow:0 inSection:0],
[NSIndexPath indexPathForRow:1 inSection:0],
[NSIndexPath indexPathForRow:2 inSection:0],
nil];

reloadSections doesn't work inside an if

- (void)viewWillAppear:(BOOL)animated
{
if (_needRefresh)
{ NSLog(#"Type This If You Are IN");
[self.myTableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
_needRefresh = NO;
}
I want to refresh my table only if something is changed. the code [self.myTableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade]; works great when I move to the viewController but when I put it inside if something is going wrong. I can see that the NSLog from inside the if is printed so I don't understand what's the difference and why the reloading is not getting executed.

Select uitableview row programmatically

i have read a lot on this argument, i have tested this example that works:
source code example. but this doesn't use storyboards (i don't know if this is the reason that make my project not working)
i have made another project using storyboard, the same sequence of instruction doesn't work...
-(void)viewDidAppear:(BOOL)animated{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
[firstController.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
[firstTable.delegate tableView:firstTable didSelectRowAtIndexPath:indexPath];
}
here is my project, is the same of the previous link, only adding viewDidAppear
my not working project
can someone tell me what's wrong?
thanks
I tried to study your code downloading the project.The problem is that firstController.tableView is nil.
So fix it:
[firstTable selectRowAtIndexPath:indexPath
animated:NO
scrollPosition:UITableViewScrollPositionNone];
Or assign firstController.tableView to the table view.
- (void)viewDidLoad {
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
}
If you want the callback from the delegate:
if ([self.tableView.delegate respondsToSelector:#selector(tableView:didSelectRowAtIndexPath:)]) {
[self.tableView.delegate tableView:self.tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
}

UITableView reorder like Clear app

How to start moving after the long tap detect? long tap detect with uilongpressgesturerecognizer.
My code:
`- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
flMoveRow = NO;
[self setEditing:YES animated:YES];
[listView reloadData];
}
- (void)longTapGesture:(UILongPressGestureRecognizer *)sender{
if((sender.state != UIGestureRecognizerStateEnded)&&(!flMoveRow)){
NSLog(#"==longTapGesture:");
flMoveRow = YES;
[listView beginUpdates];
NSArray *indexPaths = [[NSArray alloc] initWithObjects:indexPath, nil];
[listView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
[listView endUpdates];
sender.enabled = NO;
return;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(#"==canMoveRowAtIndexPath:%i -- %d", indexPath.row, (int)flMoveRow);
return flMoveRow;}`
break touch. Thanks.
There is a control available on cocoacontrols.com which mimics the Clear app. It is still being worked on apparently but when I downloaded it, it was still very impressive. You could either incorporate it fully in your app or use it as a starting point in your own research.
link.

didSelectRowAtIndexPath event not triggered when select row programmatically

I would like to select a cell in a tableView. It is working properly using this code, but once selected programmatically it does not trigger a didSelectRowAtIndexPath event. How can I trigger it?
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.mainTable selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
It is working as documented:
Calling this method does not cause the delegate to receive a tableView:willSelectRowAtIndexPath: or tableView:didSelectRowAtIndexPath: message, nor will it send UITableViewSelectionDidChangeNotification notifications to observers.
Call it yourself after selecting your cell:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self tableView:self.mainTable willSelectRowAtIndexPath:indexPath];
[self.mainTable selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
[self tableView:self.mainTable didSelectRowAtIndexPath:indexPath];