Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView load more when scrolling to bottom like Facebook application

I am developing an application that uses SQLite. I want to show a list of users (UITableView) using a paginating mechanism. Could any one please tell me how to load more data in my list when the user scrolls to the end of the list (like on home page on Facebook application)?

like image 866
rokridi Avatar asked Nov 28 '13 15:11

rokridi


People also ask

How do you optimize table views performance for smooth fast scrolling?

First off, the tableView(_:cellForRowAt:) method should be as fast as possible. This method is called every time a cell needs to be displayed. The faster it executes, the smoother scrolling the table view will be.

Is UITableView scrollable?

UITableView scrolls back because it's content size is equal to it's frame (or near to it). If you want to scroll it without returning you need add more cells: table view content size will be large then it's frame.

How do I scroll to top tableView?

To scroll to the top of our tableview we need to create a new IndexPath . This index path has two arguments, row and section . All we want to do is scroll to the top of the table view, therefore we pass 0 for the row argument and 0 for the section argument. UITableView has the scrollToRow method built in.

How do I Paginate in Swift?

Setting up for pagination in swift Let's create an Xcode project, add a table view in the Main storyboard. Create TableViewCell and Xib. After that, register TableViewCell and assign delegate and dataSource. Your TableViewCell.


1 Answers

You can do that by adding a check on where you're at in the cellForRowAtIndexPath: method. This method is easy to understand and to implement :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {     // Classic start method     static NSString *cellIdentifier = @"MyCell";     MyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];     if (!cell)     {         cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MainMenuCellIdentifier];     }      MyData *data = [self.dataArray objectAtIndex:indexPath.row];     // Do your cell customisation     // cell.titleLabel.text = data.title;      BOOL lastItemReached = [data isEqual:[[self.dataArray] lastObject]];      if (!lastItemReached && indexPath.row == [self.dataArray count] - 1)     {         [self launchReload];     } } 

EDIT : added a check on last item to prevent recursion calls. You'll have to implement the method defining whether the last item has been reached or not.

EDIT2 : explained lastItemReached

like image 96
shinyuX Avatar answered Oct 11 '22 11:10

shinyuX