我有一个我不明白的非常奇怪的问题。
我有一个UITableView
,当我点击一行时会导致崩溃。
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.delegate = self; <-- If I remove this, no crash caused
self.tableView.dataSource = self;
有趣的是,我didSelectRowAtIndexPath
内部根本没有代码,但仍然崩溃了?
日志并没有说太多,我得到(lldb)
但是当我看得更远时,我从调试器得到了这个。
[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:]
是什么原因?我不明白如何解决这个问题。
Thread 1, Queue : com.apple.main-thread
#0 0x02b1909f in objc_msgSend ()
#1 0x01afb285 in -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] ()
#2 0x01afb4ed in -[UITableView _userSelectRowAtPendingSelectionIndexPath:] ()
#3 0x025055b3 in __NSFireDelayedPerform ()
#4 0x034f9376 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ ()
#5 0x034f8e06 in __CFRunLoopDoTimer ()
#6 0x034e0a82 in __CFRunLoopRun ()
#7 0x034dff44 in CFRunLoopRunSpecific ()
#8 0x034dfe1b in CFRunLoopRunInMode ()
#9 0x03b387e3 in GSEventRunModal ()
#10 0x03b38668 in GSEventRun ()
#11 0x01a4bffc in UIApplicationMain ()
#12 0x000125bd in main at /Users/damianmendez/dev/trigd/jagodu-ios/Jagodu/Jagodu/main.m:16
#13 0x03210725 in start ()
有人有什么建议吗? 感谢。
编辑: 在像@Phillip Millis这样的已启用僵尸告诉我之后,我得到了这个:
*** -[RefineSearchViewController tableView:didSelectRowAtIndexPath:]: message sent to deallocated instance 0x9d24030
更新:
添加RefineSearchViewController (which has a UITableView)
RefineSearchViewController *refineSearchController = [[RefineSearchViewController alloc] initWithTransparentViews];
refineSearchController.parentDelegate = self.parentDelegate;
CGRect searchFrame = refineSearchController.view.frame;
searchFrame.origin.y = titleLabel.frame.origin.y + titleLabel.frame.size.height + 5;
refineSearchController.view.frame = searchFrame;
[self.refineSearchContainer addSubview:refineSearchController.view];
[self.view addSubview:self.refineSearchContainer];
启动代码RefineSearchViewController
-(RefineSearchViewController*)init {
self = [super init];
if (self) {
JoDModel *model = [JoDModel defaultModel];
self.title = @"Search profile";
_propertyNames = model.searchProfilePropertyNames;
_properties = model.searchProfileProperties;
_doneInvocation = nil;
_isRefineSearch = NO;
_transparentViews = NO;
}
return self;
}
-(RefineSearchViewController*)initWithTransparentViews {
self = [self init];
if (self) {
_transparentViews = YES;
}
return self;
}
-(void)viewDidLoad {
[super viewDidLoad];
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.titleShadow.hidden = !_addShadow;
JoDModel *model = [JoDModel defaultModel];
UIBarButtonItem *doneButton;
if (_isRefineSearch) {
doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Search" style:UIBarButtonItemStylePlain target:self action:@selector(done)];
_keyboardBg = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, model.keyboardHeight)];
_keyboardBg.backgroundColor = [UIColor blackColor];
[self.view addSubview:_keyboardBg];
} else {
doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Search" style:UIBarButtonItemStylePlain target:self action:@selector(performSearch)];
}
self.navigationItem.rightBarButtonItem = doneButton;
// When entering this view controller, the user will probably make a new search soon, therefore it's important to update the geo location
if (model.shareMyLocation) {
JoDAppDelegate *appDelegate = (JoDAppDelegate*)[UIApplication sharedApplication].delegate;
[appDelegate updateLocation];
}
}
-(void)dealloc {
self.tableView = nil;
_propertyNames = nil;
_properties = nil;
_doneInvocation = nil;
_titleShadow = nil;
_keyboardBg = nil;
}
答案 0 :(得分:2)
您正在创建RefineSearchViewController
作为本地变量。假设您的项目使用ARC,那么当该引用超出范围(创建它的方法的结尾)时,它将被释放。
在视图控制器中创建一个强大的属性,然后创建它并分配给它。
答案 1 :(得分:1)
在粘贴最后一个代码后,您的应用程序崩溃是正常的。 实际上你应该保留对新VC(RefineSearchViewController)的引用。要显示其内容,请执行
[self.refineSearchContainer addSubview:refineSearchController.view];
[self.view addSubview:self.refineSearchContainer];
但VC失败了。您可以使用此方法使用容器View控制器机制
- (void)addChildViewController:(UIViewController *)childController
或者更容易使用pushViewController:
的NavigationController,具体取决于您想要做什么。