Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pull-to-refresh does not work on empty TableView

When I clear all data from datasource and reload the tableView the pull-to-refresh is no more available. How can I enable pull-to-refresh even on an empty tableView?

override func viewDidLoad() {
    super.viewDidLoad()
    ...
    // Pull to refresh for iPhone
    refreshControl?.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)

    tableView.tableFooterView = UIView(frame: CGRectZero) // To remove separator line on empty cells
    tableView.estimatedRowHeight = Constants.DetailTableView.estimatedRowHeight
    tableView.rowHeight = UITableViewAutomaticDimension

    setupRefreshControl()
}

private func setupRefreshControl() {
    // If on iPhone, List becomes the pull-to-refresh handler and the delegate
    if UIScreen.mainScreen().traitCollection.horizontalSizeClass == .Compact {
        SyncService.shared.delegate = self
        refreshControl?.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
        refreshControl?.tintColor = UIColor.whiteColor()
    } else {
        // Otherwise disable the refresh control in List
        refreshControl = nil
    }
}
like image 586
Martin Koles Avatar asked Apr 25 '26 01:04

Martin Koles


2 Answers

In my case, I just need to enable bouncing:

self.collectionView.alwaysBounceVertical = YES;

default NO. if YES and bounces is YES, even if content is smaller than bounds, allow drag vertically

like image 124
Pavlo Razumovskyi Avatar answered Apr 27 '26 21:04

Pavlo Razumovskyi


In the table refresh function I actually have code to display some 'empty table' view to remind the user the tableview is empty and this view was above the tableview's view, hence blocking the interaction. I fixed the view's layers and now it's working. So if you experience the issue in the question, just check whether some view does not overlap your tableview, which was my case here.

like image 41
Martin Koles Avatar answered Apr 27 '26 20:04

Martin Koles



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!