how to use searchController with two arrays - iphone

I have two arrays (first names, Last names) in my TableView (cell.textLabel and cell.detailTextLabel)
I want to make a search bar in this TableView.
I have created class with two string.
#interface clientContent : NSObject { }
#property (nonatomic, strong) NSString *clName;
#property (nonatomic, strong) NSString *clLast;
#end
then I have created array in main class (which have Table view) and added my information
like this
for (int i = 0; i < PQntuples(clientQuery); i++)
{
clientContent *cc = [[clientContent alloc] init];
cc.clName = [[NSString alloc] initWithUTF8String:PQgetvalue(clientQuery, i, 0)];
cc.clLast = [[NSString alloc] initWithUTF8String:PQgetvalue(clientQuery, i, 1)];
[allClients addObject:cc];
}
after this I load it into tableView;
clientContent *cc = [allClients objectAtIndex:indexPath.row];
cell.textLabel.text = cc.clName;
cell.detailTextLabel.text = cc.clLast;
}
Then I use
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
How to make it right? I can't understand..

You should override searchDisplayController:shouldReloadTableForSearchString: delegate method of UISearchDisplayController and filter datasource array for searchResultsTableView as you want.
You can apply predicate on your array(array of clientContent object) and change dataSource array. Using predicate you can filter your array on both fields(clName & clLast).
You can get help related to predicates from https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSPredicate_Class/Reference/NSPredicate.html

Related

How to retain objects in NSMutableArray IOS5

I have an array of custom objects called array which stores a bunch of core data. I use this array to populate my UITableView and to create the cells. I then try to use the same array when UISearch is opened but at this point the array is empty, how could I retain the objects in the array?
EDIT
Ok, here is some of my code
my .h file
#interface SelectCourses : UIViewController <DataHandlerDelegate, UITableViewDataSource, UITableViewDelegate, UITableViewDataSource> {
DataHandler *dataHandler;
NSMutableArray *courses;
IBOutlet UITableView *table;
IBOutlet UISearchBar *searchFilter;
//NSMutableArray *filteredCourses;
BOOL isFiltered;
}
#property (retain) NSMutableArray* filteredCourses;
Then my .m file, I have left out several functions which I believe are irrelevant
#implementation SelectCourses
#synthesize Delegate;
#synthesize filteredCourses;
...
...
- (void) dataHandlerHasLoadedCourseData:(NSMutableArray *)courseData {
courses = courseData;
[table reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(isFiltered){
return filteredCourses.count;
}
if (courses) {
NSLog(#"Count: %i", [courses count]);
if (courses.count == 1) {
return 0;
}
return [courses count];
}
else
return 0;
}
...
...
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchFiltert {
NSLog(#"searchBarSearchButtonClicked");//More debugging
if(searchFilter.text.length == 0)
{
isFiltered = FALSE;
}
else
{
NSLog(#"%#", [NSString stringWithFormat:searchFilter.text]);
isFiltered = true;
for (Course *course in courses)
{
NSRange codeRange = [course.Code rangeOfString:searchFilter.text options:NSCaseInsensitiveSearch];
NSRange nameRange = [course.Name rangeOfString:searchFilter.text options:NSCaseInsensitiveSearch];
if(codeRange.location != NSNotFound || nameRange.location != NSNotFound)
{
[filteredCourses addObject:course];
}
}
}
[table reloadData];
}
I still get the following error when running the script, I simply assumed it was due to the array being empty
2012-04-11 20:54:23.067 scheduleTable[45885:fb03] -[__NSArrayM Code]: unrecognized selector sent to instance 0x897b360
2012-04-11 20:54:23.068 scheduleTable[45885:fb03] *** WebKit discarded an uncaught exception in the webView:shouldInsertText:replacingDOMRange:givenAction: delegate: <NSInvalidArgumentException> -[__NSArrayM Code]: unrecognized selector sent to instance 0x897b360
My course.h and .m looks like this
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#class Post;
#interface Course : NSManagedObject
#property (nonatomic, retain) NSString *Code;
#property (nonatomic, retain) NSString *Name;
#end
and
#import "Course.h"
#import "Post.h"
#implementation Course
#synthesize Code;
#synthesize Name;
#end
If I'm understanding your question correctly, to retain your array, declare the array in your .h file, like so:
#property (retain) NSMutableArray *yourArrayName;
Then,synthesize it in your .m file, like this:
#synthesize yourArrayName;
I tried using the retain method as Oral b answered but it got me nowhere, I am a bit of a newbie to Objective c which is why I am having the issues.
My courses array was declared in an interface and I was not able to achieve the same result with it as I did with the new array I created just for the search. So in ViewDidLoad I do this
coursesForSearch = [[NSMutableArray alloc] init];
Then where objects were added to my other array I simply added this array and also added the objects to it.
Now I can get objects from the array like so
[[coursesForSearch objectAtIndex:500] objectAtIndex:0]);

iOS: add new cell to tableview from another view

I was wondering what is the best way to go about adding a new cell to a tableview in iOS. Let's say I have a firstviewcontroller and a secondviewcontroller. On the firstviewcontroller I have a tableview already populated with some data, I also have a navigation bar with an add button on the top. Tapping the add button will present a new view and then I want to pass data from that secondviewcontroller to firstviewcontroller and populate my tableview with it.
Thanks,
Sam
You secondViewController should create a delegate protocol. Your firstViewController should then be assigned as its delegate.
Once secondViewController saves the data it calls a method that your firstViewController should implement
A good exemple here:
How do I create delegates in Objective-C?
To get you started: secondViewController.h
#protocol secondViewControllerDelegate;
#interface secondViewController : UIViewController{
id<secondViewControllerDelegate> __unsafe_unretained delegate;
}
#property (nonatomic, unsafe_unretained) id<secondViewControllerDelegate> delegate;
#end
#protocol secondViewControllerDelegate <NSObject>
- (void)dataSaved;
#end
You can add a new class with tableView data source. initiate it with nesessary data (from both View controllers. use delegate to cooperate them) and after anu data chaging call [tableView setDataSource:]:
// DataSourceAtoZ.h
#import <Foundation/Foundation.h>
#import "MallsViewController.h"
#interface DataSourceCategory : NSObject <UITableViewDataSource> {
NSMutableArray *data;
UITableView *tableView;
}
#property (nonatomic, retain) NSMutableArray *data;
- (id)initWithData:(NSMutableArray *)d;
#end
then, anywhere in your code, compose data you wish to be in tableView and set dataSource:
NSMutableArray *dt = [[NSMutableArray alloc] init];
for (int i = 0; i < [categories count]; i++) {
NSMutableArray *names = [[NSMutableArray alloc] init];
[names addObject:[[categories objectAtIndex:i] objectAtIndex:0]];
for (int j = 1; j < [[categories objectAtIndex:i] count]; j++) {
NSRange match = [[[categories objectAtIndex:i] objectAtIndex:j] rangeOfString:[params objectAtIndex:0]];
if (match.length != 0) {
[names addObject:[[categories objectAtIndex:i] objectAtIndex:j]];
}
}
[dt addObject:names];
[names release];
}
dsCategory = [[DataSourceCategory alloc] init];
dsCategory = [dsCategory initWithData:dt];
[dt release];
You should create your arrays in the App delegate and then call them and change them however you want from there..
MyAppDelegate *appDelegate = (MyAppDelegate*)[[UIApplication sharedApplication] delegate];
once you have this line you can connect between two classes and call any methods on the delegate for example:
[appDelegate populateMyArray];
you don't add "new cells" like you described it, you use the same cells and populate them with different data.

Storing and opening data in a variable

I'm making an app that calculates certain things.
I need it to be able to take the input from the first textfields, for example 4+4 and save the result in a variable.
In the second text fields there could be 8+8 for example, and the result of that will also be saved into a variable (possibly the same).
Third row of textfields could yield more numbers etc, etc..
In the end there will be a button "Calculate" for example. And that will take the results from first, second, third etc textfields and calculate all of those together and output the end result.
The calculations are of course more advanced than this, but I just need the basic/simple idea of how to do this.
There is no need for saving the data to a file just now, it should just be in the app while the other textfields are being filled.
For 0x8badf00d:
Header.
#interface UnitConverterViewController : UIViewController {
NSMutableArray *calculationsArray;
UITextField *m1Text;
UITextField *m2Text;
}
#property (nonatomic, retain) IBOutlet UITextField *m1Text;
#property (nonatomic, retain) IBOutlet UITextField *m2Text;
#property (nonatomic, retain) IBOutlet NSMutableArray *calculationsArray;
#end
Implementation:
#implementation UnitConverterViewController
#synthesize m1Text, m2Text, calculationsArray;
#synthesize resultTotal = _resultTotal;
-(id)init {
if(self = [super init]){
calculationsArray = [[NSMutableArray alloc] init];
}
}
- (void)compute{
NSString* sumString = [NSString stringWithFormat:#"%d",[m1Text.text intValue]+[m2Text.text intValue]];
[calculationsArray addObject:sumString];
}
-(IBAction)calculate{
int total=0;
for(NSString* sumStr in calculationsArray){
total = total+[sumStr intValue];
}
NSLog(#"Total: %d", total);
[calculationsArray release], calculationsArray = nil;
}
I must be doing something wrong, and I know I need a way to output this, a label and such. But for now I need to know if what I've done so far is correct, and what the best way of outputting it would be.
You should declare the variables to store the results in your header file, these are than accessible from anywhere in your .m file, the same goes for your text fields.
For example:
Calculator.h
#interface Calculator: SuperclassName{
UITextField *_fldOne;
UITextField *_fldTwo;
UITextField *_fldThree;
UITextField *_fldFour;
int resultOne;
int resultTwo;
int _resultTotal;
}
#property(nonatomic, readonly) int resultTotal;
- (void) calculate;
#end
Calculator.m
#implementation Calculator
#synthesize resultTotal = _resultTotal;
- (void) calculate{
resultOne = [_fldOne.text intValue] * [_fldTwo.text intValue];
resultTwo = [_fldThree.text intValue] / [_fldFour.text intValue];
totalResult = resultOne + resultTwo;
}
#end
In this example resultOne and Two, and all the textfields are available throughout your class to work with, the totalResult is set as a readonly property and synthesized to create a getter automaticaly (which returns the value stored in _totalResult because of synchronizing like totalResult = _totalResult) as so it is available to read from outside the class.
As long as it all happens on one screen it should be more than enough, but of course you could make an NSDictionary or NSArray but that seems unnecessary here.
Hope this helps
Save the result to array. Lets say you have NSMutableArray* calculationsArray;//iVar
//initialize calculationsArray in init method
-(id)init
{
if(self = [super init])
{
calculationsArray = [[NSMutableArray alloc] init];
}
}
- (void)compute
{
NSString* sumString = [NSString stringWithFormat:#"%d",[textField1.text intValue]+[textField2.text intValue]);
[calculationsArray addObject:sumString];
}
- (IBAction)calculate
{
int total=0;
for(NSString* sumStr in calculationsArray)
{
total = total+[sumStr intValue];
}
NSLog(#"Total: %d",total);
[calculationsArray release],calculationsArray = nil;
}

Hiding all UILabels in NSMutableArray

I have various UILabels that I would like to hide using a for-loop.
#interface MyViewController : UIViewController {
NSMutableArray * labelArray;
}
#property (nonatomic, retain) IBOutlet UILabel *label1, *label2, *label3;
...
-(void)viewDidLoad {
[super viewDidLoad];
[labelArray initWithObjects:label1,label2,label3,nil];
for(int i=0; i<sizeof(labelArray); i++){
UILabel *label = [labelArray objectAtIndex:i];
label.hidden = !label.hidden;
}
}
When this is executed, the labels are not hidden. They have been "hooked up" in Interface Builder. What am I doing incorrectly? Thanks!
That is not what sizeof is for. That's a compiler construct that tells you how many bytes a value takes up, which has no clue how many elements are in an NSMutableArray at runtime. You want:
for (UILabel *label in labelArray) {
label.hidden = !label.hidden;
}
If that doesn't work, then your array does not contain the objects you believe it does — quite possibly, you've forgotten to actually create the array — simply sending init to nil does not create an object. Either way, you should probably be doing labelArray = [[NSMutableArray alloc] initWithObjects:label1,label2,label3,nil];. alloc and init go together hand-in-glove.

UITableView corruption upon scroll

Im trying to get a UITableView to display items contained within an array.
This array is contained within a class (HX_ParkingSearch).
I have a ref to this class that contains the array inside the app delegate class, to enable the views to access it.
Problem is that I get one page of results displaying correctly inside the tableview
but when i try and scroll down an exception occurs when trying to access the next item in the array.
It turns out that when i scroll down and the cellForRowAtIndexPath method fires,
the items inside the array are invalid and appear to have been released but i dont understand where
they are being released!
Does anyone have any ideas because this is really doing my head in now!
Many thanks,
Chris.
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
HX_ParkingLocation *location;
bookingApp2AppDelegate *del = (bookingApp2AppDelegate *) [[UIApplication sharedApplication] delegate];
NSMutableArray* array = [del.parkingSearch locations];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
location = (HX_ParkingLocation*) [array objectAtIndex: [indexPath row] ];
return cell;
}
#import <Foundation/Foundation.h>
#interface HX_ParkingLocation : NSObject
{
NSString *name;
}
#property(retain,nonatomic) NSString* name;
/*
Initialises this Location instance by passing in the name and code of the location and the URL of the webapi product endpoint.
The URL is used to find available products at this location.
*/
-(id) initWithName: (NSString*) n;
#end
#import <Foundation/Foundation.h>
#interface HX_ParkingSearch : NSObject
{
NSMutableArray* locations;
}
#property (retain) NSMutableArray* locations;
-(BOOL) loadLocations;
#end
#import "HX_ParkingSearch.h"
#import "HX_Parking_Location.h"
#implementation HX_ParkingSearch
#synthesize locations;
//Finds the locations
-(BOOL) loadLocations
{
[locations release];
//Create array to hold locations
locations = [[NSMutableArray alloc] initWithCapacity:30];
//Loop through all returned locations
for(int i=0;i<15;i++)
{
//Get location name
NSString* n = [NSString stringWithFormat:#"Item #%i",i ];
//Create location instance, which retrieves availability and product information and stores the information in location object.
HX_ParkingLocation* location = [[HX_ParkingLocation alloc] initWithName:n];
//add to array
[locations addObject:location];
}
return YES;
}
#end
#import <UIKit/UIKit.h>
#import "HX_ParkingSearch.h"
#interface bookingApp2AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navigationController;
HX_ParkingSearch *parkingSearch;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#property (retain) HX_ParkingSearch *parkingSearch;
#end
#implementation bookingApp2AppDelegate
#synthesize window;
#synthesize navigationController;
#synthesize parkingSearch;
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
//Create new parking search instance by specifying the endpoint urls
HX_ParkingSearch* search = [[HX_ParkingSearch alloc] init];
[search loadLocations];
parkingSearch = search;
//NSLog(#"Search Retain count = %i" ,[search retainCount]);
[window addSubview:[navigationController view]];
//[window addSubview:[navigationController initWithNibName:#"VC_Locations" bundle:[NSBundle mainBundle]]];
[window makeKeyAndVisible];
}
Is there a reason that you're init-ing your array with a capacity of 30 in loadLocations, but only inserting 15 items?
what's in this method for your datasource?:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
and also:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
the tableview may just be trying to grab an index that's out of bounds of locations