2014-03-14 42 views
0

在此先感謝。如何使用搜索欄和其他視圖控制器中的搜索顯示控制器顯示數據

我想創建一個具有名稱,圖片和數字的數組的應用程序。我已經使用相應數組中的所有數據創建了表視圖;我已經有搜索欄工作並顯示正確的搜索。我所缺少的是在另一個視圖控制器中顯示搜索欄的結果。 (我已經得到了這個與表視圖工作:我選擇一個名稱,它打開另一個視圖,然後顯示圖片和數字等),我只是錯過了同樣的事情,但當我做搜索。

這是我的代碼。

- (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]]; 

     } 
    } 
} 

在此先感謝任何人,如果這是重複的問題,我很抱歉;我試圖搜索周圍,但我沒有找到任何東西。

回答

0

從代碼中不清楚你的searchResults數組中存儲了什麼。通常情況下,這將是代表Principal的對象,它具有Name,Description,Numeros和Image的屬性,但是您明確地將它們保留在單獨的數組中。

不知何故,當你搜索並顯示結果時,你正在訪問這些數組;你需要在這裏重新創建你在那裏做了什麼。換句話說,你知道你的搜索結果是#row,現在你需要將它映射到原始數組中的正確行(我想通過searchResults)。

換句話說,請張貼的tableView代碼:的cellForRowAtIndexPath

編輯:後的cellForRowAtIndexPath代碼:加入

好了,你只得到了名字在SearchResult所,這意味着你必須找到其他數據。

再一次,創建一個新的對象類型Principal,它具有Name,Description,Numeros和Image屬性,那麼你的主數組和你的searchResults不會是字符串,但是Principals和你可以輕鬆處理向您的視圖控制器提供適當的信息。 (事實上​​,你的子視圖控制器可能有一個Principal * person屬性,你可以從你的主數組或者搜索數組中爲它分配合適的Principal。

但是...如果你想修補這個,讓它工作,你可以在名稱數組中搜索searchResult中所選名稱的名稱(假設沒有兩個人具有相同的名稱,否則它總會找到第一個名稱)。

所以你的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 ob ject並且在你的兩個數組中。

+0

我發佈了你要求的mackworth代碼。謝謝 –

+0

非常感謝你MackWorth,它真的幫助我解釋你的解釋,在我用你給我的代碼嘗試它之後唯一的一件事,那就是我沒有在最後一行工作,所以我用真正的Row來嘗試它,並且它工作。 「desViewController.PrincipalDescription = [說明objectAtIndex:」row「];謝謝,我會盡量學習你所建議的主要變量,我在網上看到了一些例子,但不能很好地理解它,謝謝。 –

相關問題