Thread: Search Bar
View Single Post
  #1 (permalink)  
Old March 14th, 2012, 03:20 AM
Jule Jule is offline
Authorized User
Points: 62, Level: 1
Points: 62, Level: 1 Points: 62, Level: 1 Points: 62, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2011
Posts: 16
Thanks: 8
Thanked 0 Times in 0 Posts
Default Search Bar

I have a table view app that i would like to have a search bar on, i have tried the search bar in chapter 10 but it does not work, think i have coded it wrong. Can I get help to get this.

there is my rootviewcontroller.m file

Code:

#import "RootViewController.h"
#import "DetailViewController.h"

@implementation RootViewController

#define CELL_HEIGHT 80.0

/*
- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
*/


- (void)viewWillAppear:(BOOL)animated {
    dao = [[cropLibraryDao alloc] initWithLibraryName:@"TestData"];
    self.title = @"Crop List";
    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}

/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/


 // Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations.
	return YES; //(interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release anything that can be recreated in viewDidLoad or on demand.
	// e.g. self.myOutlet = nil;
}


#pragma mark Table view methods

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


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [dao libraryCount];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"LibraryListingCell";
    
    cropListingViewCell *cell = (cropListingViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"cropListingView" owner:self options:nil];
        cell = [_cell autorelease];
        _cell = nil;
    }
    
	cell.nameLabel.text = [[dao libraryItemAtIndex:indexPath.row] valueForKey:@"name"];    
    cell.levelLabel.text = [NSString stringWithFormat:@"Level %@ ", 
                                    [[dao libraryItemAtIndex:indexPath.row] valueForKey:@"level"]];
    cell.cropImageView.image = [UIImage 
                                 imageNamed:[[dao libraryItemAtIndex:indexPath.row] valueForKey:@"cropImage"]];

    return cell;
}



// Override to support row selection in the table view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailViewController *controller = [[DetailViewController alloc] 
                                        initWithcropData:[dao libraryItemAtIndex:indexPath.row]
                                        nibName:@"DetailViewController" bundle:[NSBundle mainBundle]];
    controller.title = [[dao libraryItemAtIndex:indexPath.row] valueForKey:@"name"];
    [self.navigationController pushViewController:controller animated:YES];
    [controller release];    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return CELL_HEIGHT;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


- (void)dealloc {
    [super dealloc];
}


@end
and rootviewcontroller.h
Code:
#import "cropLibraryDao.h"
#import "cropListingViewCell.h"

@interface RootViewController : UITableViewController {
    cropLibraryDao *dao;
    
    IBOutlet cropListingViewCell *_cell;
}

@end
detailviewcontroller.h

Code:
#import <UIKit/UIKit.h>


@interface DetailViewController : UIViewController {
	
	IBOutlet UIScrollView *scrollView;
	
	IBOutlet UIImageView *cropImageView;
    IBOutlet UILabel *nameLabel;
    IBOutlet UILabel *levelLabel;
	IBOutlet UITextView *commentsTextView;
	IBOutlet UILabel *costLabel;
	IBOutlet UILabel *returnsLabel;
	IBOutlet UILabel *xpReturnsLabel;
	IBOutlet UILabel *revenueLabel;
    IBOutlet UILabel *timeLabel;
	IBOutlet UILabel *boostingLabel;
	IBOutlet UILabel *spoilageLabel;
	IBOutlet UILabel *coinsLabel;
	IBOutlet UILabel *xpLabel;
   
	
    NSDictionary *cropData;
}


@property (nonatomic, retain) UIImageView *cropImageView;
@property (nonatomic, retain) UILabel *nameLabel;
@property (nonatomic, retain) UILabel *levelLabel;
@property (nonatomic, retain) UITextView *commentsTextView;
@property (nonatomic, retain) UILabel *costLabel;
@property (nonatomic, retain) UILabel *returnsLabel;
@property (nonatomic, retain) UILabel *xpReturnsLabel;
@property (nonatomic, retain) UILabel *revenueLabel;
@property (nonatomic, retain) UILabel *timeLabel;
@property (nonatomic, retain) UILabel *boostingLabel;
@property (nonatomic, retain) UILabel *spoilageLabel;
@property (nonatomic, retain) UILabel *coinsLabel;
@property (nonatomic, retain) UILabel *xpLabel;



- (id)initWithcropData:(NSDictionary *)data nibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;


@end
detailviewcontroller.m
Code:
#import "DetailViewController.h"


@implementation DetailViewController

@synthesize cropImageView, nameLabel, levelLabel, commentsTextView, costLabel, returnsLabel, xpReturnsLabel, revenueLabel, timeLabel, boostingLabel, spoilageLabel, coinsLabel, xpLabel;


	

- (id)initWithcropData:(NSDictionary *)data nibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        cropData = data;
    }
    return self;
}


/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
    
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    cropImageView.image = [UIImage imageNamed:[cropData valueForKey:@"cropImage"]];
    nameLabel.text = [cropData valueForKey:@"name"];
    levelLabel.text = [NSString stringWithFormat:@"Level %@ ", [cropData valueForKey:@"level"]];
	commentsTextView.text = [cropData valueForKey:@"comments"];
	costLabel.text = [NSString stringWithFormat:@"Cost %@ coins", [cropData valueForKey:@"cost"]];
	returnsLabel.text = [NSString stringWithFormat:@"Returns %@ coins", [cropData valueForKey:@"returns"]];
    xpReturnsLabel.text = [NSString stringWithFormat:@"XP Returns %@ ", [cropData valueForKey:@"xpReturns"]];
    revenueLabel.text = [NSString stringWithFormat:@"Profits %@ coins", [cropData valueForKey:@"revenue"]];
	timeLabel.text = [NSString stringWithFormat:@"Harvest in %@ hour(s)", [cropData valueForKey:@"time"]];
	boostingLabel.text = [NSString stringWithFormat:@"To Boost %@ Mojo", [cropData valueForKey:@"boosting"]];
    spoilageLabel.text = [NSString stringWithFormat:@"Spoils in %@ hour(s)", [cropData valueForKey:@"spoilage"]];
    coinsLabel.text = [NSString stringWithFormat:@"Earn %@ coins per hour", [cropData valueForKey:@"coins"]];
	xpLabel.text = [NSString stringWithFormat:@"Earn %@ XP per hour", [cropData valueForKey:@"xp"]];

	[super viewDidLoad];
    
    scrollView.frame = CGRectMake(0, 0, 320, 460);
	[scrollView setContentSize:CGSizeMake(320, 678)];
    
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}



// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations.
	return YES; //(interfaceOrientation == UIInterfaceOrientationPortrait);
}



@end

thanks for any help and suggestions. If I have to I will start again using the example in chapter 10.

Last edited by Jule; March 15th, 2012 at 10:13 PM.