how to add elements to tableview on scrolling iphone?

[亡魂溺海] 提交于 2019-12-12 18:12:09

问题


i'm using a UITableView, list elements from web service..

what i need to do is first call 20 elements from web service and display in list, when the user scroll down call another 20 records from webservice and add to tableview..

how to do this?


回答1:


You can load your 20 items from your web service and store them into an array. Then, create a table view and display those 20 items. If you want the scrolling action to trigger the loading then just become the delegate of the UIScrollView of the table view. Otherwise you could just have a button that says "Load More." When you want to load more just download the data and update the number of items in the list and reload the table view.




回答2:


//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
static int m=5;
static int resultsSize = 5; ;
- (void)viewDidLoad {
    [super viewDidLoad];


    recipes=[NSArray arrayWithObjects:@"one",@"two",@"three",@"four",@"five",@"six",@"seven",@"eight",@"nine",@"ten",@"eleven",@"twelve", nil];
    // Do any additional setup after loading the view, typically from a nib.

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return m;

    }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

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

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


    return cell;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)aScrollView
                  willDecelerate:(BOOL)decelerate
{
    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;

    float reload_distance = 50;
    if(y > h + reload_distance) {

        NSInteger i;

        NSMutableArray *arrayWithIndexPaths = [NSMutableArray array];
        for ( i = resultsSize; i < resultsSize + 2; i++)
        {
            [arrayWithIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];


        }

        m=resultsSize+2;
        resultsSize =resultsSize+2;
        [table insertRowsAtIndexPaths:arrayWithIndexPaths withRowAnimation:UITableViewRowAnimationFade];

    }
}

//}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
@end



回答3:


This is not feasible do it in pieces. at view load time call web service and make array of options then call table data source function for table view.



来源:https://stackoverflow.com/questions/4615140/how-to-add-elements-to-tableview-on-scrolling-iphone

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