Maybe I´m just stupid but I cant understand why this isnt working.
I want to achieve a little animation when I'm entering editing mode within a UITableView.
[super setEditing:NO animated:YES];
[myTable setEditing:NO animated:YES];
[myTable reloadData];
[self.navigationItem.leftBarButtonItem setTitle:#"Edit"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStylePlain];
Shouldnt this animated:YES suppose to animated this entering of the editmode?
Regards.
- f0rz
Solution, I was reloading the tableview . This was making the animation to stop.
Removed [myTable reloadData] and it worked again!
Related
I've noticed that scrollToRowAtIndexPath:atScrollPosition:animated: doesn't scroll to cell that are not currently in view, so If I have 100 cells and I need to get to the one at 70, the call to that selector will do nothing.
Is there a way I can get that cell into memory? I already have the cell's index path...
I need to scroll to that position in my app when the user would want to go there.
Thanks for any thoughts!
EDIT: #dasblinkenlight
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillHide
{
//Load remote cell here then scroll
// :( dont know how to load remote cell yet
}
- (void)keyboardWillShow
{
//Load remote cell here then scroll
// :( dont know how to load remote cell yet
//_cellIndexPath gets assigned on didSelectRowAtIndexPath:
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:_cellIndexPath.row inSection:_cellIndexPath.section] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
EDIT2:
- (void)keyboardWillShow
{
//Load remote cell here then scroll
[NSThread detachNewThreadSelector:#selector(keyboardWillShowThreaded) toTarget:self withObject:nil];
}
- (void)keyboardWillShowThreaded
{
[NSThread sleepForTimeInterval:2.0];
[self performSelectorOnMainThread:#selector(keyboardWillShowMainThread) withObject:nil waitUntilDone:YES];
}
- (void)keyboardWillShowMainThread
{
//Get the cell
//_textFieldThatHasFirstResponder is a subview in the cell
//This returns null, probably because the cell is not loaded into memory
UITableViewCell *cell = [_textFieldThatHasFirstResponder superview];
NSLog(#"CELL TO SCROLL TO: %#",cell);
NSIndexPath *indexPathForCell = [self.tableView indexPathForCell:cell];
[self.tableView scrollToRowAtIndexPath:indexPathForCell atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}
OK, I've got the cause of this, see, you have:
NSIndexPath *indexPathForCell = [self.tableView indexPathForCell:cell]; // nil here
[self.tableView scrollToRowAtIndexPath:indexPathForCell atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
When you send indexPathForCell: for an out-of-view cell it returns nil, so tableView doesn't know where to scroll to.
you can implement a delegate so that you can call it from the class where you are in, so that is can update the position
"I've noticed that scrollToRowAtIndexPath:atScrollPosition:animated:
doesn't scroll to cell that are not currently in view, so If I have
100 cells and I need to get to the one at 70, the call to that
selector will do nothing. "
No. it is not true. I have just tried with a tableview with 100 rows. The following code works well.
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:78 inSection:0]
atScrollPosition:UITableViewScrollPositionNone animated:YES];
I don't think adding sleep is changing anything. It just delays execution but does not affect the order. Can you check if the index your are passing to scrollToRowAtIndexPath is valid? I remember seeing the same problem myself but it was related to invisible cell. It was impossible to retrieve invisible cell (tableView returned nil) and therefore its index path was nil and thus scrolling failed.
You could store locations of all cells or compute it on the fly and then pass it to
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated;
It's the only solution I can imagine.
This is really simple, though still driving my nuts. I have a uitableview where I am trying to animate transition in and out of editing mode. This is what I took from an example that I have seen. It does do the job, but without the animation.
Any thoughts?
- (IBAction) EditTable:(id)sender
{
if(self.editing)
{
[super setEditing:NO animated:YES];
[tblSimpleTable setEditing:NO animated:YES];
[tblSimpleTable reloadData];
[self.navigationItem.leftBarButtonItem setTitle:#"Edit"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStylePlain];
}
else
{
[super setEditing:YES animated:YES];
[tblSimpleTable setEditing:YES animated:YES];
[tblSimpleTable reloadData];
[self.navigationItem.leftBarButtonItem setTitle:#"Done"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStyleDone];
}
}
PS: I am also not sure why I need this line: [super setEditing:NO animated:YES]; but things just dont seem to work at all without it. I just saw a few examples online that dont do that.
Thanks!
Maybe you should not reloadData when set editing property.
BTW, What's your "super" class? Normally you don't have to invoke [super setEditing:YES animated:YES];
Is it only the button that isn't animating properly? Either way you should probably be using super.editButtonItem instead of your own; it's animated and just setting the text and style like that (I believe) isn't. As far as calling the super, are you overriding one of the editing methods and not calling the super method from within there? And xuzhes's answer about the reloadData is, I believe, correct as well.
Try this:
#Implementation YourViewController // This can (should) be a subclass of UITableViewController to make your life easier
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem; // Automatically calls setEditing:animated: and changes itself to "Edit"/"Done" between presses
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
if (editing == YES) {
// Do stuff here
} else {
// Do stuff here
}
// Reload all sections of the table view
// THIS IS THE PART YOU'RE INTERESTED IN
NSRange range = NSMakeRange(0,[self.tableView numberOfSections]);
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:range];
[self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
}
Check out the documentation for comments on the methods from Apple :)
How can I tell when [UITableView setEditing:YES animated:YES] has completed?
I don't want to give any context, because I want to avoid anybody giving me workarounds, which do not interest me.
What I want is to call the above, then have a separate function called when the animation is completed.
I am edited the post to give context and some workarounds.
Originally I setEditing and immediately reload the table data.
[tableView setEditing:YES animated:YES];
[tableView reloadData];
The problem is that the table reloads before the animation begins, and so the animation is never seen.
Here are some various workarounds:
[tableView setEditing:YES animated:YES];
[self performSelector:#selector(ReloadTable) withObject:nil afterDelay:1.0];
This works but if I get the delay incorrect then it will look bad. So I need to know what the delay is, which I can figure out, but we are not gauranteed that the delay will always be the same.
isEditing = YES;
[tableView reloadData];
[tableView setEditing:YES animated:YES];
This could work, but the table behaves differently depending on if we are in editing mode. So I have to use my own isEditing variable instead of the standard UITableView.editing. I would rather not have to create a new boolean isEditing variable.
[tableView setEditing:YES animated:YES];
[tableView insertRowsAtIndexPaths:path withRowAnimation:UITableViewRowAnimationTop];
This almost works well but in editing mode the first row should have the UITableViewCellEditingStyleInsert, while the other rows get UITableViewCellEditingStyleDelete. And with the above code the editing style gets set BEFORE the row is added. Therefore the second row ends up with UITableViewCellEditingStyleInsert.
[CATransaction begin];
[CATransaction setCompletionBlock: ^{
// your animation has finished
}];
[tableView setEditing:YES animated:YES];
[CATransaction commit];
Note that setCompletionBlock must be on the top.
In iOS 4 you can do the following:
[UIView animateWithDuration:0.3f
animations:^{
[self.tableView setEditing:YES animated:NO];
}
completion:^(BOOL finished){
// Do something
}
];
Swift 4 version of accepted answer:
CATransaction.begin()
CATransaction.setCompletionBlock {
// your animation has finished
}
tableView.setEditing(true, animated: true)
CATransaction.commit()
I have a button (a UINavigationBarItem) used for editing my UITableView, which only allows deletions. So when I press delete, the little red line comes up next to each cell, and I can delete each row.
When the button is pressed, the following function is called:
-(void)editButtonSelected:(id)sender {
if(self.editing)
NSLog(#"self.editing = true");
else
NSLog(#"self.editing = false");
if(self.editing) {
[super setEditing:NO animated:NO];
[tableView setEditing:NO animated:NO];
[tableView reloadData];
[leftButton setTitle:#"Delete"];
[leftButton setStyle:UIBarButtonItemStylePlain];
self.editing = false;
}
else {
[super setEditing:YES animated:YES];
[tableView setEditing:YES animated:YES];
[tableView reloadData];
[leftButton setTitle:#"Done"];
[leftButton setStyle:UIBarButtonItemStyleDone];
self.editing = true;
}
}
And it works fine. But only for a while. As soon as I introduce a new UIViewController, and then dismiss that controller, this delete function doesn't work on this main screen I have. It works fine until a new UIViewController is put on top. The button itself works fine, and the value of self.editing does get toggled between true and false correctly, but the little red lines do not show up. Why could this be happening?
I would guess that the target on your UINavigationBarItem is still set to the first view controller, not the subsequent view controllers that get pushed on the stack.
In an iPhone app I have a UITextView and a button, which lets the user send the content of the UITextView as a text message. The code looks like this:
MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
picker.messageComposeDelegate = self;
picker.body = textView.text;
[self presentModalViewController:picker animated:YES];
Everything works fine, except for when the message is either sent or Cancel is tapped in the MFMessageComposer: The keyboard for the UITextView is not shown anymore, even though the cursor blinks.
I tried a few things, including a [textView resignFirstRepsonder] in both the button code and -viewDidDisappear. [textView becomeFirstResponder] in the MFMessageComposeViewControllerDelegate method or the -viewDidAppear didn't change anything either...
Any ideas?
I had the same issue, and was resigned to accepting fabian's solution, but found that by calling [self dismissModalViewControllerAnimated:NO] and then calling [textView becomeFirstResponder], I was able to make the keyboard reappear. Something about the animation was screwing up the keyboard; looks like a bug in iOS 4.2.
After the view has disappeared, you need to make your view first responder. Add the MFMessageComposeViewControllerDelegate protocol to your header, then use the following:
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
[self dismissModalViewControllerAnimated:YES];
[self becomeFirstResponder];
}
Happy coding,
Zane
I had a similar problem and was able to fix it by calling becomeFirstResponder after a slight delay:
[textField performSelector:#selector(becomeFirstResponder) withObject:nil afterDelay:0.01];
The delay trick also solves the problem of missing text cursor after showing an UIAlert right after MFMessageComposeViewController finishes, however the delay needs to be much longer (0.5 sec in my case)
I was not able to find a better solution, so here is my fix:
In
- (void) actionSheet:(UIActionSheet *)actionSheet
willDismissWithButtonIndex:(NSInteger)buttonIndex
I dismiss the Keyboard and in
- (void) actionSheet:(UIActionSheet *)actionSheet
didDismissWithButtonIndex:(NSInteger)buttonIndex`
I present the MFMessageComposeViewController.
In
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller
didFinishWithResult:(MessageComposeResult)result
I don't do [textView becomeFirstResponder] as it doesn't work. Neither does it work in viewDidAppear:. The user has to tap the UITextField again.
Not a very nice solution but the only one I found...
As of iOS 5, here is one workaround. Before you present the MFMessageComposeViewController instance, resign first responder on your UITextView:
[self presentViewController:messageComposer animated:YES completion:NULL];
[textView resignFirstResponder];
Then in the delegate method messageComposeViewController:didFinishWithResult: do this:
[controller dismissViewControllerAnimated:YES completion:^{
[textView performSelector:#selector(becomeFirstResponder) withObject:nil afterDelay:0];
}];
This fixed the disappearing keyboard problem for me. Without having to permanently dismiss the keyboard.
This behaviour will not appear if viewController which is shown before modal VC is a child of navigation controller. So solution is to make fake UINavigationController and add your VC controller to nav controller.