i'm using a UITableView, list elements from web service..
what i need to do is first call 20 elements from web s开发者_运维百科ervice and display in list, when the user scroll down call another 20 records from webservice and add to tableview..
how to do this?
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.
//
#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
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.
精彩评论