pronebird / UIScrollView-InfiniteScroll

UIScrollView ∞ scroll category
MIT License
1.06k stars 148 forks source link

Table is pushed down after finish loading #38

Closed greynguyen closed 8 years ago

greynguyen commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

@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 commented 8 years ago

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

greynguyen commented 8 years ago

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

pronebird commented 8 years ago

@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.