我需要在表視圖中加載一些數據,雖然這是在後臺事情我想爲了增加一個活動的指標表明,有一個過程正在進行,將隱藏一旦這個過程飾面。什麼是最有效的方式來實現這樣的事情?添加UIActivityIndicator到UITableView的
0
A
回答
1
取決於是否要阻止您的用戶或不還怎麼重要的是活動指示。
如果你不想阻止用戶使用Application.networkActivityIndicatorVisible
,如果你想有更大的活動指標,但仍不能阻止用戶使用文本和UIActivityIndicator表視圖(tableview.height -= activityview.height
)以下動畫的UIView,然後隱藏完整或者如果您想阻止用戶,請使用阻止活動指示器。
- http://www.dejal.com/developer/?q=developer/dsactivityview
- https://github.com/jdg/MBProgressHUD(我是用MBProgressHUD個人,很容易學習和使用)
0
您可以添加具有UIIndicatorView和一個UILabel作爲您單元的子視圖的視圖。你可以用這種方式來顯示錯誤數據加載/錯誤的網絡/空數據...
例子:
控制器可以定義兩種模式:UITableViewModeMessage和UITableViewModeData。
在viewDidLoad中,設置self.tableViewMode = UITableViewModeMessage。何時返回數據,設置self.tableViewMode = UITableViewModeData併爲tableview重新加載數據。
一些代碼:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.tableViewMode == UITableViewModeMessage) {
return 2;
} else {
return self.yourEntries ? self.yourEntries.count : 0;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.tableViewMode == UITableViewModeMessage) {
return [self tableView:tableView messageCellForRowAtIndexPath:indexPath];
} else {
return [self tableView:tableView dataCellForRowAtIndexPath:indexPath];
}
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
// Remove Loading... progress view if exist.
UIView *progressView = [cell viewWithTag:100];
[progressView removeFromSuperview];
if (self.tableViewMode == UITableViewModeMessage) {
if (indexPath.row == 1) {
// remove the current label.
cell.textLabel.text = nil;
// We build progress view and attach to cell here but not in cellForRowAtIndexPath is because in this method cell frame is already calculated.
UIView *progressView = [self progressViewForCell:cell message:@"Loading..." alpha:0.9];
[cell addSubview:progressView];
}
}
}
// cell to display when loading
- (UITableViewCell *)tableView:(UITableView *)tableView messageCellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MessageCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.textLabel.textColor = [UIColor grayColor];
cell.textLabel.textAlignment = UITextAlignmentCenter;
}
if (indexPath.row == 1) {
cell.textLabel.text = @"Loading...";
} else {
cell.textLabel.text = nil;
}
return cell;
}
// cell to display when has data
- (UITableViewCell *)tableView:(UITableView *)tableView dataCellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"DataCell";
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [[self.yourEntries objectAtIndex:indexPath.row] description];
return cell;
}
// Build a view which has a UIActivityIndicatorView and a UILabel
- (UIView *)progressViewForCell:(UITableViewCell *)cell message:(NSString *)message alpha:(CGFloat)alpha
{
// NOTE: progressView needs to be removed from cell in cellForRowAtIndexPath:
CGRect progressViewFrame = CGRectZero;
progressViewFrame.size.width = CGRectGetMaxX(cell.bounds);
progressViewFrame.size.height = CGRectGetMaxY(cell.bounds) - 2;
UIView *progressView = [[UIView alloc] initWithFrame:progressViewFrame];
progressView.backgroundColor = RGBA(255, 255, 255, 1);
progressView.alpha = alpha;
progressView.tag = 100;
UILabel *loadingLabel = [[UILabel alloc] initWithFrame:progressView.bounds];
loadingLabel.backgroundColor = [UIColor clearColor];
loadingLabel.font = [UIFont systemFontOfSize:14];
loadingLabel.textColor = [UIColor blackColor];
loadingLabel.textAlignment = UITextAlignmentCenter;
loadingLabel.text = message;
CGFloat widthOfText = [loadingLabel.text sizeWithFont:loadingLabel.font].width;
CGFloat spaceBetweenIndicatorAndLabel = 5;
// activityIndicatorView has size in which width and height is equal to 20.
UIActivityIndicatorView *activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[activityIndicatorView setCenter:CGPointMake(CGRectGetMidX(cell.bounds) - (widthOfText/2) - (activityIndicatorView.bounds.size.width/2) - spaceBetweenIndicatorAndLabel, CGRectGetMidY(cell.bounds))];
[activityIndicatorView setColor:[UIColor blackColor]];
[activityIndicatorView startAnimating];
[progressView addSubview:activityIndicatorView];
[progressView addSubview:loadingLabel];
return progressView;
}
相關問題
- 1. 添加UIActivityIndicator到UITableView
- 2. 添加UIActivityIndicator到視圖的問題!
- 3. 將UIActivityIndicator添加到Default.png圖片
- 4. 如何將UIActivityIndicator添加到drawRect中
- 5. 我怎麼可以在UITableView上添加UIActivityIndicator
- 6. UIActivityIndicator
- 7. UIActivityIndicator同時將數據加載到UITableView中
- 8. UIActivityIndicator問題
- 9. UIActivityIndicator消失
- 10. UIActivityIndicator not displayed
- 11. KivaJSON與UIActivityIndicator?
- 12. UIActivityIndicator like MBProgressHud
- 13. xcode中的UIActivityIndicator?
- 14. 將UIActivityIndicator添加到模式視圖(ELCimagePicker)
- 15. UIActivityIndicator與UIWebView loadRequest?
- 16. 的Xcode - UIActivityIndicator太快
- 17. 無法看到UIActivityIndicator在UIWebView的
- 18. UIActivityIndicator無法在中心加載
- 19. 在iOS中通過UIImagePickerController加載UIActivityIndicator
- 20. 如何在UITableView的searchBar上方顯示UIActivityIndicator
- 21. UIActivityIndicator沒有顯示
- 22. UIAlertView與UIActivityIndicator矛盾
- 23. iPhone UIActivityIndicator線程幫助!
- 24. UIActivityIndicator動畫問題
- 25. UIActivityIndicator和GCD /線程
- 26. 如何居中UIActivityIndicator?
- 27. UIActivityIndicator stopAnimating()不起作用
- 28. UIActivityIndicator具有複雜
- 29. UIActivityIndicator瞬間顯示
- 30. 如何從NSObject類添加UIActivityIndicator並對其進行控制。
我想肯定這些鏈接。感謝您向我展示這些。 – 2012-07-31 20:55:11
順便說一句,你是如何將MBProgressHud項目添加到你的應用程序?每次我嘗試編譯時,都會收到編譯錯誤。 – 2012-07-31 21:33:50
什麼樣的編譯錯誤?如果您使用的是ARC,則必須禁用ARC for MBProgressHUD('Target'>'Build Phases'>'Compile Sources'>'MBProgressHUD.m' set compiler flag'-fno-objc-arc') – 2012-07-31 21:37:10