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.