Home > DeveloperSection > Forums > How to control multiple arrays when using search in UITableView
Mikki Halpin
Mikki Halpin

Total Post:23

Points:161
Posted on    July-24-2015 12:06 AM

 iPhone iPhone  Ios  Objective C 
Ratings:


 1 Reply(s)
 609  View(s)
Rate this:

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.

 



Tarun Kumar

Total Post:213

Points:1635
Supported
Posted on    July-24-2015 8:44 AM

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,

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

}

 


Modified On Jul-24-2015 08:45:33 AM

Don't want to miss updates? Please click the below button!

Follow MindStick