如何在UITableView下添加活动指示器?

时间:2022-09-06 20:29:50

In my application i want to add activity indicator under UItableview where tableview will scroll but i do not know how can i add activity indicator over there.

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

To elaborate,when i will finish the scrolling of tableview then for more data i have to set a refresh option by an activity indicator.

要详细说明,当我要完成tableview的滚动时,为了获得更多的数据,我必须通过一个活动指示器来设置一个刷新选项。

i have tried it at the top of the tableview and it worked but i dont know how can i add it below the tableview. here is some sample code..

我已经在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];
}
}

so please someone give me some example code about how can i do that.

所以,请给我一些例子来说明我该怎么做。

actually i am new in iphone application development.So please help me.

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

Thanks in Advance.

提前谢谢。

4 个解决方案

#1


13  

The best way to add activity indicator is at footer of tableview.

添加活动指示器的最佳方法是在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;

}  

Add a button to the footerView and set action to button (as you needed). This how app store tableview works. On clicking button fetch some more data add to table array scroll the table without animation.

向footerView添加一个按钮,并将操作设置为按钮(如您所需要的)。这就是app store tableview的工作原理。在单击按钮时,将更多的数据添加到表数组中,而不是动画。

#2


49  

This is how I've done it:

我就是这样做的:

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

Then you just set tableFooterView to nil to remove it.

然后将tableFooterView设为nil来删除它。

Note: 44, btw, is the default height of a UITableViewCell when using UITableViewStylePlain. It's 45 for UITableViewStyleGrouped.

注意:在使用UITableViewStylePlain时,44、btw是UITableViewCell的默认高度。这对UITableViewStyleGrouped 45。

#3


17  

Swift Update :-

迅速更新:

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

#4


0  

One more way to add activity indicator. Check if your data displayed is not equals to the total count of data. Then you add one extra cell and Add "View More" button. When i click on the view more the hide that button and add activity indicator in that cell. And when process is done then reload the table view.

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

Thanks.

谢谢。

#1


13  

The best way to add activity indicator is at footer of tableview.

添加活动指示器的最佳方法是在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;

}  

Add a button to the footerView and set action to button (as you needed). This how app store tableview works. On clicking button fetch some more data add to table array scroll the table without animation.

向footerView添加一个按钮,并将操作设置为按钮(如您所需要的)。这就是app store tableview的工作原理。在单击按钮时,将更多的数据添加到表数组中,而不是动画。

#2


49  

This is how I've done it:

我就是这样做的:

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

Then you just set tableFooterView to nil to remove it.

然后将tableFooterView设为nil来删除它。

Note: 44, btw, is the default height of a UITableViewCell when using UITableViewStylePlain. It's 45 for UITableViewStyleGrouped.

注意:在使用UITableViewStylePlain时,44、btw是UITableViewCell的默认高度。这对UITableViewStyleGrouped 45。

#3


17  

Swift Update :-

迅速更新:

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

#4


0  

One more way to add activity indicator. Check if your data displayed is not equals to the total count of data. Then you add one extra cell and Add "View More" button. When i click on the view more the hide that button and add activity indicator in that cell. And when process is done then reload the table view.

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

Thanks.

谢谢。