-2

Im adding searching in UITableView. I want to add smart searching like used in XCode

For example

Keyword

Smarting

Should match with

  1. SmartSearching

  2. SmartWatching

I also want to make bold to matching letters in resulting UILabel inside UITableView

XCode used this kind of search too

I'm attaching a snapshot from XCode

enter image description here

How we can achieve this

Note: This is not normal searching.

Saranjith
  • 11,242
  • 5
  • 69
  • 122
  • Could be related: [UITableView filtering](https://stackoverflow.com/questions/39738518/uitableview-filtering) – Ahmad F Jul 31 '17 at 07:36
  • @AhmadF: Perhaps I am overlooking something, but I cannot see a solution to this specific problem in that Q&A. – Martin R Jul 31 '17 at 07:39
  • @MartinR my bad, flagging a question will automatically mark as a "close", I should add it manually. Also, you're right about it; It *could* be -somehow- related... – Ahmad F Jul 31 '17 at 07:43
  • [Comparing strings with tolerance](https://stackoverflow.com/questions/2344320/comparing-strings-with-tolerance). Or implement your own algorithm, it shouldn't be too difficult. – Willeke Jul 31 '17 at 07:52
  • You can have an idea there: https://stackoverflow.com/questions/44412710/how-to-searchpredicate-content-from-list-like-xcode-suggestion/44413753#44413753 – Larme Jul 31 '17 at 08:10
  • https://stackoverflow.com/q/44412710/5215474 doesn't working it matches me wrong cells. That code is not acceptable. Also i need to have swift version please do reopen the question @MartinR – Saranjith Jul 31 '17 at 09:03
  • Did you try both answers that I linked to? Please provide a *concrete* example if it does not work for you. – Martin R Jul 31 '17 at 09:08
  • @MartinR Im using swift – Saranjith Jul 31 '17 at 09:08
  • 1
    Then *why* did you tag the question with both [objective-c] and [swift] originally? – Martin R Jul 31 '17 at 09:09
  • Thats by mistake i have changed it just after getting objective c answers – Saranjith Jul 31 '17 at 09:10
  • 1
    " doesn't working it matches me wrong cells." My code works last time I checked. It's a solution similar to the one by @Martin R. The "matches wrong cell" make me clearly think about a bad dataSource implementation. – Larme Jul 31 '17 at 09:52
  • Unless you have custom functionality that requires you to implement a custom solution, see if you can avoid re-inventing the wheel and reference or use one of the libraries below: https://github.com/mnbayan/AutocompleteTextfieldSwift https://github.com/EddyBorja/MLPAutoCompleteTextField – cohen72 Jul 31 '17 at 07:44
  • Im getting wrong cells in search results. im trying to correct it now. But code i need to have is in swift – Saranjith Jul 31 '17 at 11:14

3 Answers3

1
//Filter within Tableview delegate functions

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", SEARCH_TEXT];
    NSArray *filtered = [arrayCategories filteredArrayUsingPredicate:predicate];
    return filtered.count
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:@"CELLSEARCH"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", SEARCH_TEXT];
    NSArray *filtered = [arrayCategories filteredArrayUsingPredicate:predicate];

    [cell.textLabel setText:filtered[indexPath.row]];
    return cell;
}
Berlin Raj
  • 124
  • 6
  • It's better to FILTER before (and change the array), rather than doing it EACH time in `tableView: numberOfRowsInSection` and `tableView:cellForRowAtIndexPath:`. – Larme Jul 31 '17 at 09:53
1

I think this is what you are looking for

let arr = ["job","smart","hating","eating","luck","cup",]
let charset = CharacterSet(charactersIn: "smarting")
for str in arr
{
    if str.lowercased().rangeOfCharacter(from: charset) != nil
    {
    print(str)
    }
}

OUTPUT

smart hating eating

Jaydeep Vyas
  • 4,411
  • 1
  • 18
  • 44
0

In case if you want to show results from array. You can use NSPredicate. You need to add all strings in array and take another array for showing filtered results like:

//filter content from array:
- (void)filterContentForSearchText:(NSString*)searchText {

    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", searchText];
    YOUR_FILTERED_ARRAY = [[YOUR_ARRAY filteredArrayUsingPredicate:resultPredicate] mutableCopy];

}

In case you have array of dictionaries then just replace the filtration line with this one

YOUR_FILTERED_ARRAY = [[YOUR_ARRAY filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(name contains[c] %@)", searchText]]mutableCopy];
//name is the "key" and search text is the matching value!

REFERENCE for predicate.

Apple API Doc for filteredarrayusingpredicate.

SWIFT 3x:

//filter content from array:
func filterContent(forSearchText searchText: String) {
    let resultPredicate = NSPredicate(format: "SELF contains[c] %@", searchText)
    YOUR_FILTERED_ARRAY = YOUR_ARRAY.filter { resultPredicate.evaluate(with: $0) }

}

YOUR_FILTERED_ARRAY = YOUR_ARRAY.filter { NSPredicate(format: "(name contains[c] %@)", searchText).evaluate(with: $0) }
Anurag Sharma
  • 4,276
  • 2
  • 28
  • 44