我有两个UITableViewController
,A和B。当我在A表格视图中点击一个单元格时,我将使用UINavigationController
来推出表格视图控制器B。但是B的数据是从互联网下载的,这需要数秒钟的时间。因此,我想在加载B时添加一个UIActivityIndicatorView
。我该如何实现这一点?
我有两个UITableViewController
,A和B。当我在A表格视图中点击一个单元格时,我将使用UINavigationController
来推出表格视图控制器B。但是B的数据是从互联网下载的,这需要数秒钟的时间。因此,我想在加载B时添加一个UIActivityIndicatorView
。我该如何实现这一点?
您可以将UIActivityIndicatorView 添加为单元格的accessoryView
。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinner.frame = CGRectMake(0, 0, 24, 24);
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryView = spinner;
[spinner startAnimating];
[spinner release];
}
在tableview B类的viewDidLoad方法中,添加一个活动指示器。
// Create the Activity Indicator.
let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)
activityIndicator.hidesWhenStopped = true
view.addSubview(activityIndicator)
// Position it at the center of the ViewController.
activityIndicator.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
activityIndicator.centerXAnchor.constraint(equalTo: view.centerXAnchor),
activityIndicator.centerYAnchor.constraint(equalTo: view.centerYAnchor)])
activityIndicator.startAnimating()
现在调用你的从网络下载数据的方法。
myDownloadMethod()
如果您不想在过程中使用户界面失去响应,请在另一个线程中执行。
请参阅此线程。 可以使用后台线程解析数据吗?
当通知内容已下载时,请停止指示器。
activityIndicator.stopAnimating()
现在你可以调用 tableview.reloadData()
来重新加载表格以显示新内容。
UIActivityIndicatorView * activityindicator1 = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(150, 200, 30, 30)];
[activityindicator1 setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
[activityindicator1 setColor:[UIColor orangeColor]];
[self.view addSubview:activityindicator1];
[activityindicator1 startAnimating];
[self performSelector:@selector(callfunction) withObject:activityindicator1 afterDelay:1.0];
-(void)callfunction
{
// Here your stuf
}
这对我很有效,你可以尝试一下:
在 didHighlightRowAtIndexPath
时调用 [activityIndicator startAnimating]
,在 didUnhighlightRowAtIndexPath
时调用 [activityIndicator stopAnimating]
比 didSelectRowAtIndexPath
更有用。
- (void)runIndicatorAtIndexPath:(NSIndexPath *)indexPath display:(BOOL)playing{
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.accessoryView = activityIndicator;
playing == YES ?[activityIndicator startAnimating]:[activityIndicator stopAnimating];
}
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
[self runIndicatorAtIndexPath:indexPath display:YES];
}
- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath{
[self runIndicatorAtIndexPath:indexPath display:NO];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
/* set cell attributes here */
NSInteger lastSectionIndex = [tableView numberOfSections] - 1;
NSInteger lastRowIndex = [tableView numberOfRowsInSection:lastSectionIndex] - 1;
if ((indexPath.section == lastSectionIndex) && (indexPath.row == lastRowIndex)) {
if(isMoreDataAvailableOnserver)
{
[self showSpinnerAtFooter];
[self getMoreDataFromServer];
}
else {
[self hideSpinnerAtFooter];
}
}
return cell;
}
- (void)hideSpinnerAtFooter {
self.tableView.tableFooterView = nil;
}
- (void)showSpinnerAtFooter {
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[spinner startAnimating];
spinner.frame = CGRectMake(0, 0, 320, 44);
self.tableView.tableFooterView = spinner;
}
spinner.frame
更改为spinner.frame =CGRectMake (cell.center.y, cell.center.x, cell.frame.size.width, cell.frame.size.height);
- Sam