0

Hi I am new to iphone Application Development, but this question is more of coding related.I am using a tableview to display users name(textLabel) with phoneNumbers(detailtextLabel) using Addressbook of course. I have stored both of them in separate arrays. Also, I have an email addresses stored in a separate array. and using all this I am successfully able to display the contacts in sorted order in the table view created. The problem comes in the implementation of searchDisplayController. Here I want to be able to search by users name and emailid(either).

So, I am using an NSDictionary to store name+email as a value (NSString format) with keys as the index of the row on which it is displaying that contact. Now I am successfully getting an array of all the matching contacts using the following code.

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope{

NSPredicate *resultPredicate = [NSPredicate
                                predicateWithFormat:@"SELF contains[cd] %@",
                                searchText];

searchResults  = nil;
matchingContacts = nil;
NSArray *allContactValues = [allContactNamesWithEmailIDs allValues];
searchResults = (__bridge CFArrayRef)([[allContactValues sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)] filteredArrayUsingPredicate:resultPredicate]);
matchingContacts  = [[NSMutableArray alloc] initWithArray:(__bridge NSMutableArray*)searchResults];

The matchingContacts gives back the array of all the matching contact Names. all But using this has a drawback when there are two users with same name(same value bt different keys), I could not trace it back to different keys. It is display the same names with same phoneNumbers twice which would be a problem.

The code creating the cells is as follows:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{     static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

}
    if(tableView == self.searchDisplayController.searchResultsTableView){

        if (farmMarkets.count>0) {
            destIndex = [[[allContactNamesWithEmailID allValues] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)] indexOfObject:matchingContacts[indexPath.row]];  //?? I believe this is what needs to be modified to display multiple contacts. Just don't know how.

            [cell.textLabel setText:farmMarkets[destIndex]];

            if (destIndex<0) {
                [cell.detailTextLabel setText:@""];
            }
            else{
                [cell.detailTextLabel setText:phoneNumbersArray[destIndex]];
            }

        }
    }

}

Can someone give me an example with code, explaining how this should be better implemented? All ideas will be helpful. In an all I will summarise it now. I want to implement a tableView displaying contacts with Name and phone Number and a search bar at the top. And when i search using either name or email ID it should display the names with corresponding phone Number.

Roadblock
  • 2,041
  • 2
  • 24
  • 38

1 Answers1

0

From your description you have a lot of different methods of storing data and your problem comes form trying to correlate them together at different times.

I would simplify your structure so you have a single array of source information where that array contains dictionaries. Each dictionary contains all of the data for one person.

Now, you can sort that array as required and on any (combination of) key in the dictionaries. Also, when you filter, you filter the array (or, better, a copy of it so you still have the original) so the order is maintained and you don't need to correlate to anything else.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • Still the problem of getting NameClash while searching, won't get resolved. This is the only problem I am facing. I am able to present rest of the things successfully. – Roadblock Aug 23 '13 at 12:10
  • If the items in the array are unique then you won't get a clash. Your current clash is because you are trying to search across 2 arrays where one array has duplicates because of the limited info it contains (just the name). – Wain Aug 23 '13 at 12:32