简体   繁体   中英

iPhone - Adding new sections and rows into UITableView

I have a UITableView to display a list of data.

The list will contain images, so I am loading images with lazy loading. Now I don't want to load the whole list at a time.

The loading should be like, first it should load some 10 records and when we scrolling down to the tableview , it should automatically load next 10 records as on.

For this I need to add rows when I am scrolling to bottom. The tableview may contain different sections.

So how can I add new rows and new sections at the end of the tableview while scrolling down?

At

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
   {
     if (indexPath.row == [yourArray count]-1)
        {
         //start fetching the next set of data in background asynchronously and when fetching is complete use delegate methods to reload or append your current UITableView , meanwhile start a activity indicator as footer to show loading
         }
   }

this is the effective method, if you try to implement synchronously when scrolling reaches bottom, it will make your device stuck

Look at the Apple documentation about UITableView . You can use the following method to add a row :

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

Firstly, the thing you want to do is known as Lazy Loading .

Means your table view will load images or show data in the cells which are currently visible to user. So have a look at this link. It provides sample code that might help you.

And to insert row, you can use

(void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM