【问题标题】:iOS- Activity indicator not starting on first tap of UITableView celliOS-第一次点击 UITableView 单元格时未启动活动指示器
【发布时间】:2013-12-23 10:32:39
【问题描述】:

我是一个初学者,我实际上是在显示一个带有单元格的表格视图,当我们点击表格视图单元格时,会发生解析并且在解析活动指示器动画期间。但是当我尝试这样做时,第一次点击活动指示器没有显示,当我选择另一个单元格时第一次点击后它开始工作。为什么会发生这种情况?我在 didSelectRowAtIndexPath 中编写我的代码。这里 [self xmlParsing] 是我的解析函数。那么如何解决这个问题呢?请帮忙。

-(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];
       }
}

【问题讨论】:

    标签: ios uitableview uiactivityindicatorview


    【解决方案1】:

    enter code hereFirstable 为什么你if 什么都有?在每一个如果你有相同的一组方法调用。

    如果在你的方法开始时,你就有了可重用性:

    if(cell.accessoryView==nil)
    

    我认为这是一个错误。

    最后你在其他线程上触发动画,你不能这样做 - 总是调用主线程进行动画:

    所以你的方法应该是这样的:

    -(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];
           }
    }
    

    现在让我们关注线程安全动画。在上述方法中“通常”调用您的方法,例如 [self threadStartAnimating:self];。然后修改如下:

    -(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];
            });
        });
    
    }
    

    【讨论】:

      【解决方案2】:

      Apple 明确禁止开发人员在后台线程上更新用户界面元素。每个应用程序都有一个主 UI 线程,并且所有 UI 更新都必须在该线程上执行。我怀疑您没有在主线程上为活动指示器设置动画。

      你在哪里打电话给[activityIndicator startAnimating]?如果您在threadStartAnimating:(您在后台线程上运行)调用它,那么这很可能是问题所在。相反,尝试在将其添加到视图后直接对其进行动画处理,如下所示:

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

      编辑:一个完整​​的 if 语句如下所示:

        if (indexPath.row==0)
          {
              [parseArray removeAllObjects];
              [self.view addSubview:activityIndicator];
              [activityIndicator startAnimating];
              [self xmlParsing];
              [activityIndicator stopAnimating];
         }
      

      【讨论】:

      • threadStartAnimating:是一个包含[activityIndi​​cator startAnimating]的函数;
      • 喜欢这个 - (void) threadStartAnimating:(id)data { [activityIndi​​cator startAnimating]; }
      • 好的,你明白为什么这不起作用了吗?您正在尝试在后台线程上为 activityInidicator 设置动画,因为您正在执行 [NSThread detachNewThreadSelector:@selector(threadStartAnimating:... 您是否尝试了我的建议?
      • 好的。同意。但即使我尝试按照您的建议做同样的事情,它也没有奏效。得到了同样的结果。 :(
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-14
      • 2012-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多