【问题标题】:How to show data with Search Bar and Search Display Controller in other View Controller如何在其他视图控制器中使用搜索栏和搜索显示控制器显示数据
【发布时间】:2014-03-15 09:30:00
【问题描述】:

提前致谢。

我正在尝试创建一个包含名称、图片和数字的数组的应用程序。我已经用相应数组中的所有数据创建了表格视图;我已经让搜索栏工作并显示正确的搜索。我缺少的是在另一个视图控制器中显示搜索栏的结果。 (我已经使用表格视图进行了此操作:我选择了一个名称,然后它会打开另一个视图,然后显示图片和数字等,)我只是错过了同样的东西,但是当我进行搜索时。

这是我的代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *simpleTableIdentifier = @"DreamCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    if (tableView == self.tableView) {
       cell.textLabel.text = [Names objectAtIndex:indexPath.row];
        cell.imageView.image =[UIImage imageNamed:[Images objectAtIndex:indexPath.row]];
    } else{
        cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.searchDisplayController.isActive) {
       [self performSegueWithIdentifier:@"ShowDreamsDetails" sender:self];
    }
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender  {
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    if ([segue.identifier isEqualToString:@"ShowDreamsDetails"])  {
        NSString *object =nil;
        //  NSIndexPath *indexPath =nil;

        if (self.searchDisplayController.isActive) {
            indexPath =[[self.searchDisplayController searchResultsTableView] indexPathForSelectedRow];
            object = [searchResults objectAtIndex:indexPath.row];

            /* *Here is where I have the problem, If I leave it like thi,s it only shows         the first row in my array; I needed to send the correct name and details to the other View Controller.   */

            DesciptionViewController *desViewController = segue.destinationViewController;
            desViewController.PrincipalName = [Names objectAtIndex:indexPath.row ];
            desViewController.PrincipalDescription = [Description objectAtIndex:indexPath.row];
            desViewController.PrincipalNumber = [Numeros objectAtIndex:indexPath.row];
            desViewController.Images1 = [UIImage imageNamed: [Images objectAtIndex: indexPath.row]];  
        } else {
            DesciptionViewController *desViewController = segue.destinationViewController;
            desViewController.PrincipalName = [Names objectAtIndex:indexPath.row];
            desViewController.PrincipalDescription = [Description objectAtIndex:indexPath.row];
            desViewController.PrincipalNumber = [Numeros objectAtIndex:indexPath.row];
            desViewController.Images1 = [UIImage imageNamed: [Images objectAtIndex: indexPath.row]];

        }
    }
}

提前感谢任何人,如果这是一个重复的问题,我很抱歉;我试图四处寻找,但没有找到任何东西。

【问题讨论】:

    标签: ios objective-c xcode uitableview searchbar


    【解决方案1】:

    从这段代码中并不清楚你的 searchResults 数组中存储了什么。通常这将是表示 Principal 的对象,它具有 Name、Description、Numeros 和 Image 的属性,但您显然将它们保存在单独的数组中。

    不知何故,当您进行搜索并显示结果时,您正在访问这些数组;你需要在这里重新创建你在那里所做的。换句话说,您知道您有搜索结果#row,现在您需要将它(我猜是通过 searchResults)映射到原始数组中的正确行。

    也就是说,请贴出tableView:cellForRowAtIndexPath的代码

    编辑:在 cellForRowAtIndexPath 的代码之后:添加

    好的,所以您的 searchResults 中只有名称,这意味着您必须找到其他数据。

    同样,创建一个新的对象类型 Principal 是最简单和最好的,它具有 Name、Description、Numeros 和 Image 属性,然后您的主数组和 searchResults 将不是字符串,而是 Principals,您可以轻松处理向您的视图控制器提供适当的信息。 (事实上​​,您的子视图控制器可能有一个属性 Principal *person,您可以从主数组或搜索数组中为其分配适当的 Principal。

    但是...如果您想修补它并使其工作,您>可以

    因此您的 prepareForSegue 更改为:

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender  {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        if ([segue.identifier isEqualToString:@"ShowDreamsDetails"])  {
            NSInteger realRow = indexPath.row;
            if (self.searchDisplayController.isActive) {
                indexPath =[[self.searchDisplayController searchResultsTableView] indexPathForSelectedRow];
                NSString * name = (NSString *) [searchResults objectAtIndex:indexPath.row];
                realRow = [Names indexOfObject:name];
            }
            if (realRow != NSNotFound) {
                DesciptionViewController *desViewController = segue.destinationViewController;
                desViewController.PrincipalName = [Name objectAtIndex: row];
                desViewController.PrincipalDescription = [Description objectAtIndex: row];
                desViewController.PrincipalNumber = [Numeros objectAtIndex: row];
                desViewController.Images1 = [UIImage imageNamed: [Images objectAtIndex: row]];
            } else {
                NSLog(@"Well, this is very odd; an invalid indexPath or a name in searchResults that's not in Names");
            }
        }
    }
    

    但同样,最好定义一个 Principals 对象并将其放在两个数组中。

    【讨论】:

    • 我发布了您要求 mackworth 的代码。谢谢
    • 非常感谢 MackWorth ,它真的对我的解释很有帮助,在我用你给我的代码尝试它之后唯一的事情是我最后没有使用 row,所以我尝试了真正的 Row 并且它起作用了。”desViewController.PrincipalDescription = [Description objectAtIndex: “row”]; 谢谢。我会尝试使用您建议的主要变量来学习,我在网上看到示例但不能很好地理解它。谢谢.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-09
    • 1970-01-01
    相关资源
    最近更新 更多