搜索栏和搜索显示控制器与segue

时间:2012-07-03 20:43:39

标签: segue

点击搜索表格单元格时从未调用

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"Show Detail"])
    {
        Player *player = [self.fetchedResultsController objectAtIndexPath:[self.tableView indexPathForSelectedRow]];
        [segue.destinationViewController setPlayer:player];
    }
}

这会正确过滤列表,但在点击搜索表格单元格时,永远不会调用prepareForSegue。

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

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

    if (tableView == self.tableView) 
    {
        // normal table view population
        Player *player = [self.fetchedResultsController objectAtIndexPath:indexPath];
        cell.textLabel.text = [[NSString alloc] initWithFormat:@"#%@ %@ %@", player.number, player.firstName, player.lastName];
        cell.detailTextLabel.text = [[NSString alloc] initWithFormat:@"%@", player.position];
        [cell.imageView setImageWithURL:[NSURL URLWithString:player.photo] placeholderImage:[UIImage imageNamed:@"playerplaceholder.jpg"]];

    }
    else if(tableView == self.searchDisplayController.searchResultsTableView)
    {
        // search view population
        Player *player = [self.filteredFetchedResultsController objectAtIndexPath:indexPath];
        cell.textLabel.text = [[NSString alloc] initWithFormat:@"#%@ %@ %@", player.number, player.firstName, player.lastName];
        cell.detailTextLabel.text = [[NSString alloc] initWithFormat:@"%@", player.position];
        [cell.imageView setImageWithURL:[NSURL URLWithString:player.photo] placeholderImage:[UIImage imageNamed:@"playerplaceholder.jpg"]];

    }

    return cell;
}

1 个答案:

答案 0 :(得分:1)

你需要把IF检查搜索是否有结果。 就像在这段代码中一样:

if ([[segue identifier]isEqualToString:@"ShowDetails"])
    {
        ShowDetailsViewController *sdvc = (ShowDetailsViewController *)[segue destinationViewController];
        NSIndexPath *indexPath = [[self tableView] indexPathForSelectedRow];

        Books *selectedBooks = nil;
        if(self.searchDisplayController.active)
            selectedBooks = (Books *)[[self searchResults]objectAtIndex:[[[[self searchDisplayController]searchResultsTableView]indexPathForSelectedRow]row]];
        else
            selectedBooks = (Books *)[[self fetchResultsController] objectAtIndexPath:indexPath];

        sdvc.currentBooks = selectedBooks;
    }