質問

I am a beginner and I am actually displaying a tableview with cells, as we tap on table view cell, parsing takes place and during parsing activity indicator animates. But when I try to do that, at the first tap activity indicator is not shown, after first tap when I select another cell then it starts working. Why this happens? I am writing my code in didSelectRowAtIndexPath. Here [self xmlParsing] is my parsing function. So how to solve this problem ? Kindly help.

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell= [tableView cellForRowAtIndexPath:indexPath];

    if(cell.accessoryView==nil)
  if (indexPath.row==0)
        {
            [parseArray removeAllObjects];
            [self.view addSubview:activityIndicator];
            [NSThread detachNewThreadSelector:@selector(threadStartAnimating:) toTarget:self withObject:nil];
            [self xmlParsing];
            [activityIndicator stopAnimating];
       }
   if (indexPath.row==1)
        {
            [parseArray removeAllObjects];
            [self.view addSubview:activityIndicator];
            [NSThread detachNewThreadSelector:@selector(threadStartAnimating:) toTarget:self withObject:nil];
            [self xmlParsing];
            [activityIndicator stopAnimating];
       }
   if (indexPath.row==2)
        {
            [parseArray removeAllObjects];
            [self.view addSubview:activityIndicator];
            [NSThread detachNewThreadSelector:@selector(threadStartAnimating:) toTarget:self withObject:nil];
            [self xmlParsing];
            [activityIndicator stopAnimating];
       }
}
役に立ちましたか?

解決

enter code hereFirstable why you if everything? In every if you have the same set of method call.

Then you have leave reusability if in the beginning of your method:

if(cell.accessoryView==nil)

I assume that is a mistake.

And finally you fire the animation on some other thread, you cannot do that - always call main thread for animation:

So your method should look like:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
            [parseArray removeAllObjects];
            [self.view addSubview:activityIndicator];

           // [NSThread detachNewThreadSelector:@selector(threadStartAnimating:) toTarget:self withObject:nil]; // this line have to be rewrite. I write solution below.
            [self xmlParsing];
            [activityIndicator stopAnimating];
       }
}

Now let's focus on thread-safety animation. Call your method "normally" like [self threadStartAnimating:self]; in above method. Then modify it like:

-(IBAction) threadStartAnimating:(id)object {

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //do all your task here (task, witch not included pushing or taking something from/to the screen).
        dispatch_async(dispatch_get_main_queue(), ^{
            //here is the place to redraw the screen like [self.view addSubview:mySub];
        });
    });

}

他のヒント

Apple explicitly disallows developers from updating user interface elements on background threads. Each application has one main UI thread and all UI updates must be performed on that thread. I suspect you're not animating the activity indicator on the main thread.

Where are you calling [activityIndicator startAnimating]? If you're calling it in threadStartAnimating: (which you are running on a background thread) then that is most likely the problem. Instead, try animating it directly after adding it to the view like this:

[self.view addSubview:activityIndicator];
[activityIndicator startAnimating];

EDIT: One full if statement would look like this:

  if (indexPath.row==0)
    {
        [parseArray removeAllObjects];
        [self.view addSubview:activityIndicator];
        [activityIndicator startAnimating];
        [self xmlParsing];
        [activityIndicator stopAnimating];
   }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top