I need to show UIRefreshControl
at the bottom of the UITableView
like in the following Objective-C libraries:
but I'm using Swift and noticed some problems when using "Bridging-Header.h"
file with these libraries.
What is the alternative and the easiest way to achieve such behavior?
Thanks in advance.
You can add a refresh control to a table or collection view by assigning an instance of the UIRefreshControl class to this property. If your application targets versions prior to iOS 10, you simply add the refresh control as a subview to the table view.
A view that presents data using rows in a single column.
I got into the same problem in my project, and found this answer. You can add a UIActivityIndicatorView
instance on the bottom of the table, initially hidden, and when it enters the if condition above, unhide it and start its animation.
You may need to change the table's bottom offset, add "1 cell" height to it while it's loading and place it back when it finishes inside the if condition as well.
In Swift 3:
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
// Use this 'canLoadFromBottom' variable only if you want to load from bottom iff content > table size
let contentSize = scrollView.contentSize.height
let tableSize = scrollView.frame.size.height - scrollView.contentInset.top - scrollView.contentInset.bottom
let canLoadFromBottom = contentSize > tableSize
// Offset
let currentOffset = scrollView.contentOffset.y
let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height
let difference = maximumOffset - currentOffset
// Difference threshold as you like. -120.0 means pulling the cell up 120 points
if canLoadFromBottom, difference <= -120.0 {
// Save the current bottom inset
let previousScrollViewBottomInset = scrollView.contentInset.bottom
// Add 50 points to bottom inset, avoiding it from laying over the refresh control.
scrollView.contentInset.bottom = previousScrollViewBottomInset + 50
// loadMoreData function call
loadMoreDataFunction(){ result in
// Reset the bottom inset to its original value
scrollView.contentInset.bottom = previousScrollViewBottomInset
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With