2011-10-31 8 views
18

私のアプリケーションでは、UItableviewの下でアクティビティインジケータを追加したいのですが、tableviewはスクロールしますが、そこにアクティビティインジケータを追加する方法がわかりません。UITableViewの下にアクティビティインジケータを追加するにはどうすればよいですか?

詳しくは、私がテーブルビューのスクロールを終了すると、より多くのデータについては、アクティビティインジケータでリフレッシュオプションを設定する必要があります。

私は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アプリケーション開発で新しいです。私を助けてください。

ありがとうございました。

答えて

13

アクティビティインジケータを追加する最も良い方法は、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; 

} 

フッタービューにボタンを追加し、必要に応じてボタンにアクションを設定します。これは、App Storeのテーブルビューの仕組みを示しています。ボタンをクリックすると、さらにいくつかのデータがテーブル配列に追加されます。アニメーションなしでテーブルをスクロールします。

+0

おかげで、私はそれをしようとします... – Emon

+0

:あなたは私にいくつかのソースコードを提供します?? – Emon

+0

コード全体やフッタ表示コード – Anand

0

もう1つ、アクティビティインジケータを追加する方法。表示されたデータがデータの合計数と等しくないかどうかを確認します。次に、追加のセルを1つ追加し、[詳細を表示]ボタンを追加します。ビューをクリックすると、そのボタンを非表示にし、そのセルにアクティビティインジケータを追加します。処理が完了したら、テーブルビューをリロードします。

ありがとうございました。

49

これは私がそれをやった方法です:

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

次に、あなたはそれを取り除くためにniltableFooterViewを設定します。

注: UITableViewStylePlainを使用した場合のデフォルトの高さはbtwで、UITableViewCellです。 の場合は45です。

+0

素晴らしいソリューション、ありがとう – oyatek

+0

くそー、これは簡単でした。どうもありがとう。 – HotFudgeSunday

17

スウィフトアップデート: -

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 
+0

@cChathuranga Silva..Thank u –

関連する問題