1

I have 3 core data entities.

MenuItems, Items, ItemLangs.

All entities has iItemId attribute. The user need to search using sItemName which can be found in the ItemLangs.

The UITableView contains array of MenuItems.

I have tried filteredArrayUsingPredicate but my predicate is not right. Do I need to loop through the array of MenuItems first before I can filter the array?

Here is my predicate:

NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"sItemName LIKE %@", searchText];

May I know how can I use this to filter the array of MenuItems that I have?

Thank you!

Nirav Bhatt
  • 6,940
  • 5
  • 45
  • 89
Lie-An
  • 2,563
  • 3
  • 17
  • 20

2 Answers2

3

What you need is a predicate like this:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"sItemName CONTAINS[cd] %@", searchText];

[cd] stands for case insensitiveness.

Alternately, if you like "LIKE", refer to this answer.

UPDATE:

You should also look at the data item you want to fetch - sItemName in your case. Through debugger, inspect what object does your data source array return? I am referring to the array you want to apply the above predicate to. Maybe there is some keypath hierarchy involved in getting through towards sItemName, in which case you need to use dot notation, such as Items.ItemLangs.sItemName.

Community
  • 1
  • 1
Nirav Bhatt
  • 6,940
  • 5
  • 45
  • 89
  • thank you for your answer. I think there is nothing wrong with my predicate. The problem is with how am I going to use the predicate. The contents of tableview is MenuItems and the predicate can be used in ItemLangs. The entities are connected throug iItemId. MenuItem -> Items -> ItemLangs – Lie-An Aug 05 '14 at 06:30
  • Maybe you should use some key path notation (dot syntax, like arraymember.Items.ItemLangs.sItemName)? – Nirav Bhatt Aug 05 '14 at 06:37
  • I have tried before, using menuItems.Items.ItemLangs.sItemName. But how should I use it in the array of MenuItems. Should I loop through MenuItems? And the relationship between Items and ItemLangs is to many. – Lie-An Aug 05 '14 at 06:46
  • As I said, inspect the array that holds MenuItems - it should tell you . If it is one to many, it must hold an array of children. In such case you would first grab a MenuItem object from your array, and access the array called Items. Within it, you will find each Item members. And Within each of them will be ItemLangs array, which will get you desired sItemName. You must know exact data item to get there. – Nirav Bhatt Aug 05 '14 at 06:50
  • See tutorial on Magical Record: http://www.raywenderlich.com/56879/magicalrecord-tutorial-ios – Nirav Bhatt Aug 05 '14 at 06:54
2

Refer following links, it may help you:

1) How to implement SearchBar and search display controller in tableview in xib

2) first attempt at synamic filtering a search array

3) http://www.appcoda.com/search-bar-tutorial-ios7/

4) http://www.raywenderlich.com/16873/how-to-add-search-into-a-table-view

Community
  • 1
  • 1
VRAwesome
  • 4,721
  • 5
  • 27
  • 52
  • 1
    thank you for your answer. I have already checked those links and have not found the correct answer before I posted here in stackoverflow. – Lie-An Aug 05 '14 at 06:30