Evgeniy Kleban
Evgeniy Kleban

Reputation: 6940

Selecting row in table cause crash

Please consider following code:

-(void)selectSpecificRowForSpotlight:(NSNumber*)row{
   // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{

        /* Fill news list */

        if (self.singlObj.getSpotlightArray.count > 1){

            if (self.viewModel.arrValues.count > 1){

                NSLog(@"we got spotlight");
                [self.viewModel.arrValues insertObject:[[self.singlObj.spotArray    objectAtIndex:self.singlObj.currentSpotlightIndex-1] objectAtIndex:0] atIndex:0];
                [self.viewModel.arrValues insertObject:[[self.singlObj.spotArray    objectAtIndex:self.singlObj.currentSpotlightIndex-1] objectAtIndex:1] atIndex:1];
                [self.viewModel.arrValues insertObject:[[self.singlObj.spotArray    objectAtIndex:self.singlObj.currentSpotlightIndex-1] objectAtIndex:2] atIndex:2];
            }
        }
         [self.myTableView reloadData];
 });

    NSIndexPath *path = [NSIndexPath indexPathForRow:[row integerValue] inSection:0];
    [self.myTableView.delegate tableView:self.myTableView didSelectRowAtIndexPath:path];
}

Details not really matter, the problem is, following lines:

 NSIndexPath *path = [NSIndexPath indexPathForRow:[row integerValue] inSection:0];
    [self.myTableView.delegate tableView:self.myTableView didSelectRowAtIndexPath:path];

cause an crash (SIGABRT error). I fix it with:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{

But of course it is not good to call a function by timer, without promise. Why call to delegate method cause crash? How to avoid it without using dispatch_after call?

Upvotes: 0

Views: 180

Answers (1)

vadian
vadian

Reputation: 285059

NEVER EVER call delegate methods starting with or including will, should and did directly. They are called exclusively by the target class.

To select a row there is a method in UITableView class

- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath
                    animated:(BOOL)animated
              scrollPosition:(UITableViewScrollPosition)scrollPosition 

Upvotes: 2

Related Questions