-1

Hi in my array there is huge amount of data and I have to display it in UITableView. But the condition here is I have to display only 5 records initially,then once the user scroll down I have to load more records.I tried searching it but didn't get any useful answer please help me and I agree that we have to use ((void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath )

Another way

we can adjust the return value of tableView:numberOfRowsInSection: method, every time we want to insert ten rows, you can plus 5 to the return value.

but i am not understand how to do this please help me

my code:

   #import "TableViewController.h"

@interface TableViewController ()
{
    UITableView * tableView;
    NSUInteger reloads_;
    NSMutableArray * MainArray;
    int  scrollValue;
    long dataLimit;
    NSArray * yourDataSource;
}

@end

@implementation TableViewController
- (void)viewDidLoad
{
    [super viewDidLoad];

    MainArray = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",nil];

    tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:tableView];
}

#pragma mark - UITableViewDataSource
// number of section(s), now I assume there is only 1 section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView
{
    return 1;
}

// number of row in the section, I assume there is only 1 row
- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section
{

    return yourDataSource.count;
}

// the cell will be returned to the tableView
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"newFriendCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = [yourDataSource objectAtIndex:indexPath.row];

    return cell;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    [self estimatedTotalData];
}

- (void)estimatedTotalData
{
    long currentRow = ((NSIndexPath *)[[tableView indexPathsForVisibleRows] lastObject]).row;

    // 25 here is the initial data count
    long estimateDataCount = 25;

    while (currentRow > estimateDataCount)
    {
        estimateDataCount+=25;
    }

    dataLimit = estimateDataCount;

    if (dataLimit == currentRow+1)
    {
        dataLimit+=25;

        if (dataLimit != estimateDataCount)
        {
            //[self requestForData]; or load necessary data
            // take not that dataLimit is the total data that must be displayed.
            NSArray *yourLocalData = @[@"1", @"2", @"3", @"4", @"5"];
            // just add more sample objects
            yourDataSource = [self setsLimitForObject: yourLocalData limit: dataLimit];
            [tableView reloadData];
        }
    }
}

- (NSArray *)setsLimitForObject:(id)object limit:(long)limit
{
    if ([object isKindOfClass:[NSArray class]])
    {
        NSMutableArray *tempArray = [object mutableCopy];

        NSRange range = NSMakeRange(0, limit);

        if (tempArray.count >= range.length)
            return [tempArray subarrayWithRange:range];

        else
        {
            NSLog(@"Out of bounds");

            return object;
        }

    } else NSLog(@"Sets Log: Cannot set limit for object %@", [object class]);

    return nil;
}

@end
AbhiRam
  • 2,033
  • 7
  • 41
  • 94
  • use UIRefreshControl. Once you scroll down one method call. You can add more objects in that method. – kb920 Dec 28 '15 at 11:08
  • This is the default behaviour of the table view right ? table view is such that it will not load entire data at a time instead it display the rows as per the height of the screen. when the user scrolls down the upper cells content will be removed and cell will be reused to show more content. You can combine this with pagination to show 5 records per page – harsha yarabarla Dec 28 '15 at 11:10
  • this is possible see this link http://stackoverflow.com/questions/20269474/uitableview-load-more-when-scrolling-to-bottom-like-facebook-application and http://stackoverflow.com/questions/18587570/uicollectionview-load-more-data – AbhiRam Dec 28 '15 at 11:13
  • use " scrollViewDidEndDragging" delegate method. – Vineesh TP Dec 28 '15 at 12:33

2 Answers2

0

Try this approach, this is more like a paging...

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self estimatedTotalData];
}

- (void)estimatedTotalData
{
    long currentRow = ((NSIndexPath *)[[YourTableView indexPathsForVisibleRows] lastObject]).row;

    // 25 here is the initial data count
    long estimateDataCount = 25;

    while (currentRow > estimateDataCount)
    {
        estimateDataCount+=25;
    }

    dataLimit = estimateDataCount;

    if (dataLimit == currentRow+1)
    {
        dataLimit+=25;

        if (dataLimit != estimateDataCount)
        {
            //[self requestForData]; or load necessary data
            // take not that dataLimit is the total data that must be displayed.
            NSArray *yourLocalData = @[@"1", @"2", @"3", @"4", @"5"];
            // just add more sample objects
            yourDataSource = [self setsLimitForObject: yourLocalData limit: dataLimit];
            [yourTable reloadData];
        }
    }
}

Update:

- (NSArray *)setsLimitForObject:(id)object limit:(long)limit
{
    if ([object isKindOfClass:[NSArray class]])
    {
        NSMutableArray *tempArray = [object mutableCopy];

        NSRange range = NSMakeRange(0, limit);

        if (tempArray.count >= range.length)
            return [tempArray subarrayWithRange:range];

        else
        {
            NSLog(@"Out of bounds");

            return object;
        }

    } else NSLog(@"Sets Log: Cannot set limit for object %@", [object class]);

    return nil;
}
0yeoj
  • 4,500
  • 3
  • 23
  • 41
  • what should i do inside this if (dataLimit != estimateDataCount) condition? please explain clearly – AbhiRam Dec 28 '15 at 12:19
  • Its up to you.. If you are loading data from server.. that where you put your request code.. but if you have your data stored locally you just have to load the stored data then limit the array depending from the dataLimit. im currently using the `-subarrayWithRange` approach which is the second option in this comment. – 0yeoj Dec 28 '15 at 12:24
  • @AbhiRam, wait i'll add my data limiting code for you to try and test it. – 0yeoj Dec 28 '15 at 12:27
  • @AbhiRam, there. check it out. :) happy new year. Cheers! :) – 0yeoj Dec 28 '15 at 12:34
  • what is that yourDataSource? – AbhiRam Dec 28 '15 at 12:37
  • @AbhiRam that is your `mainArray` in your code above.. :) – 0yeoj Dec 28 '15 at 12:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/99123/discussion-between-abhiram-and-0yeoj). – AbhiRam Dec 28 '15 at 12:39
-1

You can use below code for your cellForRowAtIndexPath,

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *CellIdentifier = @"newFriendCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

cell.textLabel.text = [mainArray objectAtIndex:indexPath.row];

if (indexPath.row % 5 == 0) { // this if loop will get execute at every fifth row
    // if you are loading data from API response then call API here and add next set of data into your existing array.
    // then just reload your tableview by it's name e.g. [theTableView reloadData];
}

return cell;
}

and for your numberOfRowsInSection, instead of fixed 5 rows, just use mainArray.count like below,

- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section
{
    return mainArray.count;
}
Parth Pandya
  • 1,460
  • 3
  • 18
  • 34