I want to display name and lastName in my UITableViewCell. I have an array with a lot of data that I retrieve from a database, and pretty much everything I need is in the array, the trick is to filter and show the results as I want.
I have the following code to filter what is being typed on searchBar and add to an NSMutableArray:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"usuario.username contains [cd] %#", searchText];
NSArray *filtroUsuario = [name filteredArrayUsingPredicate:predicate];
searchResults = [filtroUsuario valueForKeyPath:#"#distinctUnionOfObjects.usuario.nome"];
I use #distinctUnionOfObjects because the objects that I'm filtering are not user objects, therefore I want to retrieve user values, so as some objects point to the same user, I get duplicate names.
The code to put information on the UITableView is like this:
cell.textLabel.text = searchResults[indexPath.row];
It all works fine. My trouble is that now I want to show one more key on the cell, so the keypath would be usuario.sobrenome. How would I put both values together?
I've tried playing with the line:
searchResults = [filtroUsuario valueForKeyPath:#"#distinctUnionOfObjects.usuario.nome"];
and got some interesting results, but not the one I'm expecting.
It looks like you should be able to use #distinctUnionOfObjects.usuario, so your results is an array of user objects (or some other objects with the keys that you require). Then in the table view cell setup you do:
id user = searchResults[indexPath.row];
cell.textLabel.text = [user valueForKey:#"nome"];
cell.otherTextLabel.text = [user valueForKey:#"sobrenome"];
Related
I have a table with a UISearchController that searches it. The first table is populated from a JSON with a format of:
{
"items":
[
{
"title":"title1",
"url":"url1",
},
{
"title":"title2",
"url":"url2",
}
]
}
The "title" is shown as the cell's textLabel and the url is the link that opens when the cell is clicked.
When I search in the search bar a results table shows populated by the the titles that match the search criteria. My problem is these don't include the urls so nothing happens when these cells are clicked. My search criteria is as follows:
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
// filter the search results
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF contains [cd] %#", self.searchController.searchBar.text];
self.results = [[self.JSONarray valueForKey:#"title"] filteredArrayUsingPredicate:predicate];
[self.tableView reloadData];
}
I can see what I think is the problem but cannot figure out how to fix it. When searching it is only searching through the titles and populating the results array with these but I need the urls to filter based on the corresponding titles.
Any help would be really appreciated.
I'm typing this off the top of my head since I don't have all your code, but try the following:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"title contains [cd] %#", self.searchController.searchBar.text];
self.results = [self.JSONarray filteredArrayUsingPredicate:predicate];
The difference is that we're not filtering on the title elements of the JSON array first; instead, we're searching the whole array, and using title in the predicate.
After doing this, self.results should be an NSArray of NSDictionarys, each of which contains a title and url element. So you can access the URLs as appropriate to retrieve your results.
I am not sure how to ask the question, so perhaps if I say what the issue is someone out there can help.
I have a SQL lite table which I use to populate two arrays: one of names and surnames, and the other array, which gets populated at the same time, of the ID of the person from the database.
I then display these names in a TableView. I use the Predicate method to search for someones name in the original array (called storiesArray).
However, when I select this person to display, it displays the First person in the stories Array.
I have tried getting the row where the search string is found, but this returns a very large number, even though it does find the string (using Predicates as explained).
How can I get the row number of the found string?
So, I have tried the following code:
NSUInteger indexOfTheObject = [storiesArray indexOfObject: searchText];
But this returns the number 2147483647, which in effect is a -1.
I know that the searchText is in stories array because the following code does return a valid search result:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:#"SELF contains[c] %#", searchText];
self.searchResult = [NSMutableArray arrayWithArray:[storiesArray filteredArrayUsingPredicate:resultPredicate]];
}
self.searchResult gets populated with the correct string.
Thanks in advance.
Similar Threads here:
How retrieve an index of an NSArray using a NSPredicate?
how to find the index position of the ARRAY Where NSPredicate pick the value. I use filteredArrayUsingPredicate as filter
http://useyourloaf.com/blog/2010/10/19/searching-arrays-with-nspredicate-and-blocks.html
NSUInteger index = [self.myarray indexOfObjectPassingTest:
^(id obj, NSUInteger idx, BOOL *stop) {
return [predExists evaluateWithObject:obj];
}];
I am performing a simple PFQuery to fetch a bunch of Box objects (class name).
My Box has a pointer to a Toy object called toy.
I let my user select a bunch a toys, then the search only display the Box objects with those Toy objects.
So I end up with an NSArray of PFObjects of type Toy. I have an array of objectId strings for the objects, and I just create another array like this:
PFObject *obj = [PFObject objectWithoutDataWithClassName:#"Toy" objectId:objectId];
Now I can query the object, I would have thought. I have tried doing a query of Box objects with an NSPredicate which looks like this:
[NSPredicate predicateWithFormat:[NSString stringWithFormat:#"toy = %#", toyObject]];
My app crashes and tells me it is unable to parse that. So before adding the predicate I take the objectId instead and try doing that:
[NSPredicate predicateWithFormat:[NSString stringWithFormat:#"toy.objectId = %#", toyObject.objectId]];
However, it doesn't like that format either. So how can I create an NSPredicate that lets me only fetch objects with a specific pointer result like explained.
In other words just use
PFUser *user = [PFUser currentUser];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"fromUser = %#", user];
where in this example, "user" is a pointer stored in the table and we're searching for it under the "fromUser" column
I think you're making this too hard on yourself. If I'm reading your question correctly, there is a much simpler way to do this, and you don't have to use NSPredicate at all:
NSMutableArray *toys = [NSMutableArray array];
// Figure out some way (during selection) to get the "toy" objects into the array
PFQuery *query = [PFQuery queryWithClassName:#"Box"];
[query whereKey:#"toy" containedIn:toys];
[query findObjects... // You should know the rest here
And that's it! Nice and easy, should find all Box instances that have a toy that is contained in the toys array.
Turns out it was pretty simple.
Don't use [NSString stringWithFormat:(NSString *)] if you are creating an NSPredicate format string. NSPredicate really doesn't like it and can often fail to parse your format.
i have a problem with using a search bar filter for my UITableView.
I'm logging Temperature data in multiple arrays:
NSMutableArray *timeArray; // Array which saves date and time
NSMutableArray *tempInside; // saves inside temp
NSMutableArray *tempOutside; // saves outside temp
NSMutableArray *timeArraySearch; // Array which saves date and time searched
NSMutableArray *tempInsideSearch; // saves inside temp searched
NSMutableArray *tempOutsideSearch; // saves outside temp searched
So to display the all the data, i have a UITableView. I use the timeArray for the cell.textlabel.text.
Right now i have trouble with getting the data filtered using the search bar. Here is my
filterContentForSearchText method:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:#"SELF contains[cd] %#",
searchText];
tempInsideSearch = [tempInside filteredArrayUsingPredicate:resultPredicate];
tempOutsideSearch = [tempOutside filteredArrayUsingPredicate:resultPredicate];
timeArraySearch = [timeArray filteredArrayUsingPredicate:resultPredicate];
}
I ran the application and got a empty array exception. Then i checked this method again and realized that its normal that the tempInsideSearch and tempOutsideSearch are empty, because they don't contain values like "06:41PM" ...
So finally here is my question:
How can i filter the temp arrays, that they fit the index with the timeArray?
Create a class with date/time, insideTemp, outsideTemp properties, store instances of it into an array, and apply the filtering on that array instead of using 3 different arrays to store informations highly tied ...
I am trying to work out how many cells my table view should have. This is determined by results of a query which are stored in Array.
When the view is loaded the array might not exist or have any values so the cells should be 0.
How do I check through my array to check for a specific object. I understand I can use containsObject or equalTo...
My array would consists of objects like this:
{<GameTurn:TLED0qH44P:(null)> {\n GameRef = \"<Game:KgguI4ig4O>\";\n GameTurnImage = \"<PFFile: 0xb3da9d0>\";\n GameTurnWord = tester;\n OriginalImageCenterX = \"27.9\";\n OriginalImageCenterY = \"29.39375\";\n TurnCount = 1;\n UploadedBy = \"<PFUser:UgkZDtDsVC>\";\n}
There would be multiple entries of the above. For each entry I need to check if the UploadedBy key is equal to the PFUser currentUser. If it is add one cell, and so on.
So I need to get an overall count of the items in the array where that key is equalto the current user.
You can filter the array to get a new array of all matching objects:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"UploadedBy = %#", currentUser];
NSArray *filtered = [array filteredArrayUsingPredicate:predicate];
and use the filtered array as table view data source.
If the array comes from a Core Data fetch request, it would be more effective to
add the predicate to the fetch request already.
There are many ways you can filter an array in Objective-C. Here is one method using blocks and NSIndexSet.
You can grab all the indexes of your original array where the objects pass a test, specified in the block. Then create another array consisting of the objects at those indexes.
// get all indexes of objects passing your test
NSIndexSet *indexes = [myArray indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
// replace this with your equality logic
return [obj uploadedBy] == [PFUser currentUser];
}];
// Filled with just objects passing your test
NSArray *passingObjects = [myArray objectsAtIndexes: indexes];