请考虑以下代码:
-(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];
}
细节并不重要,问题是,按照以下几行:
NSIndexPath *path = [NSIndexPath indexPathForRow:[row integerValue] inSection:0];
[self.myTableView.delegate tableView:self.myTableView didSelectRowAtIndexPath:path];
导致崩溃(SIGABRT错误)。我修复它:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
但当然,没有承诺,通过计时器调用函数是不好的。为什么调用委托方法导致崩溃?如何在不使用dispatch_after调用的情况下避免使用它?
答案 0 :(得分:2)
永远不会直接调用以will
,should
和did
开头或包含的委托方法。它们由目标类专门调用。
要选择行,UITableView
类
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath
animated:(BOOL)animated
scrollPosition:(UITableViewScrollPosition)scrollPosition