How to make dynamically adding of rows when user reached to the last row of the UITableView?

点点圈 提交于 2019-12-02 19:51:00

How would you exactly want to invoke the loading of the additional 10 rows? When a user just scrolls down to see the first 10 that are loaded by default he might not want to load 10 more already.

You may add a "add more" line as the last row of your table. When the user clicks on this one you add 10 more.

(I don't know how one would detect a "bouncing effect" like shifting up the visible rows to indicate that one would like to see more.)

The basic logic would look like this:

  1. in cellForRowAtIndexPath you check if the user clicked on the last line and then invoke your code to add the 10
  2. to actually add 10 more lines you have to call [myTable reloadData]
  3. but before you call you need to increase the returned value of numberOfRowsInSection by 10 and make sure that cellForRowAtIndexPath will correctly return your new lines 11-20

ps if you REALLY want 10 additional rows to be loaded when the user reaches the end of the table you need to invoke the loading of 10 more in cellForRowAtIndexPath or willDisplayCell:forRowAtIndexPath: when it is called for your last line.

It is actually quite simple. What you need to do is implement the tableView:willDisplayCell:forRowAtIndexPath: method, which belongs to the UITableViewDelegate protocol. This method hits every time a cell is about to be displayed. So, it will let you know when the last cell is about to be displayed. Then you could do something like-

– (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *) cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{
     if(indexPath.row == [self.array count] - 1) //self.array is the array of items you are displaying
     {
          //If it is the last cell, Add items to your array here & update the table view
     }
}

Another (a bit mathematical) option is to implement UIScrollView delegate methods (UITableView is a subclass of UIScrollView), namely scrollViewDidEndScrollingAnimation: or scrollViewDidScroll:. These will let you know the y-position of the content the user is viewing. If it is found that the bottom most content is visible, you can add more items.

HTH,

Akshay

yash rathore

uitableview is derived from uiscrollview. To achieve your objective, you need to implement scrollViewDidEndDecelerating:

 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    float endScrolling = scrollView.contentOffset.y + scrollView.frame.size.height;
    if (endScrolling >= scrollView.contentSize.height) 
    {
      // your code goes here
    }
}

This will detect a "bouncing effect" like shifting up the visible rows to indicate that one would like to see more.

Srinivas
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [categoriesList count];
}

Here categoriesList is an array. We can add objects to this array and call reloadData in the tableview.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!