Add rows dynamically to UITableView

I am developing an app that fetches some messages from a web service that returns them by pages. I am displaying the first page initially, but if the user wants to scroll down and see past messages, I am supposed to make the request for the next page and get the messages. My question is, what's the best way to expand the Table View and add the messages to the data source and load them in the table so the user can see them?

Thanks.

Answers


Create 'load more' section with one row in it. Place an activity indicator in it. In 'cellForItem' for this section make a request and reloadData once you receive the data.


You can detect when the table view is near the bottom. Make a request for more data and then load it into the tableview's data source. (Array or whatever you're using) then you just reload the tableview. Row count can be the datasource you're using.

I do this often in my app.


Implement the UIScrollView delegate method scrollViewDidScroll, and check whether the table has been scrolled close to the bottom, and then update your array that helps the datasource methods popular the table view, and call reloadData:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    DLog(@"Did scroll: contentOffsetY: %f, contentHeight:%f", scrollView.contentOffset.y, scrollView.contentSize.height);

    float scrollViewHeight = scrollView.frame.size.height;
    float scrollContentSizeHeight = scrollView.contentSize.height;
    float scrollOffset = scrollView.contentOffset.y;

    if (scrollOffset + scrollViewHeight >= scrollContentSizeHeight)
    {
        // update your datasource/array that provides content to table view
        [yourTableView reloadData];
    }
}

Need Your Help

Multiple shops one shopping cart is not working

magento e-commerce shopping-cart

I have created in magento 2 shops with 1 common shopping cart. If I am a visitor and add to cart 1 product from 1st store and then go to 2nd store the cart reset.I saw on gap.com that this work. An...

Regex to remove xml declaration from a string

c# xml regex tidy

First of all, I know this is a bad solution and I shouldn't be doing this.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.