---
title: "How to control multiple arrays when using search in UITableView"  
description: "How to control multiple arrays when using search in UITableView"  
author: "Anonymous User"  
published: 2015-07-24  
updated: 2015-07-24  
canonical: https://www.mindstick.com/forum/23369/how-to-control-multiple-arrays-when-using-search-in-uitableview  
category: "iphone"  
tags: ["iphone", "ios", "objective c"]  
reading_time: 3 minutes  

---

# How to control multiple arrays when using search in UITableView

I have four [array](https://www.mindstick.com/articles/335/jagged-array-in-c-sharp-dot-net) videoName, ActorName, VideoID, ActorID. I combined videoName & ActorName to make a single array "[title](https://www.mindstick.com/articles/12860/how-to-get-financing-for-salvage-title-cars)", and same with VideoID & ActorID to make array "IDs" In short, title = ActorName + videoName IDs = ActorID + VideoID

here is my code,

**Tableview [Methods](https://www.mindstick.com/articles/13060/runny-nose-remedy-methods-that-work-best)**

```
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:                                                                      (NSInteger)section {
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return self.searchResults.count;
    } else {
        return self.title.count;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:                                                                                    (NSIndexPath *)indexPath
{
    static NSString *cellID = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil)
    {         cell = [[UITableViewCell alloc] initWithStyle:                   UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.imageView.frame = CGRectMake(0,0,32,32);
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row];
        cell.imageView.image = [UIImage imageNamed:                                                   [img objectAtIndex:indexPath.row]];
        cell.textLabel.textColor = [UIColor blackColor];
    } else {
        cell.textLabel.text = [self.title objectAtIndex:indexPath.row];
        cell.imageView.image = [UIImage imageNamed:[img objectAtIndex:                                                    indexPath.row]];
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:                                                           (NSIndexPath *)indexPath
{ }
```

**Search Methods**

```
- (void)filterContentForSearchText:(NSString*)searchText scope:              (NSString*)scope {
    NSPredicate *predicate = [NSPredicate predicateWithFormat:                                                  @"SELF beginswith[c] %@", searchText];
    // NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:                                                 @"contains[c] %@", searchText];
    self.searchResults = [self.title filteredArrayUsingPredicate:predicate];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)              controller shouldReloadTableForSearchString:(NSString *)searchString {
    [self filterContentForSearchText:searchString scope:                                        [[self.searchDisplayController.searchBar scopeButtonTitles]
                                        objectAtIndex:[self.searchDisplayController.searchBar
                                        selectedScopeButtonIndex]]];    return YES;
}
```

**[Requirement](https://yourviews.mindstick.com/view/85169/becoming-an-influencer-requirement-of-skills-and-knowledge)**

Now, first i need to get that which row was selected, [actor](https://answers.mindstick.com/qa/116208/rob-reiner-legendary-comedic-actor-and-princess-bride-director-found-dead-in-his-home) or [Video](https://www.mindstick.com/articles/290133/6-necessary-preparations-you-need-before-editing-a-video), secondly actorID or VideoID. It was easy if there was no [search bar](https://www.mindstick.com/forum/315/how-to-achieve-similar-google-search-bar-functionality-in-c), because after the search all rows restored again with new data plus rows are populated from "title" not "IDs" so how can i get IDs when [user](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) [select](https://www.mindstick.com/forum/160534/orderby-then-select-vs-select-then-orderby-performance) the row.

## Replies

### Reply by Tarun Kumar

create an object for this with 3 properties for name and id like,

```
@interface Item : NSObject  @property (nonatomic, strong) NSString *name;@property (nonatomic, strong) NSString *itemId;@property (nonatomic, strong) NSString *imageName; @end
```

then create an array of Item using videoName + videoId and actorName + actorId like,

```
Item *videoItem = [[Item alloc] init];videoItem.name = videoName;videoItem.itemId = videoId;videoItem.imageName = videoImageName;
```

create for all items in videoName array and actor array. Add both this to a common dataArray like,

```
[self.dataArray addObejct:videoItem];
```

and

```
Item *actorItem = [[Item alloc] init];actorItem.name = actorName;actorItem.itemId = actorId;actorItem.imageName = actorImageName;
```

similarly [self.dataArray addObejct:actorItem];

and in cellForRowAtIndexPath:

```
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:                              (NSIndexPath *)indexPath{    //other stuff    Item *currentItem = nil;    if (tableView == self.searchDisplayController.searchResultsTableView)    {         currentItem = [self.searchResults objectAtIndex:indexPath.row];    } else {         currentItem = [self.dataArray objectAtIndex:indexPath.row];    }     cell.textLabel.text = currentItem.name;    cell.imageView.image = [UIImage imageNamed:currentItem.imageName];     //rest stuff}
```

finally in [search](https://www.mindstick.com/articles/65368/best-smo-services-company-in-hyderabad-improve-search-rankings),

```
- (void)filterContentForSearchText:(NSString*)searchText scope:                                   (NSString*)scope{    NSPredicate *predicate = [NSPredicate predicateWithFormat:                             @"name beginswith[c] %@", searchText];    //    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:                             @"contains[c] %@", searchText];    self.searchResults = [self.dataArray filteredArrayUsingPredicate:predicate];}
```

So in any case,

```
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:                                          (NSIndexPath *)indexPath{         Item *currentItem = nil;        if (tableView == self.searchDisplayController.searchResultsTableView){             currentItem = [self.searchResults objectAtIndex:indexPath.row];        } else {             currentItem = [self.dataArray objectAtIndex:indexPath.row];        }        //all details will be in currentItem}
```


---

Original Source: https://www.mindstick.com/forum/23369/how-to-control-multiple-arrays-when-using-search-in-uitableview

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
