DNB5brims
DNB5brims

Reputation: 30608

Why I can't add a loading spinner when I click the table cell?

Here is the source code:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  [self loadingWithBackground]; //this view called the spinner

  return indexPath;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  [tableView deselectRowAtIndexPath:indexPath animated:YES];

  [self performSelectorOnMainThread:@selector(performIndexPath:) 
                         withObject:indexPath 
                      waitUntilDone:YES];
}

-(void)performIndexPath:(NSIndexPath *)aIndexPath{

//application logic, flip the view over, and display the data
}

But it works perfectly right:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
          [tableView deselectRowAtIndexPath:indexPath animated:YES];
          [self loadingWithBackground];
          return;

        //bababbaa
}

The code of loadingWithBackground:

-(void)loadingWithBackground{
  [self performSelectorOnMainThread:@selector(performLoadingWithBackground) 
                         withObject:NULL
                      waitUntilDone:YES];

  [m_oUIActivityIndicatorView startAnimating];
}
-(void)performLoadingWithBackground{
  self.m_isLoadFinished = NO;

  if(m_oUIActivityIndicatorView != NULL){
    [m_oUIActivityIndicatorView removeFromSuperview];
    m_oUIActivityIndicatorView = NULL;
  }

  if(m_oUIActivityIndicatorBackgroundView != NULL){
    [m_oUIActivityIndicatorBackgroundView removeFromSuperview];
    m_oUIActivityIndicatorBackgroundView = NULL;
  }

  m_oUIActivityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  [m_oUIActivityIndicatorView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
  m_oUIActivityIndicatorBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, m_oUIActivityIndicatorView.frame.size.width, m_oUIActivityIndicatorView.frame.size.height)];
  [m_oUIActivityIndicatorBackgroundView setBackgroundColor:[UIColor grayColor]]; //I love the black one
  [m_oUIActivityIndicatorBackgroundView setAlpha:0.8];
  [m_oUIActivityIndicatorBackgroundView setCenter:CGPointMake(self.view.frame.size.width/2.0, self.view.frame.size.height/2.0)];
  m_oUIActivityIndicatorBackgroundView.layer.cornerRadius = 10;
  [m_oUIActivityIndicatorBackgroundView addSubview:m_oUIActivityIndicatorView];
  m_oUIActivityIndicatorView.hidden = NO;
  [self.view addSubview:m_oUIActivityIndicatorBackgroundView]; 
}

I tried to perform the loadingWithBackground using main thread, but it also can't show. When I copy the method: loadingWithBackground in the didSelectRowAtIndexPath, it doesn't shows the loadingWithBackground neither. How can I achieve it? Thanks.

Upvotes: 1

Views: 254

Answers (2)

Rohit Wankhede
Rohit Wankhede

Reputation: 506

NSInteger SelectedIndex;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  SelectedIndex = indexPath.row;
  // Start animating activity indicator
  [self performSelectorOnMainThread:@selector(performIndexPath) withObject:nil waitUntilDone:NO];
  // OR USING TIMER 
  // timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(performIndexPath) userInfo:nil repeats:NO];
}

-(void)performIndexPath {
  //stop activity indicator
}

Upvotes: 4

Novarg
Novarg

Reputation: 7450

A couple of things I'd try:

  • Move [m_oUIActivityIndicatorView startAnimating]; to the end of the performLoadingWithBackground method and call that method instead of loadingWithBackground. Seems to be pretty much the same.

  • Add [self.view bringSubviewToFront:m_oUIActivityIndicatorBackgroundView]; after [self.view addSubview:m_oUIActivityIndicatorBackgroundView];. Not sure if it will help, but I always do it

  • Debugging

Hope it helps

Upvotes: 0

Related Questions