【问题标题】:how to add elements to tableview on scrolling iphone?如何在滚动 iphone 上向 tableview 添加元素?
【发布时间】:2014-10-28 18:22:45
【问题描述】:

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

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

如何做到这一点?

【问题讨论】:

    标签: iphone xcode uitableview dynamic loading


    【解决方案1】:

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

    【讨论】:

      【解决方案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】:

        这是不可行的。 在视图加载时调用 Web 服务并制作一系列选项,然后为表视图调用表数据源函数。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-05-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多