【问题标题】:How can i add a activity indicator below the UITableView?如何在 UITableView 下方添加活动指示器?
【发布时间】:2011-12-18 16:41:02
【问题描述】:

在我的应用程序中,我想在 UItableview 下添加活动指示器,其中 tableview 将滚动,但我不知道如何在那里添加活动指示器。

详细说明,当我完成 tableview 的滚动时,对于更多数据,我必须通过活动指示器设置刷新选项。

我已经在 tableview 的顶部尝试过它并且它有效,但我不知道如何将它添加到 tableview 下方。这是一些示例代码..

      - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
if (isLoading) return;
isDragging = YES;


refreshHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, scrollView.contentOffset.y + REFRESH_HEADER_HEIGHT, 320, REFRESH_HEADER_HEIGHT)];
refreshHeaderView.backgroundColor = [UIColor clearColor];

refreshLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, scrollView.contentOffset.y + REFRESH_HEADER_HEIGHT, 320, REFRESH_HEADER_HEIGHT)];
refreshLabel.backgroundColor = [UIColor clearColor];
refreshLabel.font = [UIFont boldSystemFontOfSize:12.0];
refreshLabel.textAlignment = UITextAlignmentCenter;

refreshArrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"refresharrow.png"]];
refreshArrow.frame = CGRectMake((scrollView.contentOffset.y + REFRESH_HEADER_HEIGHT - 27) / 2,
                                (scrollView.contentOffset.y + REFRESH_HEADER_HEIGHT - 44) / 2,
                                27, 44);

refreshSpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
refreshSpinner.frame = CGRectMake((scrollView.contentOffset.y + REFRESH_HEADER_HEIGHT - 20) / 2, (scrollView.contentOffset.y + REFRESH_HEADER_HEIGHT - 20) / 2, 20, 20);
refreshSpinner.hidesWhenStopped = YES;

[refreshHeaderView addSubview:refreshLabel];
[refreshHeaderView addSubview:refreshArrow];
[refreshHeaderView addSubview:refreshSpinner];
[tableview addSubview:refreshHeaderView];


}

 - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (isLoading) {
    // Update the content inset, good for section headers
    if (scrollView.contentOffset.y > 0){
        NSLog(@"scrollView.contentOffset.y 1= %d",scrollView.contentOffset.y );
        tableview.contentInset = UIEdgeInsetsZero;
    }
    else if (scrollView.contentOffset.y >= -REFRESH_HEADER_HEIGHT){
        NSLog(@"scrollView.contentOffset.y 2= %d",scrollView.contentOffset.y );
        tableview.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    }
} else if (isDragging && scrollView.contentOffset.y > scrollView.contentOffset.y + REFRESH_HEADER_HEIGHT) {
    // Update the arrow direction and label
    [UIView beginAnimations:nil context:NULL];
    if (scrollView.contentOffset.y > scrollView.contentOffset.y + REFRESH_HEADER_HEIGHT) {
        // User is scrolling above the header
        NSLog(@"scrollView.contentOffset.y 3= %d",scrollView.contentOffset.y );
        refreshLabel.text = self.textRelease;
        [refreshArrow layer].transform = CATransform3DMakeRotation(M_PI, 0, 0, 1);
    } else { // User is scrolling somewhere within the header
        refreshLabel.text = self.textPull;
        [refreshArrow layer].transform = CATransform3DMakeRotation(M_PI * 2, 0, 0, 1);
    }
    [UIView commitAnimations];
}
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
if (isLoading) return;
isDragging = NO;
if (scrollView.contentOffset.y >= scrollView.contentOffset.y + REFRESH_HEADER_HEIGHT) {
    // Released above the header
    [self startLoading];
}
}

所以请有人给我一些关于我该怎么做的示例代码。

实际上我是 iphone 应用程序开发的新手。所以请帮助我。

提前致谢。

【问题讨论】:

    标签: iphone uitableview uiscrollview uiactivityindicatorview


    【解决方案1】:

    这就是我的做法:

    UIActivityIndicatorView *spinner = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] autorelease];
    [spinner startAnimating];
    spinner.frame = CGRectMake(0, 0, 320, 44);
    self.tableView.tableFooterView = spinner;
    

    然后您只需将tableFooterView 设置为nil 即可将其删除。

    注意: 顺便说一句,44 是使用UITableViewStylePlainUITableViewCell 的默认高度。 UITableViewStyleGrouped 是 45。

    【讨论】:

      【解决方案2】:

      快速更新:-

      let pagingSpinner = UIActivityIndicatorView(activityIndicatorStyle: .Gray)
      pagingSpinner.startAnimating()
      pagingSpinner.color = UIColor(red: 22.0/255.0, green: 106.0/255.0, blue: 176.0/255.0, alpha: 1.0)
      pagingSpinner.hidesWhenStopped = true
      tableView.tableFooterView = pagingSpinner
      

      【讨论】:

      • @cChathuranga Silva..谢谢你
      【解决方案3】:

      添加活动指示器的最佳方式是在 tableview 的页脚。

          - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { 
      UIView *headerView = [[[UIView alloc] init]autorelease];
      
          [headerView setBackgroundColor:[UIColor clearColor]];
      
      UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
      [button addTarget:self 
                 action:@selector(aMethod:)
       forControlEvents:UIControlEventTouchDown];
      [button setTitle:@"Load More" forState:UIControlStateNormal];
      button.frame = CGRectMake(10.0, 210.0, 160.0, 40.0);
      
      
          [headerView addSubview:button];
      
          [headerLabel release];
      
          return headerView;
      
      }  
      

      向footerView 添加一个按钮并将操作设置为按钮(根据需要)。这就是应用商店表格视图的工作方式。点击按钮获取更多数据添加到表格数组滚动表格没有动画。

      【讨论】:

      【解决方案4】:

      另一种添加活动指示器的方法。检查您显示的数据是否不等于数据总数。然后添加一个额外的单元格并添加“查看更多”按钮。当我单击更多视图时,隐藏该按钮并在该单元格中添加活动指示器。处理完成后,重新加载表格视图。

      谢谢。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-19
        • 1970-01-01
        • 2020-05-19
        • 1970-01-01
        • 2013-10-28
        • 1970-01-01
        相关资源
        最近更新 更多