Scroll all the way down to the bottom of UITableView
If your requirement is to scroll to the last cell
of tableView
you can use setContentOffset
like this so that you can scroll to the last cell of tableView
.
Put this in the reloadData() function:
func reloadData(){
chatroomTableView.reloadData()
dispatch_async(dispatch_get_main_queue(), { () -> Void in
let scrollPoint = CGPoint(x: 0, y: self.chatroomTableView.contentSize.height - self.chatroomTableView.frame.size.height)
self.chatroomTableView.setContentOffset(scrollPoint, animated: true)
})
}
Add this to your UITableViewDelegate :
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
let lastRowIndex = tableView.numberOfRowsInSection(0)
if indexPath.row == lastRowIndex - 1 {
tableView.scrollToBottom(true)
}
}
Add this to the bottom of your .swift file:
extension UITableView {
func scrollToBottom(animated: Bool = true) {
let sections = self.numberOfSections
let rows = self.numberOfRowsInSection(sections - 1)
if (rows > 0){
self.scrollToRowAtIndexPath(NSIndexPath(forRow: rows - 1, inSection: sections - 1), atScrollPosition: .Bottom, animated: true)
}
}
}
Auto scroll to bottom of UITableView
let indexPath = IndexPath(item: noOfRows, section: 0)
yourTableView.scrollToRow(at: indexPath, at: UITableView.ScrollPosition.bottom, animated: true)
How to scroll to the bottom of a UITableView on the iPhone before the view appears
I believe that calling
tableView.setContentOffset(CGPoint(x: 0, y: CGFloat.greatestFiniteMagnitude), animated: false)
will do what you want.
UITableView scroll to bottom
Scrolling to tableViewCell
?
//for instance, you have 15 cells
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:14 inSection:0];
[self.tableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionTop
animated:YES];
iOS UITableView Scroll to bottom of section
You can try with this code:
int yourSection = 2;
int lastRow = [tableView numberOfRowsInSection:yourSection] - 1;
[tableView scrollToRowAtIndexPath:[NSIndexPath lastRow inSection:yourSection] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
You get the numbers of rows in your section, then scroll to that indexPath.
UITableView not scrolling to the bottom
Not being able to scroll to the bottom of the tableview is usually a symptom of its height being too large. (i.e. it's cut off at the bottom)
Don't compute the height you need and put those numbers in your code. Just find out what it should be from the view hierarchy. For example, you might compute the height of your table view with CGRectGetHeight(self.view.bounds) - CGRectGetMaxY(self.iAdView)
, and the view's y origin with CGRectGetMaxY(self.iAdView)
assuming that the iAd view and your table view are both subviews of self.view
. Or, even better, just use autoresize masks or autolayout to keep the table view the right size.
Tableview won't fully scroll to the bottom
the is because of tabBar it is hide the table view so you can add this code to your ViewController
let adjustForTabbarInsets: UIEdgeInsets = UIEdgeInsets(0, 0, self.tabBarController!.tabBar.frame.height, 0)
self.yourTableView.contentInset = adjustForTabbarInsets
self.yourTableView.scrollIndicatorInsets = adjustForTabbarInsets
Detect when UITableView has scrolled to the bottom
Swift 3
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row + 1 == yourArray.count {
print("do something")
}
}
Related Topics
Ruby: Loaderror - Library Not Found for Class Digest::Sha1 -- Digest/Sha1
Swift Language Multicast Delegate
Invert Calayer Mask That Is Based on a Stroke (No Fill)
Uibezierpath Subclass Initializer
How to Insert a Override Function into a If Else Statement
Swift Good Coding Practice: If Statement with Optional Type Bool
How to Work with Udp Sockets in iOS, Swift
Unrecognized Selector Sent to Instance Swift 3
Center Horizontal Uicollectionview Based on Content
Screen Recording When My iOS App Is in Background with Replaykit
Swift Computed Properties Cannot Be Used in Init
Save Struct in Class to Nsuserdefaults Using Swift
Checking If Text Fields Are Empty Cause Error in Swift 2
Cannot Invoke 'Decode' with an Argument List of Type '(T, From: Data)'
Get Section Number in Custom Cell Button Action
Dynamically Passing Closure with Keypaths to a Sorting Function