【问题标题】:How to add UIActivityIndicatorView to the bottom of UITableView, and toggle it on loadings如何将 UIActivityIndi​​catorView 添加到 UITableView 的底部,并在加载时切换它
【发布时间】:2016-11-11 11:09:06
【问题描述】:

我的UITableView 有“滚动加载更多”行为。当您滚动到底部时,它开始加载更多内容。我想要实现的是在服务调用正在进行时在 UITableView 底部显示UIActivityIndicatorView,并在添加新项目时隐藏它。

有什么好的方法可以解决这个问题?我正在使用 Swift,但也欢迎使用 Objective-C 解决方案。

【问题讨论】:

    标签: ios objective-c swift xcode uitableview


    【解决方案1】:

    我在 Objective-C 中实现了相同的功能

    cellForRowAtIndexPath

    if (indexPath.row == [self.yourArrayData count] - 1)
    {
         // Call the API
         [self loadMoreData:@"all" andPages:[NSString stringWithFormat:@"%ld", (long)pages] AndIsLoadMore:YES];
    }
    

    添加加载器或设计

    -(void)addLoaderViewAtBottom {
        viewLoader = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-100, self.view.frame.size.width, 50)];
        [viewLoader setBackgroundColor:[UIColor whiteColor]];
        [self.view addSubview:viewLoader];
    
        UIActivityIndicatorView *activityIndicator1 = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        activityIndicator1.center = CGPointMake((SCREEN_SIZE.width/2), 30);
        activityIndicator1.alpha = 1.0;
        activityIndicator1.hidden = NO;
        [viewLoader addSubview:activityIndicator1];
    
        [activityIndicator1 startAnimating];
    
    }
    
    -(void)hideLoaderView {
    [UIView animateWithDuration:1
                     animations:^(void) {
                         [viewLoader setFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, 50)];
    
                     }];
    }
    

    然后在您的 API 调用方法中使用这两种方法。在 API 调用开始时调用 addLoaderViewAtBottom 并在收到响应后调用 hideLoaderView

    编码愉快...

    【讨论】:

      【解决方案2】:

      初始化UIActivityIndicatorView

      @interface MyBulletinVC ()
      {
         UIActivityIndicatorView *activityLoadMore;
      }
      
      -(void)viewDidLoad 
      {
         activityLoadMore = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleGray)];
      }  
      

      检查UITableView 何时滚动以及content offset 是什么

      - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
      {
         float bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height;
         float tblBottomEdge = _tblBulletin.frame.size.height+_tblBulletin.frame.origin.y;
      
         if (bottomEdge <= tblBottomEdge)
         {
            return;
         }
      
         if (bottomEdge >= scrollView.contentSize.height)
         {
            [self show_bottom_loader];
         }
      }  
      

      如果适用于content offset,则在表的页脚中显示加载器:

      -(void)show_bottom_loader
      {
        _tblBulletin.tableFooterView = [self returnLoaderViewWhileFetchingRecords];
        CGPoint newContentOffset = CGPointMake(0, [_tblBulletin contentSize].height -  _tblBulletin.bounds.size.height);
        [_tblBulletin setContentOffset:newContentOffset animated:YES];
        [self getNewsRecords];
      }
      

      加载数据时隐藏页脚:

      -(void)hide_bottom_loader
      {
       _tblBulletin.tableFooterView = nil;
       UIView *v = [[UIView alloc] initWithFrame:CGRectZero];
       v.backgroundColor = [UIColor clearColor];
       [_tblBulletin setTableFooterView:v];
       [activityLoadMore stopAnimating];
      }
      
      -(UIView*)returnLoaderViewWhileFetchingRecords
      {
        UIView *view  = [[UIView alloc]initWithFrame:CGRectMake(0,0,_tblBulletin.frame.size.width,44)];
        view.backgroundColor  =[UIColor clearColor];
      
        UILabel *lblBg =[[UILabel alloc]initWithFrame:CGRectMake(0,1,_tblBulletin.frame.size.width,42)];
        lblBg.backgroundColor  =[UIColor whiteColor];
        [view addSubview:lblBg];
      
        UILabel *lbl =[[UILabel alloc]initWithFrame:CGRectMake(_tblBulletin.frame.size.width/2 - 20,0,_tblBulletin.frame.size.width/2,view.frame.size.height)];
        lbl.textAlignment = NSTextAlignmentLeft;
        lbl.text = @"Loading...";
        lbl.font = [UIFont fontWithName:@"Arial" size:14.0f];
        lbl.textColor = [UIColor darkGrayColor];
        lbl.backgroundColor = [UIColor clearColor];
        lbl.userInteractionEnabled = NO;
        [view lbl];
      
        activityLoadMore.frame = CGRectMake(_tblBulletin.frame.size.width/2-35,22,0,0);
        [activityLoadMore startAnimating];
        [view addSubview:activityLoadMore];
      
        return view;
      }
      
      -(void)getNewsRecords  
      {
          //fetch new records and add to your array.  
          [self hide_bottom_loader];  
          //reload table.
      }
      

      用你的表名替换tblBulletin

      【讨论】:

        猜你喜欢
        • 2010-12-15
        • 1970-01-01
        • 2016-08-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多