我是初学者,实际上正在显示带有单元格的表格 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/