我正在尝试将searchDisplayController放入我的tableviewController中。它看起来效果很好,但当我点击tableViewCell并试图转向detailViewController时,应用程序崩溃了。
如果我不尝试执行搜索,它只会崩溃。如果我点击其中一个过滤结果,它会将我细分到detailViewController。为什么这可能不起作用的任何想法?谢谢!
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath {
[self performSegueWithIdentifier:@"showBusinessDetails" sender:indexPath];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"showBusinessDetails"]) {
ProfileViewController *destination = [segue destinationViewController];
NSIndexPath * indexPath = (NSIndexPath*)sender;
if (self.tableView == self.searchDisplayController.searchResultsTableView) {
destination.title = [[businesses objectAtIndex:indexPath.row]
valueForKey:@"name"];
destination.businessDetails = [businesses objectAtIndex:indexPath.row];
}
else {
destination.title = [[filteredBusinesses objectAtIndex:indexPath.row]
valueForKey:@"name"];
destination.businessDetails = [filteredBusinesses
objectAtIndex:indexPath.row];
}
}
}
答案 0 :(得分:0)
看起来你的if语句倒退了。如果表视图是searchResultsTableView,那么你应该从filteredBusinesses(而不是business)获取businessDetails,不是吗?
答案 1 :(得分:0)
您应该在UITableViewController
委托方法tableView:didSelectRowAtIndexPath:
中询问有关哪个是当前有效的表格视图的类似问题。
正如您所写,此委托方法只能回复self.tableView
,但您也希望它能回复self.searchDisplayController.searchResultsTableView
。
要使其能够回复self.searchDisplayController.searchResultsTableView
,您应该包含与此类似的代码......
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == self.searchDisplayController.searchResultsTableView) {
UIViewController *destinationVC = nil;
SegueList *segueToList = nil;
NSString *mainStoryboard = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UIMainStoryboardFile"];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:mainStoryboard bundle:nil];
if ([self.entity isEqualToString:self.localisedEntityDocument]) {
destinationVC = [storyboard instantiateViewControllerWithIdentifier:@"id_ViewControllerExample1"]; //insert appropriate "ID"
segueToList = [[SegueList alloc] initWithIdentifier:@"segueView"
source:self
destination:destinationVC];
} else if ([self.entity isEqualToString:self.localisedEntityTransmittal]) {
destinationVC = [storyboard instantiateViewControllerWithIdentifier:@"id_ViewControllerExample2"]; //insert appropriate "ID"
segueToList = [[SegueList alloc] initWithIdentifier:@"segueView"
source:self
destination:destinationVC];
} else {
//error logging
}
UITableViewCell *cellSegue = [tableView cellForRowAtIndexPath:indexPath];
[self prepareForSegue:segueToList sender:cellSegue];
[segueToList perform];
} else {
//Probably nothing here if you are using a storyboard segues.
}
}
请注意,您需要将视图控制器ID输入(或应用)到故事板中的目标视图控制器。
您还需要编写自定义segue。我更喜欢将我的应用放在TVC课程的顶部,使用与此相似的代码......
#pragma mark - CUSTOM SEGUE
#pragma mark
////////////////////////////////////////////////////////////
/// Subclass of UIStoryboardSegue must override -perform ///
////////////////////////////////////////////////////////////
@interface SegueList : UIStoryboardSegue
@end
@implementation SegueList
- (void)perform {
DD_TVC_List *sourceViewController = self.sourceViewController;
[sourceViewController.navigationController pushViewController:self.destinationViewController animated:YES];
}
@end
////////////////////////////////////////////////////////////
/// END of subclass of UIStoryboardSegue ///
////////////////////////////////////////////////////////////
@interface //normal TVC implementation file
最后,在您的TVC prepareForSegue
方法中包含相应的代码,以便在您的委托方法segueView
中响应segue标识符(在我的示例代码tableView:didSelectRowAtIndexPath:
中)。