首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SearchDisplayController TableView段问题

SearchDisplayController TableView段问题
EN

Stack Overflow用户
提问于 2014-08-31 11:53:23
回答 2查看 69关注 0票数 0

我正在尝试将一个searchDisplayController放入我的tableviewController中。它似乎工作得很好,但当我点击tableViewCell并试图切换到detailViewController时,应用程序崩溃了。

只有当我不尝试执行搜索时,它才会崩溃。如果我点击其中一个过滤的结果,它会让我很好地进入detailViewController。有什么想法可以解释为什么这可能不起作用吗?谢谢!

代码语言:javascript
复制
- (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];
        }
    }
}
EN

回答 2

Stack Overflow用户

发布于 2014-08-31 13:03:42

看起来你的if语句倒过来了。如果表视图是searchResultsTableView,那么您应该从filteredBusinesses (而不是企业)获取businessDetails,不是吗?

票数 0
EN

Stack Overflow用户

发布于 2014-09-02 14:39:25

您应该会问一个类似的问题,即在您的UITableViewController委托方法tableView:didSelectRowAtIndexPath:中哪个是当前活动的表视图。

正如您所写的,这个委托方法只能响应self.tableView,但是您也希望它响应self.searchDisplayController.searchResultsTableView

代码语言:javascript
复制
- (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。

我更喜欢将我的放在需要它们的TVC类的顶部,使用类似于下面的代码...

代码语言:javascript
复制
#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方法中包含适当的代码,以响应委托方法tableView:didSelectRowAtIndexPath:中的段标识符(在我的示例代码segueView中)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25588790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档