如何在滚动iphone上向tableview添加元素? [英] how to add elements to tableview on scrolling iphone?

查看:76
本文介绍了如何在滚动iphone上向tableview添加元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用UITableView,列出来自Web服务的元素.

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

我需要做的是首先从Web服务中调用20个元素并显示在列表中,当用户向下滚动从Web服务中调用另外20条记录并添加到表视图中时.

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..

该怎么做?

推荐答案

//

#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

这篇关于如何在滚动iphone上向tableview添加元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆