【问题标题】:I want to implement pagination in iOS tableview [closed]我想在 iOS 表格视图中实现分页 [关闭]
【发布时间】:2016-01-02 13:29:28
【问题描述】:

我需要在 iOS tableview 中实现像 google 一样的水平分页。有什么方法可以使用单个 tableview 来做到这一点?您能建议一种方法吗?

【问题讨论】:

  • UICollectionView?
  • 你也需要垂直滚动的分页
  • @CharanGiri 我只需要水平滚动,这样每次滚动都会重新加载来自后端的不同数据。
  • 您应该为您的问题添加更多详细信息,您要查找的内容尚不清楚。
  • @jbouaziz 最初 tableview 有 10 条记录。但是有 100 条记录来自后端。我们可以在这个 tableview 中进行垂直分页,但是有什么方法可以让 tableview 可以滑动,以便新数据可以显示在同一个 tableview 中。

标签: ios uitableview pagination


【解决方案1】:

我建议您使用UICollectionView 而不是UITableView。话虽如此,为了从您的服务器加载分页数据,您需要添加几行代码: 您需要在视图控制器中添加变量以跟踪您的数据:

/**
 *  Set this flag when loading data.
 */
@property (nonatomic, assign) BOOL isLoading;


/**
 *  Set this flag if more data can be loaded.
 */
@property (assign, nonatomic) BOOL hasNextPage;


/**
 *  The current page loaded from the server. Used for pagination.
 */
@property (assign, nonatomic) int currentPage;


根据您使用的实现,有不同的实现方法,我们将检查是否是加载数据的正确时间。

UITableView

在您的UITableViewDelegate 实现中添加:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    // Check scrolled percentage
    //
    CGFloat yOffset = tableView.contentOffset.y;
    CGFloat height = tableView.contentSize.height - tableView.height;
    CGFloat scrolledPercentage = yOffset / height;


    // Check if all the conditions are met to allow loading the next page
    //
    if (scrolledPercentage > .6f && !self.isLoading && self.hasNextPage)
        [self loadNextPage:++self.currentPage];
}

UICollectionView

在您的UICollectionViewDelegate 实现中添加:

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {

    // Check scrolled percentage
    //
    CGFloat yOffset = collectionView.contentOffset.y;
    CGFloat height = collectionView.contentSize.height - collectionView.height;
    CGFloat scrolledPercentage = yOffset / height;


    // Check if all the conditions are met to allow loading the next page
    //
    if (scrolledPercentage > .6f && !self.isLoading && self.hasNextPage)
        [self loadNextPage:++self.currentPage];

}


然后加载下一页:

- (void)loadNextPage:(int)pageNumber {
    if (self.isLoading) return;
    self.isLoading = YES;

    // Fetch your data here
    // ..

    // Once the request is finished, call this
    self.isLoading = NO;
}

我还建议您检查滚动方向并将其添加为加载下一页的条件。 answer 解释了如何执行此操作。

【讨论】:

    【解决方案2】:

    UITableViewUIScrollView 的子类。在 UITableView 代表中执行此操作:

    - (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
        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 = 10;
        if(y > h + reload_distance) {
            NSLog(@"load more rows");
        }
    }
    

    如果您从 Web 服务获取数据并且该服务支持分页,那么您可以实现这一点。 if(y > h + reload_distance) 是 true,调用从 Web 服务加载更多数据,然后重新加载表视图。希望这会有所帮助.. :)

    【讨论】:

      【解决方案3】:

      您有 2 个选项可以满足您的要求 1.Scrollview分页并在其上创建UI设计。 2. Tableview with transformation 我的意思是水平的tableview。

      如果您确定与 tableview 相比,scrollview 内存会很高,因为您需要在分页上创建许多视图。如果你使用 tableview,它也很容易编码和处理内存。

      下面是转换tableview的代码。

      UITableView* menuTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 60, 320, 404) style:UITableViewStylePlain];
      menuTableView.delegate=self;
      menuTableView.dataSource=self;
      CGAffineTransform transform = CGAffineTransformMakeRotation(-1.5707963);
      menuTableView.transform = transform;
      // Repositions and resizes the view.
      CGRect contentRect = CGRectMake(0, 90, 320, 300);
      menuTableView.frame = contentRect;
      menuTableView.pagingEnabled= YES;
      menuTableView.separatorColor=[UIColor grayColor];
      [self.view addSubview:menuTableView];
      

      希望这会对您有所帮助.. 如果您需要任何其他信息,请告诉我

      【讨论】:

      • 我认为他的意思是对来自服务器的数据进行分页,而不是滚动视图。
      【解决方案4】:

      您从哪里获取数据?如果你有一个 API 层,你可以在 API 方法中构建数据分页。减少来回传递的实际数据量。

      您可以使用单个 tableView 来实现此目的。您需要定义部分的数量,以及每个部分的项目数。

      【讨论】:

      • 我正在使用 SMP 2.3 中的 MBO 获取数据。传入的数据包含 100 条记录,我需要一次显示 20 条记录。
      • 我是 iOS 新手。我们可以使用部分数量进行水平滚动吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-16
      • 2018-06-02
      • 2011-01-16
      • 1970-01-01
      • 1970-01-01
      • 2012-04-13
      • 1970-01-01
      相关资源
      最近更新 更多