ios 加载 UITableViewCell 时如何设置 UIActivityIndicatorView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7212859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set an UIActivityIndicatorView when loading a UITableViewCell
提问by Wu Linsen
I have two UITableViewController
s, A and B. When I tap one cell in table A, I will use UINavigationController
to push table view controller B. But the data of B is downloaded from Internet, which takes several seconds. So I want to add a UIActivityIndicatorView
when loading B. How can I achieve this?
我有两个UITableViewController
S,A和B。当我点击表A中的一个单元格时,我将使用UINavigationController
推送表视图控制器B。但是B的数据是从互联网上下载的,这需要几秒钟。所以我想UIActivityIndicatorView
在加载 B 时添加一个。我怎样才能做到这一点?
回答by EmptyStack
You can add UIActivityIndicatorViewas cell's accessoryView
.
您可以将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];
}
回答by ArunGJ
In viewDidLoad of tableview B class, add an activity indicator.
在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()
Now call your method that downloads data from the network.
现在调用从网络下载数据的方法。
myDownloadMethod()
Do it in a different thread if you don't want the UI to be non responsive during the process.
如果您不希望 UI 在此过程中无响应,请在不同的线程中执行此操作。
read this thread for that. Can I use a background thread to parse data?
为此,请阅读此主题。 我可以使用后台线程来解析数据吗?
When you are notified that the contents are downloaded, stop the indicator.
当您收到下载内容的通知时,停止指示器。
activityIndicator.stopAnimating()
Now you can call tableview.reloadData()
for reloading the table to display the new contents.
现在您可以调用tableview.reloadData()
重新加载表格以显示新内容。
回答by chandrika
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
}
回答by Cao Yong
It's work well for me, you can try it:
对我来说效果很好,你可以试试:
Call [activityIndicator startAnimating]
when didHighlightRowAtIndexPath
,
and call [activityIndicator stopAnimating]
when didUnhighlightRowAtIndexPath
useful than didSelectRowAtIndexPath
.
调用[activityIndicator startAnimating]
时didHighlightRowAtIndexPath
调用[activityIndicator stopAnimating]
,didUnhighlightRowAtIndexPath
有用时调用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];
}
回答by aqsa arshad
This code below will display a spinner at the footer of the table view if more data is available on the server. You can change it according to your logic of fetching data from server.
如果服务器上有更多数据可用,下面的代码将在表视图的页脚显示一个微调器。您可以根据从服务器获取数据的逻辑来更改它。
- (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;
}