On reloading the UICollectionView some time CollectionView cell does not bounce back. see the image collectionCell with title "Bgrawal ji Sourabh" should not be at this much distance from right side.It should have bounce.
Try this:
[self.collectionView scrollToItemAtIndexPath:yourDesiredIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:NO];
Please add below code before when your reload your collectionView
let indexes = NSIndexPath(forItem: 0, inSection: 0)
self.collectionViewSecond.scrollToItemAtIndexPath(indexes, atScrollPosition: .None, animated: false)
It's work for me hope work for you also!
Make sure you have enabled these two option in the CollectionView attribute Inspector i.e. Bounce on Scroll and Bounce on zoom this will allow the collectionView to give a bounce feed back on the scroll.
Related
Problem
I need to have a UITableView reload with a specific cell positioned at the top of the tableview. For instance, have the 3rd cell in the tableView appear at the top of the tableView, rather than the 1st.
The issue in doing this comes from not being able to use the
self.tableView.scrollToRow(at: indexPath, at: .top, animated: false)
function, because I have a custom header that expands and collapse when the user scrolls. That means when I call the scrollToRow() function, my header collapses, messing things up.
Question
Is there any way to have a tableView open/reload with the 3rd cell positioned at the top of the tableView without using a "scroll" function?
Thanks!
You can follow below steps to fulfill your requirement:
Identify NSIndexPath of one of the visible cells.
Get its rectForRowAtIndexPath.
Get the current contentOffset of the table, itself.
Or, you can use scrollRectToVisible
This is how we use contentOffset:
mainTableView.setContentOffset(rectForRowAtIndexPath.point, animated: true)
And scrollRectToVisible:
mainTableView.scrollRectToVisible(rectForRowAtIndexPath.point, animated: true)
When i add some text in TableView, it will show from Top of the tableview but i want to show it from bottom of tableView.
1. My TableView Screen
When i add user entered text in array and refresh tableView it will load from Top of TableView like below image.
but i want to add it from bottom like below image.
Can you guys help me, how can i achieve this?
Thanks
I am able to bind Data from bottom of TableView. All credit goes to Rajeshkumar R for helping me.
Step 1, apply a transform to the table view rotating it 180deg
tableView.transform = CGAffineTransformMakeRotation(-M_PI);
Step 2, rotate your raw cell 180deg in tableView:cellForRowAtIndexPath:
cell.transform = CGAffineTransformMakeRotation(M_PI);
Step 3, reverse your datasource. If you're using an NSMutableArray insert new objects at location 0 instead of using AddObject...
Now, the hard part is remembering that left is right and right is left only at the table level, so if you use
[tableView insertRowsAtIndexPaths:targetPath withRowAnimation:UITableViewRowAnimationLeft]
you can use the function below. It worked for me.
func scrollToLastRow() {
let indexPath = NSIndexPath(row: messageHistoryArray.count-1, section: 0)
self.createChatTableView.scrollToRow(at: indexPath as IndexPath, at: .bottom, animated: true)
}
I have one CollectionViewController and have 10 cells in it.
But when I run the app it displays the cell at bottom, but I want to display it from top cell at viewDidLoad.
I tried many things but it didn't work.
How to fix it?
in swift 3: after reloadData()
self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: true)
Its work fine......
collectionView.setContentOffset(.zero, animated: false)
Add this line after reloading the collection view.
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionTop animated:NO];
Edit:
Just want to add one more point here is if collectionView datasource has ZERO elements or it is nil then above code will not work and probably crash your app.
Write condition to check that datasource is available or not!
if (self.dataArray.count > 0) {
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionTop animated:NO];
}
Where, dataArray is your datasource.
Add this piece of code after reloading the collection view:
swift
yourCollectionView.setContentOffset(CGPoint.zero, animated: true)
objective-c
[yourCollectionView setContentOffset:CGPointZero animated:YES];
Swift 4 Swift 5
Actually, it's work just do
collectionView.setContentOffset(.zero, animated: false)
But, if in case your CollectionView implement .contentInset or outer margin, let's say margin top & bottom is set 16, this code will actually back to the top.
collectionView.setContentOffset(CGPoint(x: 0, y: -16), animated: true)
Please try this one
[self.yourcollectionview scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionTop animated:NO];
Make animated option false. Maybe some view animations are running during your collection view animation process.
collectionView.scrollToItem(at: indexPath, at: .top, animated: false)
To display the data from top cell in collectionView you can use one method
[collectionView setScrollsToTop:YES];
Hope it will Help You.
I have a UITableView that should show a particular row in its middle when it is first displayed. In viewDidLoad, I use scrollToRowAtIndexPath to achieve this:
// Center vertically on current row
let scrollIndexPath = NSIndexPath(forRow: currentRow, inSection: 0)
tableView.scrollToRowAtIndexPath(scrollIndexPath, atScrollPosition: UITableViewScrollPosition.Middle, animated: false)
This makes the top of the current row aligned with the middle of the table view meaning that the content of the row looks lower than it should be. How can I make the middle of the row align with the middle of the table view?
UITableView is a subclass of UIScrollView so you can use:
scrollRectToVisible:<#(CGRect)#> animated:<#(BOOL)#>
and calculate CGRect yourself, this way you have way more control than with using:
scrollToRowAtIndexPath
I have UITableView with cells. When i select cell - height increased, when i select this cell again - height decreased. In method "didSelectRowAtIndexPath" i use this -
NSArray *indArr = [[NSArray alloc] initWithObjects:(NSIndexPath*)[timer userInfo], nil];
[self.tableView reloadRowsAtIndexPaths:indArr withRowAnimation:UITableViewRowAnimationFade];
But "UITableViewRowAnimationFade" animation has faded effect, and cell is flickered.
I tried another animations, but they also unsuitable(
How i can avoid this effect and update cell without animation?
Setting the row animation to .None doesn't work - there is still stuff going on and moving about. In my case, some cells changed height, which caused the table view to scroll.
To hold perfectly still, do this:
UIView.performWithoutAnimation({
let loc = tableView.contentOffset
tableView.reloadRows(at: [indexPath], with: .none)
tableView.contentOffset = loc
})
Try this.
UIView.setAnimationsEnabled(false)
self.yourTableView.beginUpdates()
self.yourTableView.reloadRows(at: yourIndexPath, with:UITableViewRowAnimation.none)
self.yourTableView.endUpdates()
Have a look in the documentation of UITableView, there is a value for the rowAnimation called UITableViewRowAnimationNone, which won't animate anything. Try this instead:
[self.tableView reloadRowsAtIndexPaths:indArr withRowAnimation:UITableViewRowAnimationNone];