1

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.

1

1 Answer 1

2

create a NSObject subclass for this Item.h and Item.m with 3 properties for name and id like,

in Item.h

typedef enum : NSInteger {
    ItemTypeVideo,
    ItemTypeActor,
} ItemType;

@interface Item : NSObject 

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *itemId;
@property (nonatomic, strong) NSString *imageName;
@property (nonatomic, assign) ItemType itemType;

@end

by above code we are creating a class whose object can hold values defined in properties.

since an object of Item class can hold name, id, image name and type of video/actor, we can set these values of 1 video/actor to one object. here we have multiple vides/actor elements, so we need an array of Item objects each one holding details of one video/actor like

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

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;
actorItem.itemType = ItemTypeActor;

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
}
Sign up to request clarification or add additional context in comments.

8 Comments

how would i know if it's Actor or Video? i already have arrays filled through Webservices, can i just add then in dataArray
you can use an enum or another property to know actor/video. see updated answer.
@Akhilrajtr can i create NSObject in a same class? i cant use it when creating same class... app is crashing.
Like i added interface Item : NSObject, above interface people ()
I am having trouble in NSObject that you created first... i am new at ios... can you explain little more about it
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.