forum

Home / DeveloperSection / Forums / How to control multiple arrays when using search in UITableView

How to control multiple arrays when using search in UITableView

Anonymous User 1827 24-Jul-2015

I have four array videoName, ActorName, VideoID, ActorID. I combined videoName & ActorName to make a single array "title", and same with VideoID & ActorID to make array "IDs" In short, title = ActorName + videoName IDs = ActorID + VideoID

here is my code,

Tableview Methods

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

Now, first i need to get that which row was selected, actor or Video, secondly actorID or VideoID. It was easy if there was no search bar, 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 select the row.

 


Updated on 24-Jul-2015
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By