Encephalon
Encephalon

Reputation: 153

How to Search multiple NSArray from UITableView in IOS

What I want is to be able to search a UITableView, and have other arrays be narrowed down to the results of the cell's textlabel array. That probably just made no sense to no one. Here's an example.

An array called titleArray that fills the cell's textLabel:

"Toyota Yaris"
"Ford Escape"
"Ford F-150"

Another array called detailArray that fills the detail text label below the textLabel in a cell:

"Car"
"SUV"
"Truck"

When I search for "Ford" I want the UITableView to display the cells like this:

Cell 1: Ford Escape
        SUV

Cell 2: Ford F-150
        Truck

But it is displaying like this:

Cell 1: Ford Escape
        Car

Cell 2: Ford F-150
        SUV

Essentially it narrowed down the titleArray but not the detailArray so it is display objects 1 & 2 from titleArray but 0 & 1 from detailArray. If this confuses you please ask any questions!

Upvotes: 1

Views: 497

Answers (2)

Prof Von Lemongargle
Prof Von Lemongargle

Reputation: 3768

In your code, you are likely creating UITableViewCell object in a message named tableView:cellForRowAtIndexPath:. I would review this code for your issue. When you create/retrieve the UITableViewCell, you will then fill in the title and detail (check this in the debugger). I note that the example you are following is creating a pared down array of items in response to your search argument. It seems most likely that you will need to pare down the detail array at the same time you pare down the title array. You may also need to search in your detail array (depending on how you want your app to function). This could complicate your search logic some.

//Possible search logic
for (int iii = 0; iii < titleArray.count; iii++) {
    if (<titleArray matches search string>) {
        [titlesToDisplay addObject:[titleArray objectAtIndex:iii]];
        [detailsToDisplay addObject:[detailArray objectAtIndex:iii]];
        //Assuming that you have titles and details in corresponding slots.
    }
}

Now when you set the title and detail settings for your UITableViewCell, you should count entries in titlesToDisplay and retrieve entries from titlesToDisplay and detailsToDisplay.

Upvotes: 1

jonkroll
jonkroll

Reputation: 15722

For this you would be much better off having just one array. Two ways you could do this:

1) create a custom class (derived from NSObject) for the elements of your array, perhaps named Vehicle. Your vehicle class would have a 'name' property, and a 'type' property.

NSMutableArray *vehicleArray = [[NSMutableArray alloc] init];

Vehicle *myVehicle = [[Vehicle alloc] init];
myVehicle.name = @"Ford Escape";
myVehicle.type = @"SUV";

[vehicleArray addObject:myVehicle];

2) have the elements in your array be of type NSDictionary. Each dictionary would have a key value pair for name and type.

NSMutableArray *vehicleArray = [[NSMutableArray alloc] init];

NSDictionary *myVehicle = [[NSDictionary alloc] initWithObjectsAndKeys:@"Ford Escape", @"name", @"SUV", @"type", nil];

[vehicleArray addObject:myVehicle];

Upvotes: 2

Related Questions