1

I'm a new iOS Programmer, and I'm stuck at one problem.

I've a UITableView and I want to add 30 items from an array every time the user scrolls it down to the bottom

Could you help me please?

#import "ViewController.h"
@interface ViewController ()
{
    UIRefreshControl* refreshControl;
     int counter;
    //NSMutableArray *new;
}

@end

@implementation ViewController
@synthesize tableViewForScreen;
@synthesize array;

- (void)viewDidLoad
{
    [super viewDidLoad];

 refreshControl = [[UIRefreshControl alloc]init];
    [tableViewForScreen addSubview:refreshControl];
    [refreshControl addTarget:self action:@selector(refreshTable) forControlEvents:UIControlEventValueChanged];
    //new =[[NSMutableArray alloc]init];
    array = [[NSMutableArray alloc] initWithCapacity:570];

    counter =30;
    if(counter>[array count]){


        counter = [array count];
    }

}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [ tableViewForScreen flashScrollIndicators];
}

- (void)viewDidUnload
{
    [self setTableViewForScreen:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

#pragma mark - Table View
// set number of Sections in Table
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

 return 1;

}

// set number of Rows in each Sections of Table
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return counter;
}

// Set animated style for Table Cell Editing
-(void) setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [self.tableViewForScreen setEditing:editing animated:animated];
}

// Customize the appearance of Table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }    
    cell.textLabel.text = [array objectAtIndex:indexPath.row];
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

- (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
    CGPoint offset = aScrollView.contentOffset;
    CGRect bounds = aScrollView.bounds;
    CGSize size = aScrollView.contentSize;
    UIEdgeInsets inset = aScrollView.contentInset;
    float y = offset.y + bounds.size.height - inset.bottom;
    float h = size.height;
    // NSLog(@"offset: %f", offset.y);
    // NSLog(@"content.height: %f", size.height);
    // NSLog(@"bounds.height: %f", bounds.size.height);
    // NSLog(@"inset.top: %f", inset.top);
    // NSLog(@"inset.bottom: %f", inset.bottom);
    // NSLog(@"pos: %f of %f", y, h);

    float reload_distance = 10;
    if(y > h + reload_distance) {
        NSLog(@"load more rows");
        if(counter+30<[array count]){
            counter += 30;
            [tableViewForScreen reloadData];
            [tableViewForScreen flashScrollIndicators];
        } else if([array count]>counter) {
            counter = [array count];
            [tableViewForScreen reloadData];
            [tableViewForScreen flashScrollIndicators];
        }
    }
}
- (void)dealloc {
    [tableViewForScreen release];
    [super dealloc];
}

@end
Night Fury
  • 38
  • 6
  • so, would you like that at each "reloadData" of your UiTableView you will see 30 items more than previous reload? – ddb Jul 05 '16 at 06:50
  • yes @ddb i want to add 30 items more on each reload – Night Fury Jul 05 '16 at 06:53
  • please, check my answer :) – ddb Jul 05 '16 at 07:10
  • What do you mean by each refresh ? Is there any button for that or what is the setup ? write necessary things. You haven't provide your data. I mean have you already array with many objects and want to take 30 from it turn by turn or else? – Ketan Parmar Jul 05 '16 at 07:23
  • 1
    use paginator to load the every 30 items – HariKrishnan.P Jul 05 '16 at 07:25
  • use pagination ...follow https://github.com/nmondollot/NMPaginator/tree/master/NMPaginator – user2931321 Jul 05 '16 at 09:00
  • @SwapnilPatel, do you really have all of these sections? at every section reload, you add 30 sections.. – ddb Jul 05 '16 at 09:04
  • @ddb sorry my mistake in posting code here i am using only one section ty – Night Fury Jul 05 '16 at 09:07
  • @SwapnilPatel, what do you se when you exec this code? empty table? please, try to exec this code after `array` variable initialization `for(int i=0; i<570; i++) [array addObject:[NSString stringWithFormat:@"test%d",i]];` – ddb Jul 05 '16 at 09:12
  • thanks a million @ddb problem solved :) – Night Fury Jul 05 '16 at 09:20
  • @SwapnilPatel, anyway you are not using the refreshControl object in you code snippet – ddb Jul 05 '16 at 09:20
  • @Swapnil Patel, I updated my answer, please check it again and accept it now if it is correct :) thanks – ddb Jul 05 '16 at 09:24

3 Answers3

2

here it is an example of a UIViewController implementation, which contains a simple UITableView, called "myTableView" (I suppose it has already datasource and delegate set to the ViewController) I this solution, I suppose you have only one section, so every time the user scrolls the tableView to the bottom, a counter is increased by 30 in order to load another 30 items in the table (if any)

#import "ViewController.h"
@interface ViewController ()
{
     int counter;
}

@end

@implementation ViewController
@synthesize tableViewForScreen;
@synthesize array;

- (void)viewDidLoad
{
    [super viewDidLoad];

    [tableViewForScreen addSubview:refreshControl];
    array = [[NSMutableArray alloc] initWithCapacity:570];
    for(int i=0; i<570; i++)
        [array addObject:[NSString stringWithFormat:@"test%d",i]];

    counter=30;
    if(counter>[array count]){
        counter = [array count];
    }

}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [tableViewForScreen flashScrollIndicators];
}

- (void)viewDidUnload
{
    [self setTableViewForScreen:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

#pragma mark - Table View
// set number of Sections in Table
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

// set number of Rows in each Sections of Table
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return counter;
}

// Set animated style for Table Cell Editing
-(void) setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [self.tableViewForScreen setEditing:editing animated:animated];
}

// Customize the appearance of Table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }    
    cell.textLabel.text = [array objectAtIndex:indexPath.row];
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

- (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
    CGPoint offset = aScrollView.contentOffset;
    CGRect bounds = aScrollView.bounds;
    CGSize size = aScrollView.contentSize;
    UIEdgeInsets inset = aScrollView.contentInset;
    float y = offset.y + bounds.size.height - inset.bottom;
    float h = size.height;
    // NSLog(@"offset: %f", offset.y);
    // NSLog(@"content.height: %f", size.height);
    // NSLog(@"bounds.height: %f", bounds.size.height);
    // NSLog(@"inset.top: %f", inset.top);
    // NSLog(@"inset.bottom: %f", inset.bottom);
    // NSLog(@"pos: %f of %f", y, h);

    float reload_distance = 10;
    if(y > h + reload_distance) {
        NSLog(@"load more rows");
        if(counter+30<[array count]){
            counter += 30;
            [tableViewForScreen reloadData];
            [tableViewForScreen flashScrollIndicators];
        } else if([array count]>counter) {
            counter = [array count];
            [tableViewForScreen reloadData];
            [tableViewForScreen flashScrollIndicators];
        }
    }
}
- (void)dealloc {
    [tableViewForScreen release];
    [super dealloc];
}

@end

disclaimer: in order to check when the bottom is reached, I used this solution

Community
  • 1
  • 1
ddb
  • 2,423
  • 7
  • 28
  • 38
  • it's not a good idea. It will add the data to table even you just want to refresh/reload table. – MD. Jul 05 '16 at 07:22
  • @Mahesh, but this is what Swapnil Patel asked :) please check first and second question's comments – ddb Jul 05 '16 at 07:27
  • its not working result is like test0,test1,test2,test0,test1,test2test0,test1,test2test0,test1,test2test0,test1,test2test0,test1,test2... n so – Night Fury Jul 05 '16 at 07:27
  • as you didn't specified which kind of data are you handling, I used strings as data to display, but of course you can change them by using your data set. What kind of data do you want to display? – ddb Jul 05 '16 at 07:29
  • i have a array which contains 570 items , so i have to add 30+ on each reload e.g 1st reload 0-29, 2ed reload 30-59. and so.. – Night Fury Jul 05 '16 at 07:30
  • @Swapnil Patel, but what you mean for reload? do you mean that every time the app user scrolls down till the bottom of the tableview, then a reload of other 30 items should be triggered? – ddb Jul 05 '16 at 07:32
  • @ddb yes every time user scrolls down till the bottom of the tableview more new 30 items should be added in table, so when user will open that view table view will display only 30 items and when user will scrolls down till the bottom of the tableview it will add new 30 items from array – Night Fury Jul 05 '16 at 07:34
  • @ddb i updated my question with your solution but still facing the same problem ,thanks – Night Fury Jul 05 '16 at 08:58
0

You can use below code:

NSMutableArray *newInfos = //Your New data;

NSMutableArray *indexPaths = [NSMutableArray arrayWithCapacity:newInfos.count];
for(int i = 0; i < newInfos.count; i++) {
    NSIndexPath * indexPath = [NSIndexPath indexPathForRow:i inSection:0];
    [indexPaths addObject:indexPath];
}

[self.tableView beginUpdates]; 
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationRight];
[self.tableView endUpdates];
Ekta Padaliya
  • 5,743
  • 3
  • 39
  • 51
0

what do you mean?you mean you gotta inset 30 items to the tablview. So just update the dataSource and use the method to update the UI

 (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
Ekta Padaliya
  • 5,743
  • 3
  • 39
  • 51
Jimmy Ng
  • 101
  • 7
  • this method as flowing is ok.- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation; – Jimmy Ng Jul 05 '16 at 06:59
  • i want to add 30 items on each reload e.g 1st reload 30 ,2ed reload -it will add more 30 so count will be 60 – Night Fury Jul 05 '16 at 07:05
  • So just change the dataSource when you use the method 'reloadData'. – Jimmy Ng Jul 05 '16 at 07:14