Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-30-2010, 12:37 PM   #1 (permalink)
ACMT
 
katsogiannis's Avatar
 
Join Date: Jun 2010
Location: Chandlers Ford, UK
Posts: 39
katsogiannis is on a distinguished road
Send a message via MSN to katsogiannis Send a message via Skype™ to katsogiannis
Default UISearchBar displays wrong cell view...

hello all.

i have a TableView with a SearchBar. For each cell i have a different nib file which is work fine.
the problem that i have is that when i use the SearchBar and i choose the search results for display i am getting the wrong view.
i have notice that when i do a search and i have 3 results, the order is the same as the one on the table view. it looks like there is a problem with the cellValue on my code.

see the code below


HTML Code:
#import "RootViewController.h"
#import "TableViewAppDelegate.h"
#import "lsViewController.h"
#import "OverlayViewController.h"
#import "DetailViewController.h"
#import "pwdViewControll.h"
#import "cdViewController.h"


@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
	
	
	listOfItems = [[NSMutableArray alloc] init];
	
	NSArray *theBasicsArray = [NSArray arrayWithObjects:@"ls", @"pwd", @"cd", @"touch", @"mkdir", @"cp", @"mv", @"rm", @"su",nil];
	NSDictionary *theBasicsDict = [NSDictionary dictionaryWithObject:theBasicsArray forKey:@"Linux Commands"];
	
	NSArray *learningAboutArray = [NSArray arrayWithObjects:@"man", @"info", @"whereis", @"whatis", @"apropos", @"which", nil];
	NSDictionary *learningAboutDict = [NSDictionary dictionaryWithObject:learningAboutArray forKey:@"Linux Commands"];
	
	NSArray *buildingBlocksArray = [NSArray arrayWithObjects:@";", @"&&", @"||", @"$()", @"|", @">", @">>", @"<", nil];
    NSDictionary *buildingBlocksDict = [NSDictionary dictionaryWithObject:buildingBlocksArray forKey:@"Linux Commands"];

	[listOfItems addObject:theBasicsDict];
	[listOfItems addObject:learningAboutDict];
	[listOfItems addObject:buildingBlocksDict];
	
	
	copyListOfItems = [[NSMutableArray alloc] init];
	

	self.navigationItem.title = @"Linux Commands";
	
	
	self.tableView.tableHeaderView = searchBar;
	searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
	
	searching = NO;
	letUserSelectRow = YES;
}

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

#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
	if (searching)
		return 1;
	else
		return[listOfItems count];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	
	if (searching)
		return [copyListOfItems count];
	else {
		
		
		NSDictionary *dictionary =[listOfItems objectAtIndex:section];
		NSArray *array = [dictionary objectForKey:@"Linux Commands"];
		return [array count];
	}
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	
	if(searching)
		return @"Search Results";
	
	if(section == 0){
		return @"The Basics";
	} else if(section ==1) {
		return @"Learning About";
	} else {
		return @"Building Blocks";
	}

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	
	if(cell == nil)
		cell = [self getCellContentView:CellIdentifier];
	
	UILabel *lblTemp1 = (UILabel *)[cell viewWithTag:1];
	UILabel *lblTemp2 = (UILabel *)[cell viewWithTag:2];
	
	if(searching) {
		
		lblTemp1.text = [copyListOfItems objectAtIndex:indexPath.row];
		lblTemp2.text = @"";
	}
	else {
		
		
		NSDictionary *dictionary =[listOfItems objectAtIndex:indexPath.section];
		NSArray *array = [dictionary objectForKey:@"Linux Commands"];
		NSString *cellValue = [array objectAtIndex:indexPath.row];
		
		lblTemp1.text = cellValue;
		lblTemp2.text = @"Sub Value";
		
		[cellValue release];
	}
	
    return cell;
}

- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {
	
	CGRect CellFrame = CGRectMake(0, 0, 300, 60);
	CGRect Label1Frame = CGRectMake(10, 10, 290, 25);
	CGRect Label2Frame = CGRectMake(10, 33, 290, 25);
	UILabel *lblTemp;
	
	UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];
	
	
	lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
	lblTemp.tag = 1;
	[cell.contentView addSubview:lblTemp];
	[lblTemp release];
	
	
	lblTemp = [[UILabel alloc] initWithFrame:Label2Frame];
	lblTemp.tag = 2;
	lblTemp.font = [UIFont boldSystemFontOfSize:12];
	lblTemp.textColor = [UIColor lightGrayColor];
	[cell.contentView addSubview:lblTemp];
	[lblTemp release];
	
	return cell;
}

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

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	
	//Get the selected command
	
	NSString *selectedCommand = nil;
	
	if(searching)
		selectedCommand = [copyListOfItems objectAtIndex:indexPath.row];
	else {
	
		NSDictionary *dictionary =[listOfItems objectAtIndex:indexPath.section];
		NSArray *array = [dictionary objectForKey:@"Linux Commands"];
		selectedCommand = [array objectAtIndex:indexPath.row];
	}
	
	.
	
	NSLog(@"listOfItems has %d, accessing %d",[listOfItems count], indexPath.row);
	NSDictionary *dictionary =[listOfItems objectAtIndex:indexPath.section];
	NSArray *array = [dictionary objectForKey:@"Linux Commands"];
	NSString *cellValue = [array objectAtIndex:indexPath.row];
	
	if ([cellValue isEqual:@"ls"]){
		NSLog(@"ls");
		lsViewController *abo01 = [[lsViewController alloc] initWithNibName:@"lsView" bundle:nil];
		[self.navigationController pushViewController:abo01 animated:YES];
		NSLog(@"ls Worked");
		[abo01 release];
	}
	if ([cellValue isEqual:@"pwd"]){
		NSLog(@"pwd");
		pwdViewControll *abo02 = [[pwdViewControll alloc] initWithNibName:@"pwdView" bundle:nil];
		[self.navigationController pushViewController:abo02 animated:YES];
		NSLog(@"pwd Worked");
		[abo02 release];
		
	}
	
	if ([cellValue isEqual:@"cd"]){
		NSLog(@"cd");
		cdViewController *abo03 = [[cdViewController alloc] initWithNibName:@"cdView" bundle:nil];
		[self.navigationController pushViewController:abo03 animated:YES];
		NSLog(@"cd Worked");
		[abo03 release];
		
	}
	
}
- (NSIndexPath *)tableView :(UITableView *)theTableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	
	if(letUserSelectRow)
		return indexPath;
	else
		return nil;
}

- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath {
	
	return UITableViewCellAccessoryNone;
}

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
	
	[self tableView:tableView didSelectRowAtIndexPath:indexPath];
}

#pragma mark -
#pragma mark Search Bar 

- (void) searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar {
	
	if(searching)
		return;

	if(ovController == nil)
		ovController = [[OverlayViewController alloc] initWithNibName:@"OverlayView" bundle:[NSBundle mainBundle]];
	
	CGFloat yaxis = self.navigationController.navigationBar.frame.size.height;
	CGFloat width = self.view.frame.size.width;
	CGFloat height = self.view.frame.size.height;
	
	CGRect frame = CGRectMake(0, yaxis, width, height);
	ovController.view.frame = frame;	
	ovController.view.backgroundColor = [UIColor grayColor];
	ovController.view.alpha = 0.5;
	
	ovController.rvController = self;
	
	[self.tableView insertSubview:ovController.view aboveSubview:self.parentViewController.view];
	
	searching = YES;
	letUserSelectRow = NO;
	self.tableView.scrollEnabled = NO;
	
	self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] 
											   initWithBarButtonSystemItem:UIBarButtonSystemItemDone 
											   target:self action:@selector(doneSearching_Clicked:)] autorelease];
	
}

- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText {

	[copyListOfItems removeAllObjects];
	
	if([searchText length] > 0) {
		
		[ovController.view removeFromSuperview];
		searching = YES;
		letUserSelectRow = YES;
		self.tableView.scrollEnabled = YES;
		[self searchTableView];
	}
	else {
		
		[self.tableView insertSubview:ovController.view aboveSubview:self.parentViewController.view];
		
		searching = NO;
		letUserSelectRow = NO;
		self.tableView.scrollEnabled = NO;
	}
	
	[self.tableView reloadData];
}

- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar {
	
	[self searchTableView];
}

- (void) searchTableView {
	
	NSString *searchText = searchBar.text;
	NSMutableArray *searchArray = [[NSMutableArray alloc] init];
	
	for (NSDictionary *dictionary in listOfItems)
	{
		NSArray *array = [dictionary objectForKey:@"Linux Commands"];
		[searchArray addObjectsFromArray:array];
	}
	
	for (NSString *sTemp in searchArray)
	{
		NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
		
		if (titleResultsRange.length > 0)
			[copyListOfItems addObject:sTemp];
	}
	
	[searchArray release];
	searchArray = nil;
}

- (void) doneSearching_Clicked:(id)sender {
	
	searchBar.text = @"";
	[searchBar resignFirstResponder];
	
	letUserSelectRow = YES;
	searching = NO;
	self.navigationItem.rightBarButtonItem = nil;
	self.tableView.scrollEnabled = YES;
	
	[ovController.view removeFromSuperview];
	[ovController release];
	ovController = nil;
	
	[self.tableView reloadData];
}

- (void)dealloc {
	
	[ovController release];
	[copyListOfItems release];
	[searchBar release];
	[listOfItems release];
    [super dealloc];
}


@end

Last edited by katsogiannis; 06-30-2010 at 12:40 PM.
katsogiannis is offline   Reply With Quote
Old 06-30-2010, 01:19 PM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

I've never had any luck using a global search variable like that. If you look at the Apple examples, they make decisions based on which table is asking. I think this is because both tables can make requests at the same time.

See the table view link in my signature for an example.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Reply

Bookmarks

Tags
uiseachbar, uitableview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 328
7 members and 321 guests
anothermine, Chickenrig, Domele, givensur, michaelhansen, PixelInteractive, Sloshmonster
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,892
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:23 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0