You have two separate arrays as table view data source:
deviceName = [ "iPhone", "Galaxy", "iPad", "iMac", "HTC One"]
companyName = [ "Apple", "Samsung", "Apple", "Apple", "HTC" ]
Then you create a filtered array from deviceName
, in your example
searchResult = [ "HTC One"]
In cellForRowAtIndexPath
you use the filtered array and the original array companyName
, that's why you get the display "HTC One - Apple".
To solve the issue, you should not use two separate arrays as a data source, but a single array of dictionaries, where each dictionary contains the device name and the company name:
allDevices = @[
@{@"name": @"iPhone", @"company": @"Apple"},
@{@"name": @"Galaxy", @"company": @"Samsung"},
...
];
You would filter the array like this:
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"name CONTAINS[cd] %@",
searchText];
filteredDevices = [allDevices filteredArrayUsingPredicate:resultPredicate];
so that filteredDevices
is also an array of dictionaries, containing name and
company for each device. Then, in cellForRowAtIndexPath
, you can simply do
NSDictionary *device;
if ([self.searchDisplayController isActive]) {
device = filteredDevices[indexPath.row];
} else {
device = allDevices[indexPath.row];
}
NSString *name = device[@"name"];
NSString *company = device[@"company"];
cell.textLabel.text = [NSString stringWithFormat:@"%@ - %@", name, company];
Remark: I have omitted all retain/release calls, as I work with ARC usually.