Search code examples
iosuitableviewios6ios7

iOS - didSelectRowAtIndexPath causes crash in app


I have a very strange problem that I don't understand. I have a UITableView which causes a crash when I click on a row.

self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.delegate = self; <-- If I remove this, no crash caused
self.tableView.dataSource = self;

The funny thing is that I have no code at all inside didSelectRowAtIndexPath, and is still crashes? The log doesn't say much, I get (lldb) but when looking a bit further, I got this from debugger.

[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] is the cause? I dont understand how to solve this problem.

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 ()

Anyone has any suggestions? Thanks.

EDIT: After enabled zombies like @Phillip Millis told me too, I got this:

*** -[RefineSearchViewController tableView:didSelectRowAtIndexPath:]: message sent to deallocated instance 0x9d24030

UPDATE:

Code for adding 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];

Code for initing 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;
}

Solution

  • You are creating the RefineSearchViewController as a local variable. Assuming your project uses ARC, it will then be deallocated when that reference goes out of scope (end of the method that created it).

    Make a strong property in the view controller that creates it and assign to that instead.