我在我的UITableView
上有「滾動加載更多」行爲。當您滾動到底部時,它會開始加載更多內容。我想要實現的是在服務調用進行時在UITableView的底部顯示一個UIActivityIndicatorView
,並在添加新項目時隱藏它。如何將UIActivityIndicatorView添加到UITableView的底部,並在加載時切換它
解決這個問題的好方法是什麼?我正在使用Swift,但也歡迎客觀的C解決方案。
我在我的UITableView
上有「滾動加載更多」行爲。當您滾動到底部時,它會開始加載更多內容。我想要實現的是在服務調用進行時在UITableView的底部顯示一個UIActivityIndicatorView
,並在添加新項目時隱藏它。如何將UIActivityIndicatorView添加到UITableView的底部,並在加載時切換它
解決這個問題的好方法是什麼?我正在使用Swift,但也歡迎客觀的C解決方案。
我在執行相同的功能目標-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調用方法。呼叫addLoaderViewAtBottom
API調用啓動時並得到響應
快樂編碼後打電話hideLoaderView
...
初始化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
與你的表名。