2015-09-07 114 views
0

我已經實現拉到刷新在我的應用程序,但它崩潰時,我嘗試滾動,而它仍然刷新。這裏是我使用的代碼: 僅供參考,我在刷新時調用webServices。拉到刷新崩潰的應用程序,如果我嘗試滾動時刷新

在viewDidLoad中:

UIRefreshControl *refresh = [[UIRefreshControl alloc] init]; 
    refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to Refresh"]; 
    [refresh addTarget:self action:@selector(callWebService:) forControlEvents:UIControlEventValueChanged]; 
    self.refreshControl = refresh; 
    [self callWebService:refresh]; 

的刷新方法:

-(void)callWebService:(UIRefreshControl *)refresh 
{ 
    static BOOL refreshInProgress = NO; 
    MessageWebServices *messageWebService = [[MessageWebServices alloc]init]; 

    MessageBO *tempBO = [[MessageBO alloc]init]; 

    if(self.flag) 
    { 
     self.navigationItem.title = @"My Posts"; 
     tempBO.projectId = [AppConstants getProjectId]; 
     tempBO.customerId =[AppConstants getCustomerId]; 

    } 
    else 
    { 
     tempBO.projectId = [AppConstants getProjectId]; 
    } 


    self.messageStore = [[NSMutableArray alloc] init]; 

    if (!refreshInProgress) 
    { 
     refreshInProgress = YES; 

     refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Refreshing"]; 
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

      self.messageStore = [messageWebService getMessageList:tempBO]; 

      dispatch_async(dispatch_get_main_queue(), ^{ 
       [refresh beginRefreshing]; 
       [self.tableView reloadData]; 
       [refresh endRefreshing]; 
       refreshInProgress = NO; 
      }); 
     }); 
    } 

} 

請幫助!

+0

你的編碼是否正確檢查一次self.messageStore值是否被添加 –

+0

謝謝,夥計。我意識到我正在初始化陣列。我刪除了該行,現在可以運行 – sam24

回答

0

我從來沒有用過beginRefreshing的方法。您可以輕鬆地將刷新控制添加到您的tableView。

#pragma mark Refresh Method 

-(void)startRefreshing { 
    if (!self.refreshControl) { 
     // Initialize the refresh control. 
     self.refreshControl = [[UIRefreshControl alloc] init]; 
     self.refreshControl.backgroundColor = CRREFRESHBGCOLOR; 
     self.refreshControl.tintColor = [UIColor whiteColor]; 
     [self.refreshControl addTarget:self action:@selector(callWebservice) forControlEvents:UIControlEventValueChanged]; 
     [yourTable addSubview:self.refreshControl];//add refresh control to your table 
    } 
} 

-(void)endRefreshing { 
    if (self.refreshControl) { 
     NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
     [formatter setDateFormat:@"MMM d, h:mm a"]; 
     NSString *title = [NSString stringWithFormat:@"Last update: %@", [formatter stringFromDate:[NSDate date]]]; 
     NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:[UIColor whiteColor] 
                    forKey:NSForegroundColorAttributeName]; 
     NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:title attributes:attrsDictionary]; 
     self.refreshControl.attributedTitle = attributedTitle; 

     [self.refreshControl endRefreshing]; 
    } 
} 

呼叫後/整理Web服務方法調用[self endRefreshing];方法並重新加載表[yourTable reloadData]