如何在滚动 iPhone 上向 tableview 添加元素?

发布于 2024-10-10 19:30:29 字数 133 浏览 2 评论 0原文

我正在使用 UITableView,从 Web 服务列出元素。

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

如何要这样做吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

意中人 2024-10-17 19:30:29

您可以从 Web 服务加载 20 个项目并将它们存储到数组中。然后,创建一个表视图并显示这 20 个项目。如果您希望滚动操作触发加载,那么只需成为表视图的 UIScrollView 的委托即可。否则,您可能只有一个显示“加载更多”的按钮。当您想要加载更多时,只需下载数据并更新列表中的项目数并重新加载表视图。

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.

素手挽清风 2024-10-17 19:30:29
//

#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
//

#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
单身情人 2024-10-17 19:30:29

这是不可行的。
在视图加载时调用 Web 服务并创建选项数组,然后调用表视图的表数据源函数。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文