iOS-首次点击 UITableView 单元时事件指示器未启动

标签 ios uitableview uiactivityindicatorview

我是初学者,实际上正在显示带有单元格的表格 View ,当我们点击表格 View 单元格时,就会发生解析并且在解析事件指示器动画期间。但是,当我尝试这样做时,第一次点击事件指示器不会显示,第一次点击后,当我选择另一个单元格时,它就会开始工作。为什么会出现这种情况?我正在 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];
       }
}

最佳答案

在此处输入代码首先为什么要if一切?在每个 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];
        });
    });

}

关于iOS-首次点击 UITableView 单元时事件指示器未启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20741642/

相关文章:

IOS 应用程序崩溃 : [__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array

ios - 如何使用 UNNotificationPresentationOptions

swift - 使用 Kingfisher,图像无法在 UITableView 中正确加载

swift - 如何实现单例微调器(事件指示器)?

ios - webview 加载完成后,事件指示器不会停止

objective-c - 为什么在我的 Objective-C 代码中间接调用时此 C 数组为 NULL? (cocos2d)

ios - 从照片中提取 GPS 数据

ios - UITableView subview 不包含 UITableViewWrapperView iOS 11

如果超出父 View 边界,iOS7 的 subview 将被修剪

ios - 事件指示器 View 未显示