I want to add my sliderButton after the animation has ended but this doesn't work, it adds the button while the deletion of the section is being animated.
[coursesTable beginUpdates];
[coursesTable deleteSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationTop];
[coursesTable endUpdates];
[self.view addSubview:sliderButton];
Is there a possibility I can call a method that does that for me when the animation ended?
I believe that wrapping your table update in animateWithDuration would work:
[UIView animateWithDuration:0.0 animations:^{
[coursesTable beginUpdates];
…
[coursesTable endUpdates];
} completion:^(BOOL finished) {
// Code to run when table updates are complete.
}];
Other methods that I found suggested here on Stack Overflow did not work for me.
I used this technique at one time and tested it enough to verify that the completion block was called after I called the table's endUpdates method, but rewrote my code so I didn't need it any more before I had completely verified that the animation was actually finished.
You can find the answer here:
How to detect that animation has ended on UITableView beginUpdates/endUpdates?
Hope it helps!
I have also not found the solution of this.I have to use performSelector:withObject:afterDelay: for getting my work done.
[coursesTable deleteSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationTop];
after this the tableview again asks data from its datasource... so all the datasource methods including cellforRow is again called..
i suggest keeping a BOOL is Deleting.
so now do this
[coursesTable beginUpdates];
[coursesTable deleteSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationTop];
[coursesTable endUpdates];
Deleting = YES;
in cellForRow method
{
if (index path.row == lastrow ) // last rowcell is reached
if(Deleting)
{
//tableView has reloaded.
[self.view addSubview:sliderButton];
Deleting = NO;;
}
}
Related
so i'm inserting a UITableViewCell into my tableView.
[searchTableView beginUpdates];
[searchTableView insertRowsAtIndexPaths:#[newIndexPath] withRowAnimation:UITableViewRowAnimationTop];
[searchTableView endUpdates];
after this i'm running an asynchronous request which updates the tableviewcell with a fade animation.
[someRequestWithCompletion:^(id data) {
dispatch_async(dispatch_get_main_queue(), ^{
[searchTableView beginUpdates];
[searchTableView reloadRowsAtIndexPaths:#[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
[searchTableView endUpdates];
});
}];
but it is possible that this request finishes before the insert animation is finished. if the request finishes after the insert-animation, there is no problem, if it finishes before and calls a reloadRowsAtIndexPaths on the same cell that is currently inserting, the cell displays immediately and forces a reload while the insert-animation fades out.
is there a way to trigger the reload update after the cell is completely inserted? what would be the best way to do this?
if there are any questions, please let me know in the comments
Try This..
U can reload your TableView After Completion of Animation.
[CATransaction begin];
[searchTableView beginUpdates];
[CATransaction setCompletionBlock: ^{
[searchTableView reloadData];
}];
[searchTableView deleteRowsAtIndexPaths: [NSArray arrayWithObjects: indexPath, nil]
withRowAnimation: UITableViewRowAnimationAutomatic];
[searchTableView endUpdates];
[CATransaction commit];
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.
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 UITableView whose contents are dynamically changing, like a FIFO stack. Cells are added to the bottom and removed from the top.
This works beautifully, and I can scroll to the indexPath so that the newest message always scrolls down to the bottom (Like a chat application).
Now.. I want to add a footer to that table section. Instead of using
SrollToRowAtIndexPath
I would like to be able to scroll to the tableFooterView.
Any ideas how I can do that would be appreciated.
I am using this to scroll to the footer view of a tableView:
[self.tableView scrollRectToVisible:[self.tableView convertRect:self.tableView.tableFooterView.bounds fromView:self.tableView.tableFooterView] animated:YES];
The best way to scroll a UITableView to the bottom of it's footerView is to simply set the content offset. You can calculate the bottom using the contentSize and the current bounds
Here is the way I do it.
CGPoint newContentOffset = CGPointMake(0, [self.instanceOfATableView contentSize].height - self.instanceOfATableView.bounds.size.height);
[self.instanceOfATableView setContentOffset:newContentOffset animated:YES];
Thanks to iphone_developer here is what I did :
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[tableView numberOfRowsInSection:0]-1 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
Then while I'm adding rows, I'm calling this and my tableView's footer view keeps being visible
Swift Version:
tableView.scrollToRow(at: IndexPath(row: self.tblview.numberOfRows(inSection: 0) - 1, section: 0), at: .top, animated: true)
So many poor answers. :-)
The BEST way:
[self.tableView scrollRectToVisible:
self.tableView.tableFooterView.frame animated:YES
];
Maybe something like:
[tableView setContentOffset:CGPointMake(0, tableView.contentSize.height) animated:YES];
Since UITableView is a subclass of UIScrollView, you can scroll to wherever you like using the UIScrollView method
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated
Just set the rect so that when it's visible the footer is visible, and you'll have your solution (you can use the footer's rect or something else, just so long as you get the right behavior all the time).
My late answer is for developer, who need to show footer when the keyboard is shown.
The right solution is to consider contentInset property (which can be changed after keyboard is shown), so it's super easy:
- (void)scrollToFooter {
UIEdgeInsets tableInsets = self.tableView.contentInset;
CGFloat tableHeight = self.tableView.frame.size.height - tableInsets.bottom - tableInsets.top;
CGFloat bottom = CGRectGetMaxY(self.tableView.tableFooterView.frame);
CGFloat offset = bottom - tableHeight;
if(offset > 0.f) {
[self.tableView setContentOffset:CGPointMake(0, offset) animated:YES];
}
}
I must notice, that in my case tableView was added to my own ViewController and one of cells have UITextField which become first responder. To move show footer when keyboard is shown you need to register keyboard did shown notification and (on iOS7) perform this method in the end of current run loop, coz in this case iOS7 automatically perform scrollToRowAtIndexPath after our method and footer will not be shown.
-(void)registerKeyboardNotification {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardDidShown:)
name:UIKeyboardDidShowNotification object:nil];
}
- (void)keyboardDidShown:(id)notification {
//move to the end of run loop
[self performSelector:#selector(scrollToFooter) withObject:nil afterDelay:.0];
}
The easiest way to do this is to use UITableViewScrollPositionTop on the LAST ROW in the LAST SECTION. This works very well for me...
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:LAST_ROW inSection:LAST_SECTION] atScrollPosition:UITableViewScrollPositionTop animated:YES];
Make sure your Table Footer View is well spaced out at the bottom and it should sit nicely animated inside the view...
Hope this helps...
This good work for me!
CGRect footerBounds = [addCommentContainer bounds];
CGRect footerRectInTable = [tableview convertRect:footerBounds fromView:addCommentContainer];
[tableview scrollRectToVisible:footerRectInTable animated:YES];
This works for me in Swift 4
func addRow() {
tableView.beginUpdates()
tableView.insertRows(at: [IndexPath(row: array.count - 1, section: 0)], with: .automatic)
tableView.endUpdates()
DispatchQueue.main.async {
self.scrollToBottom()
}
}
func scrollToBottom() {
let footerBounds = tableView.tableFooterView?.bounds
let footerRectInTable = tableView.convert(footerBounds!, from: tableView.tableFooterView!)
tableView.scrollRectToVisible(footerRectInTable, animated: true)
}
Great idea, was looking for this myself :) Here's sample code, which would do the trick:
[tableView reloadData];
NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:1];
[tableView scrollToRowAtIndexPath:index
atScrollPosition:UITableViewScrollPositionBottom animated:YES];
You MUST have at least one cell in your tableView footer, the problem is that it's going to visible. Didn't have time to test, but I'd guess you could make it really small?
Additionally you must implement correct stuff inside numberOfSectionsInTableView (at least one for table and one for footer), numberOfRowsInSection (at least one for footer, your last section), viewForHeaderInSection (nil except for your cell), heightForHeaderInSection (maybe if you set this as zero), cellForRowAtIndexPath (add special case for your cell in footer)...
That should be enough.
I'm using this:
- (void)scrollToBottom{
[self.myTable scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[self.fetchedResultsController.fetchedObjects count] -1 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
This work to me:
- (void)tableViewScrollToBottomAnimated:(BOOL)animated {
NSInteger numberOfRows = [self.tableView numberOfRowsInSection:0];
if (numberOfRows) {
if (self.tableView.tableFooterView) {
[self.tableView scrollRectToVisible:
self.tableView.tableFooterView.frame animated:YES
];
} else {
[self.tableView scrollToRowAtIndexPath:
[NSIndexPath indexPathForRow:numberOfRows-1 inSection:0]
atScrollPosition:UITableViewScrollPositionBottom
animated:animated
];
}
}
}
I haven't tried, but what happens when you scroll to the last row+1?
Another idea would be to always add a dummy entry at the end and make it have a different look so it's seen as a footer. Then you can always scroll to that.
I've got a UITableView with a UISearchBar as the tableViews.tableHeaderView. Just like the new Mail.app, Notes.app, etc. in 3.0. I want to hide the SearchBar until the user drags it in his sight.
My attempt only works when there're a couple of items in the tableView, so that the tableView actually wants to scroll. I call this in loadView:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self._tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
Nevertheless it seems that Apple handles such a serachbar differently. After draging out the searchbar it doesn't seem to be bounded to the tablecells anymore (in Notes.app, not in Mail.app).
But perhaps Apple has a distinct method for that new 3.0 behaviour, and I just can't find it?
Maybe you can try it this way...
[self.tableView setContentOffset:CGPointMake(0,40)];
Worked for me too. I used the following:
[self.tableView setContentOffset:CGPointMake(0, self.searchDisplayController.searchBar.frame.size.height) animated:NO];
to query the height of the search bar.
This one gets you the exact same behavior as iPod.app:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
CGFloat searchBarHeight = CGRectGetHeight([[[self searchDisplayController] searchBar] frame]);
if ([[self tableView] contentOffset].y < searchBarHeight)
[[self tableView] setContentOffset:CGPointMake(0, searchBarHeight)];
}
This works for me.
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.bounces = YES;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.tableView setContentOffset:CGPointMake(0, 44)];
}
I had to scroll first to top then setContentOffset to 0, Then searchBar will be visible :
self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: false)
self.tableView.setContentOffset(CGPointMake(0, 0), animated: false)
I kind of like doing it this way:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// Hide the table view header by default.
NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView scrollToRowAtIndexPath:index atScrollPosition:UITableViewScrollPositionTop animated:NO];
}
This way you don't really need to worry about how tall your header is. It just works!