2014-10-08 61 views
0

我的應用程序有幾個UITableView,它們應該在我讀取數據填充表時顯示一個加載指示器。我試圖想出一個巧妙的方法來實現這一點。帶有加載活動指示器的UITableView子類

我應該子類UITableView,默認覆蓋tableView與另一個視圖,有一個活動指標和標籤?然後,我可以在我的故事板中分配每個UITableView。

但我有兩個問題:

  1. 我讀過這不是一個好主意,子類的UITableView?這樣做有沒有更好的範例?
  2. 如果我是UITableView的子類,如何開始以編程方式將自定義視圖添加爲疊加層?
+0

你說的是在呈現出裝載圖屏幕中心? – gabbler 2014-10-08 17:45:21

+0

你有沒有考慮過使用'UIRefreshControl'?如果你使用'UITableViewController',你可以免費獲得這個功能 – 2014-10-08 17:45:38

回答

1

你可以用我的解決方案:

UIViewsIndicatorsPull.h

#import <Foundation/Foundation.h>  

@interface UIViewsIndicatorContainer : NSObject 

@property (nonatomic, weak) UIView *view; 
@property (nonatomic, strong)UIActivityIndicatorView *indicator; 

@end 


@interface UIViewsIndicatorsPull : NSObject 

+ (instancetype)instance; 

- (UIActivityIndicatorView*)indicatorForViewObject:(UIView*)object; 
- (UIActivityIndicatorView*)indicatorForViewObject:(UIView*)object style:(UIActivityIndicatorViewStyle)style; 
- (void)removeIndicatorForViewObject:(UIView*)object; 

@end 

UIViewsIndicatorsPull.m

#import "UIViewsIndicatorsPull.h" 

@implementation UIViewsIndicatorsPull 
{ 
    NSMutableArray *indicators; 
} 

+ (instancetype)instance 
{ 
    static UIViewsIndicatorsPull *sharedInstance; 
    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
     sharedInstance = [UIViewsIndicatorsPull new]; 
    }); 

    return sharedInstance; 
} 

- (UIActivityIndicatorView*)indicatorForViewObject:(UIView*)object 
{ 
    return [self indicatorForViewObject:object style:UIActivityIndicatorViewStyleGray]; 
} 

- (UIActivityIndicatorView*)indicatorForViewObject:(UIView*)object style:(UIActivityIndicatorViewStyle)style 
{ 
    if (indicators == nil) { 
     indicators = [NSMutableArray array]; 
    }   
    UIActivityIndicatorView *result; 
    for (UIViewsIndicatorContainer *entity in indicators){ 
     if (entity.view == object) { 
      result = (UIActivityIndicatorView*)entity.indicator; 
      break; 
     } 
    } 

    if (result == nil) { 
     result = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:style]; 

     [object addSubview:result]; 

     UIViewsIndicatorContainer *entity = [[UIViewsIndicatorContainer alloc] init]; 
     entity.view = object; 
     entity.indicator = (UIActivityIndicatorView*)result; 

     [indicators addObject:entity]; 
    } 

    return result; 
} 

- (void)removeIndicatorForViewObject:(UIView*)object 
{ 
    for (UIViewsIndicatorContainer *entity in indicators){ 
     if (entity.view == object) { 
      UIActivityIndicatorView *indicator = entity.indicator; 
      [indicator stopAnimating]; 
      [indicator removeFromSuperview]; 

      [indicators removeObject:entity]; 
      break; 
     } 
    } 
}  
@end 

@implementation UIViewsIndicatorContainer 

@end 

的UIView + Additions.h

#import <UIKit/UIKit.h> 
#import "UIViewsIndicatorsPull.h" 

#define UIViewAutoresizingAll UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth 

@interface UIView (Additions) 

//process indicator 
- (void)showProcessIndicator; 
- (void)showProcessIndicatorWithStyle:(UIActivityIndicatorViewStyle)style; 
- (void)showProcessIndicatorWithAlignment:(UIViewContentMode)mode; 
- (void)showProcessIndicatorWithAlignment:(UIViewContentMode)mode style:(UIActivityIndicatorViewStyle)style; 
- (void)hideProcessIndicator; 

- (BOOL)isProcessIndicatorPresented; 

@end 

的UIView + Additions.m

#import "UIView+Additions.h" 

@implementation UIView (UIView_Additions) 

- (void)showProcessIndicatorWithStyle:(UIActivityIndicatorViewStyle)style 
{ 
    [self showProcessIndicatorWithAlignment:UIViewContentModeCenter style:style]; 
} 

- (void)showProcessIndicator 
{ 
    [self showProcessIndicatorWithAlignment:UIViewContentModeCenter style:UIActivityIndicatorViewStyleGray]; 
} 

- (void)showProcessIndicatorWithAlignment:(UIViewContentMode)mode style:(UIActivityIndicatorViewStyle)style 
{ 
    UIActivityIndicatorView *indicator = (UIActivityIndicatorView*)[[UIViewsIndicatorsPull instance] indicatorForViewObject:self style:style]; 
    [self positionIndicator:indicator andAlignment:mode]; 

    [indicator startAnimating]; 
    [self.superview addSubview:indicator]; 

    self.alpha = 0; 
} 

- (void)showProcessIndicatorWithAlignment:(UIViewContentMode)mode 
{ 
    [self showProcessIndicatorWithAlignment:mode style:UIActivityIndicatorViewStyleGray]; 
} 

- (void)positionIndicator:(UIActivityIndicatorView*)indicator andAlignment:(UIViewContentMode)mode 
{ 
    CGRect frame = indicator.frame; 

    switch (mode) { 
     case UIViewContentModeRight: 
      frame.origin.x = self.frame.origin.x + self.frame.size.width - frame.size.width; 
      frame.origin.y = self.frame.origin.y + self.frame.size.height/2 - frame.size.height/2; 
      indicator.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin; 
      break; 

     case UIViewContentModeLeft: 
      frame.origin.x = self.frame.origin.x; 
      frame.origin.y = self.frame.origin.y + self.frame.size.height/2 - frame.size.height/2; 
      indicator.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin; 
      break; 

     case UIViewContentModeTop: 
      frame.origin.x = self.frame.origin.x + self.frame.size.width/2 - frame.size.width/2; 
      frame.origin.y = self.frame.origin.y; 
      indicator.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; 
      break; 

     case UIViewContentModeBottom: 
      frame.origin.x = self.frame.origin.x + self.frame.size.width/2 - frame.size.width/2; 
      frame.origin.y = self.frame.origin.y + self.frame.size.height - frame.size.height; 
      indicator.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; 
      break; 

     case UIViewContentModeTopLeft: 
      frame.origin.x = self.frame.origin.x; 
      frame.origin.y = self.frame.origin.y; 
      indicator.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin; 
      break; 

     case UIViewContentModeTopRight: 
      frame.origin.x = self.frame.origin.x + self.frame.size.width - frame.size.width; 
      frame.origin.y = self.frame.origin.y; 
      indicator.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin; 
      break; 

     case UIViewContentModeBottomLeft: 
      frame.origin.x = self.frame.origin.x; 
      frame.origin.y = self.frame.origin.y + self.frame.size.height - frame.size.height; 
      indicator.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin; 
      break; 

     case UIViewContentModeBottomRight: 
      frame.origin.x = self.frame.origin.x + self.frame.size.width - frame.size.width; 
      frame.origin.y = self.frame.origin.y + self.frame.size.height - frame.size.height; 
      indicator.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin; 
      break; 

     case UIViewContentModeCenter: 
      frame.origin.x = self.frame.origin.x + self.frame.size.width/2 - frame.size.width/2; 
      frame.origin.y = self.frame.origin.y + self.frame.size.height/2 - frame.size.height/2; 
      indicator.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin; 
      break; 

     default: 
      frame.origin.x = self.frame.origin.x + self.frame.size.width/2 - frame.size.width/2; 
      frame.origin.y = self.frame.origin.y + self.frame.size.height/2 - frame.size.height/2; 
      indicator.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin; 
      break; 
    } 

    indicator.frame = frame; 
} 

- (void)hideProcessIndicator 
{ 
    UIActivityIndicatorView *indicator = [[UIViewsIndicatorsPull instance] indicatorForViewObject:self]; 

    [indicator stopAnimating]; 
    [indicator removeFromSuperview]; 

    self.alpha = 1.0; 
} 

- (BOOL)isProcessIndicatorPresented 
{ 
    return self.alpha == 0; 
} 



@end 

添加此文件到項目中,然後調用細胞:

[cell.contentView showProcessIndicator]; 
[cell.contentView showProcessIndicatorWithAlignment:UIViewContentModeCenter]; 
[cell.contentView showProcessIndicatorWithAlignment:UIViewContentModeCenter style:UIActivityIndicatorViewStyleGray]; 

[cell.contentView hideProcessIndicator]; 
0

子類表視圖是好的。這裏有一個加載指示器...

-(void)addLoadingOverlay 
{ 
    self.overlayView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds]; 
    self.overlayView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]; 
    self.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
    self.activityIndicator.center = self.overlayView.center; 
    [self.overlayView addSubview:self.activityIndicator]; 
    [self.activityIndicator startAnimating]; 
    [self.navController.view addSubview:self.overlayView]; 
} 

爲uview和活動指示燈做出選擇。通過做這樣的事情來刪除它。

[self.activityIndicator stopAnimating]; 
[self.overlayView removeFromSuperview]; 
[self.tableView reloadData]; 
相關問題