Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table is pushed down after finish loading #38

Closed
greynguyen opened this issue Jun 26, 2016 · 6 comments
Closed

Table is pushed down after finish loading #38

greynguyen opened this issue Jun 26, 2016 · 6 comments

Comments

@greynguyen
Copy link

Hi, I am new to iOS development. My table view is pushed down every time the scroll to bottom loading indicator is finished. So instead of showing the next cell at the space that the loading indicator was at, the whole table is pushed back down.

As a result, the user would have to scroll down to see the new cell. I want to achieve something like the demo you provided. When the user scrolls down, the new cell would show behind the loading indicator. Is there something I should do to achieve this effect?

@pronebird
Copy link
Owner

Hi @ndduong97,

Currently I use reloadData() which forces table view to re-calculate the content size and avoid weird bounce.

#31 adds a fix to make it possible to use beginUpdates and endUpdates without need to use reloadData which is certainly bad as it breaks animations. I was sure I merged the fix into master. 😳

@pronebird
Copy link
Owner

pronebird commented Jun 26, 2016

The fix has landed in 0.9.0. README has been updated with the code snippet.

Pretty much what you have to do is update table view and the run finishInfiniteScroll. Scroll offset should remain the same and new items should slide down behind the loading indicator.

Here is a snippet in Objective-C:

// setup infinite scroll
    [self.tableView addInfiniteScrollWithHandler:^(UITableView* tableView) {
        //
        // fetch your data here, can be async operation,
        // just make sure to call finishInfiniteScroll in the end
        //

        NSArray<NSIndexPath *> * indexPaths; // index paths of updated rows

        // make sure to update tableView before calling -finishInfiniteScroll
        [tableView beginUpdates];
        [tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
        [tableView endUpdates];

        // finish infinite scroll animation
        [tableView finishInfiniteScroll];
    }];

@greynguyen
Copy link
Author

greynguyen commented Jun 27, 2016

@pronebird Thank you very much for the response!

I am using reloadData() but I don't know why the bounce is still happening. Here is my code snippets:

func setUpInfiniteScroll() {
         tableView.addInfiniteScrollWithHandler { (scrollView) -> Void in
            self.maximumNumberOfDiscountsToShow += 5
            self.sortAndAddDataToTableArray()
        }
}
func showDataToTable() {
        self.shouldLoadingBarsBeDisplayed = false
        self.tableView.endRefreshing()
        if shouldTableFadeIn {
            UIView.transitionWithView(tableView,duration: 0.25,options: .TransitionCrossDissolve,animations: { () -> Void in
                self.tableView.reloadData()
                }, completion: nil);
        } else {
            self.tableView.reloadData()
        }
        self.tableView.finishInfiniteScroll()
    }

The sortAndAddDataToTableArray() eventually calls showDataTable()

EDIT: I did try to remove the table fade in animation but it still doesn't work

@pronebird
Copy link
Owner

@ndduong97 do you use refresh control at the same time? I wonder if they conflict somehow...

@greynguyen
Copy link
Author

greynguyen commented Jun 29, 2016

I used pull down refresher library from yalantis as well. I fixed it by moving the table.reloadData before finishInfiniteScroll

@pronebird
Copy link
Owner

@ndduong97 that makes sense. I guess transitionWithView postponed reloadData until animation finished, and finishInfiniteScroll was called too early, before new rows added to table view.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants