iphone - [self.tableView reloadData] 处的 EXC_BAD_ACCESS

标签 iphone objective-c ios uitableview exc-bad-access

我正在制作一个简单的应用程序来进行一些计算。其中一些计算相当复杂,因此可能需要一段时间才能得到结果。我正在使用 UITableView 让用户输入值。然后在单击“计算”按钮后,我制作了一个简单的 UIView,上面有一个 UIActivityIndi​​catorView,并将它放在屏幕中间。然后我在后台调用计算方法。一些代码:

    self.activityIndicatorView = [[UIView alloc]initWithFrame:CGRectMake(320/2 - 50, (480 - 64)/2 - 50, 100, 100)];
    self.activityIndicatorView.layer.cornerRadius = 10.0f;
    self.activityIndicatorView.backgroundColor = [UIColor colorWithRed:0. green:0. blue:0. alpha:0.7];
    UIActivityIndicatorView *actInd = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    actInd.frame = CGRectMake((100 - actInd.frame.size.width) / 2, (100 - actInd.frame.size.height) / 2, actInd.frame.size.width, actInd.frame.size.height);
    [actInd startAnimating];
    [self.activityIndicatorView addSubview:actInd];
    [self.activityIndicatorView bringSubviewToFront:actInd];
    [self.view addSubview:self.activityIndicatorView];
    [self.view bringSubviewToFront:self.activityIndicatorView];
    [self performSelectorInBackground:@selector(calculateAllThatShizzle) withObject:nil];

如您所见,它非常简单。但是,如果键盘仍然可见,它会在到达 -(void)calculateAllThatShizzle 中的 [self.tableView reloadData] 时崩溃(EXC_BAD_ACCESS)日志中的方法:

2012-03-23 11:06:32.418 MyApp[869:5c07] bool _WebTryThreadLock(bool), 0x6adb270: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...
1   WebThreadLock
2   -[UITextRangeImpl isEmpty]
3   -[UITextRange(UITextSelectionAdditions) _isCaret]
4   -[UITextSelectionView setCaretBlinks:]
5   -[UIKeyboardImpl setCaretBlinks:]
6   -[UIKeyboardImpl setDelegate:force:]
7   -[UIKeyboardImpl setDelegate:]
8   -[UIPeripheralHost(UIKitInternal) _reloadInputViewsForResponder:]
9   -[UIResponder _finishResignFirstResponder]
10  -[UIResponder resignFirstResponder]
11  -[UITextField resignFirstResponder]
12  -[UITableView reloadData]
13  -[ChildViewController calculateAllThatShizzle]
14  -[NSThread main]
15  __NSThread__main__
16  _pthread_start
17  thread_start

所以问题是它试图从后台线程用键盘做一些事情。我试过遍历单元格并调用 [cell.rightTextField resignFirstResponder],但没有帮助。我也试过用那个

-(void)reloadTableViewData {
  [self.tableView reloadData];
}
-(void)calculateAllThatShizzle {
  //some code omitted - code that uses UIKit
  if ([dob timeIntervalSinceDate:calcDate]>0) {
            [errorButton setTitle:@"Calculation Date must be more than Date of Birth" forState:UIControlStateNormal];
            errorButton.hidden = NO;
            [self.activityIndicatorView removeFromSuperview];
            self.activityIndicatorView = nil;
            return;
        }
  [self performSelectorOnMainThread:@selector(reloadTableViewData) withObject:nil waitUntilDone:NO];
}

但这也无济于事。我正在使用 ELCTextfieldCell

我们将不胜感激。

谢谢

附言我确实在 reloadData 上检查了一些关于 EXC_BAD_ACCESS 的其他问题,但它没有涵盖我遇到的问题

附言我也可以只在主线程中运行计算,但随后 GUI 变得无响应并且事件指示器不显示

最佳答案

试试这段代码

for(int i=0;i<[tblViewData count];i++)
{
    if([[(ELCTextfieldCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]] rightTextField] isFirstResponder])
    {
        [[(ELCTextfieldCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]] rightTextField] resignFirstResponder];
    }
}

根据您的情况进行更改。

关于iphone - [self.tableView reloadData] 处的 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9837517/

相关文章:

iphone - 互斥锁上的阻塞是否相当于空 while 循环?

ios - 以编程方式创建对象并更改该对象的位置

ios - 是否可以创建一个仅显示网站的 webview 的 ios 应用程序?

ios - Xamarin iOS 本地通知不起作用

objective-c - 检测 Mac OS X 窗口何时调整大小或移动

ios - 在 UITableView 中仅重新加载一个部分标题

iphone - 使用加速度计测量小距离 - iOS

iphone - AVPlayerLayer 进入后台模式时停止播放

ios - WCSession 发送消息在实际设备上不起作用,但在模拟器上起作用

ios - StoryBoard 找不到 ViewController