Swapnil Patel Swapnil Patel - 5 months ago 15
Objective-C Question

How to add 30 data in UITableView from array on each refresh

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

ddb ddb
Answer

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