单击搜索结果时会显示空详细信息视图

时间:2013-12-27 16:23:17

标签: search core-data detail

我有问题。我创建了一个表格视图,其中列出了学生。通过单击相应的学生,将显示相应的详细信息视图。现在我在我的项目中实现了一个包含搜索结果的搜索栏,但每次我使用搜索栏时,都会显示一个空的详细信息视图(没有任何机会选择正确的搜索结果)我真的不知道为什么。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Student Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...

    Student *student = nil;

    if (tableView == self.searchDisplayController.searchResultsTableView)
    {student = [self.searchResults objectAtIndex:indexPath.row];
    NSString *fullname = [NSString stringWithFormat:@"%@ %@ (%@)", student.vorname, student.name, student.hatBetrGrund.name];
    cell.textLabel.text = fullname;
    [self performSegueWithIdentifier:@"Detail Student Seque" sender:student.name]; // if search results are found
    }
    else
    {
    student =[self.fetchedResultsController objectAtIndexPath:indexPath];

    NSString *fullname = [NSString stringWithFormat:@"%@ %@", student.vorname, student.name];
    cell.textLabel.text = fullname;
    cell.detailTextLabel.text = student.hatBetrGrund.name;
    }

   return cell;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"Add Student Segue"])
    {

        AddStudent *addStudent = segue.destinationViewController;
        addStudent.delegate = self;
        addStudent.managedObjectContext = self.managedObjectContext;
    }
    else if ([segue.identifier isEqualToString:@"Detail Student Seque"])
    {
        DetailStudent *detailStudent = segue.destinationViewController;
        detailStudent.delegate = self;
        detailStudent.managedObjectContext = self.managedObjectContext;

        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

        self.selectedStudent = [self.fetchedResultsController objectAtIndexPath:indexPath];

        detailStudent.student = self.selectedStudent;
    }
    else {
        NSLog(@"wow, such Fail!");
    }
}

1 个答案:

答案 0 :(得分:1)

你的代码对我来说很奇怪。为什么在cellForRowAtIndexPath内执行segue?这不是正确的地方。应使用此委托来考虑内容的显示。

执行segue的方法是didSelectRowAtIndexPath。单击一行时,您只需抓取指定的用户(从普通内容或过滤的内容)并执行segue。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath    {
    if(tableView == self.searchDisplayController.searchResultsTableView) {
        // grab the user from the filtered results
    } else {
        // grab the user from the plain results
    }

    // perform the segue here (I guess in both cases you need to display details of the user)
} 

请注意,这些只是提示......