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 02-10-2011, 11:30 AM   #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 problem with UITableview

Hello all.

i am using the code from iPhone SDK Articles (UITableView ? Indexed table view) and i have some problems with the search function.
i have one view (xib) to each of my rows on the tableview and it works fine. the problem is that when i use the searchbar and i click on the selected row it displays the wrong detilview (xib).
can someone help with this as i am new on xcode.

code below:
Code:
- (void)viewDidLoad {
    [super viewDidLoad];
	
	//Initialize the array.
	listOfItems = [[NSMutableArray alloc] init];
	
	NSArray *countriesToLiveInArray = [NSArray arrayWithObjects:@"Austria", @"August", @"Australia", nil];
	NSDictionary *countriesToLiveInDict = [NSDictionary dictionaryWithObject:countriesToLiveInArray forKey:@"Dictionary"];
	
.
.
.
.
.
	
	NSArray *countriesYArray = [NSArray arrayWithObjects:@"YoYo", @"Yes", nil];
	NSDictionary *countriesYDict = [NSDictionary dictionaryWithObject:countriesYArray forKey:@"Dictionary"];
	
	NSArray *countriesZArray = [NSArray arrayWithObjects:@"Zoe", @"Zara", @"Zero", @"", @"", nil];
	NSDictionary *countriesZDict = [NSDictionary dictionaryWithObject:countriesZArray forKey:@"Dictionary"];

	
	[listOfItems addObject:countriesToLiveInDict];
	[listOfItems addObject:countriesLivedInDict];
	.
        .
	[listOfItems addObject:countriesXDict];
	[listOfItems addObject:countriesYDict];
	[listOfItems addObject:countriesZDict];
	
	//Initialize the copy array.
	copyListOfItems = [[NSMutableArray alloc] init];
	
	//Set the title
	self.navigationItem.title = @"Dictionary";
	
	//Add the search bar
	self.tableView.tableHeaderView = searchBar;
	searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
	
	searching = NO;
	letUserSelectRow = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

#pragma mark Table view methods

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

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	
	if (searching)
		return [copyListOfItems count];
	else {
		
		//Number of rows it should expect should be based on the section
		NSDictionary *dictionary =[listOfItems objectAtIndex:section];
		NSArray *array = [dictionary objectForKey:@"Dictionary"];
		return [array count];
	}
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	
	if(searching)
		return @"Search Results";
	
	if(section == 0){
		return @"A";
	} else if (section == 1){
		return @"B";
	} else if (section == 2){
		return @"C";
	.
        .
	} else if (section == 22){
		return @"W";
	} else if (section == 23){
		return @"X";
	} else if (section == 24){
		return @"Y";
	} else {
		return @"Z";
	}
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
	
	if(searching)
		return nil;
	
	NSMutableArray *tempArray = [[NSMutableArray alloc] init];
	[tempArray addObject:@"A"];
	[tempArray addObject:@"B"];
	.
        .
	[tempArray addObject:@"X"];
	[tempArray addObject:@"Y"];
	[tempArray addObject:@"Z"];

	return tempArray;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {

	if(searching)
		return -1;
	
	
	return index % 26;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Set up the cell...
	
	if(searching) 
		cell.textLabel.text = [copyListOfItems objectAtIndex:indexPath.row];
	else {
		
		//First get the dictionary object
		NSDictionary *dictionary =[listOfItems objectAtIndex:indexPath.section];
		NSArray *array = [dictionary objectForKey:@"Dictionary"];
		NSString *cellValue = [array objectAtIndex:indexPath.row];
		cell.textLabel.text = cellValue;
	}

    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	
	NSLog(@"listOfItems has %d, accessing %d",[listOfItems count], indexPath.row);
	NSDictionary *dictionary =[listOfItems objectAtIndex:indexPath.section];
	NSArray *array = [dictionary objectForKey:@"Dictionary"];
	NSString *cellValue = [array objectAtIndex:indexPath.row];
	
	
	if ([cellValue isEqual:@"Austria"]){
		NSLog(@"Austria");
		austriaController *austria = [[austriaController alloc] initWithNibName:@"austriaView" bundle:nil];
		[self.navigationController pushViewController:austria animated:YES];
		NSLog(@"Austria");
		[austria release];
	}
	
	if ([cellValue isEqual:@"August"]){
		NSLog(@"August");
		augustController *august = [[augustController alloc] initWithNibName:@"augustView" bundle:nil];
		[self.navigationController pushViewController:august animated:YES];
		NSLog(@"August");
		[august release];
	}
}

- (NSIndexPath *)tableView :(UITableView *)theTableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	
	if(letUserSelectRow)
		return indexPath;
	else
		return nil;
}
 

- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath {
	
	//return UITableViewCellAccessoryDetailDisclosureButton;
	//return UITableViewCellAccessoryDisclosureIndicator;
	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:@"Dictionary"];
		[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
Thanks in advance
__________________
To follow the path:
look to the master, follow the master, walk with the master, see through the master, become the master.


katsogiannisc@gmail.com


Larissa FC
katsogiannis is offline   Reply With Quote
Old 02-10-2011, 11:46 AM   #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

See the table view link in my signature for assistance.
__________________
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
Old 02-10-2011, 01:49 PM   #3 (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

Thanks for the quick reply.
Can you bee more specific. i am new on xcode. did you manage to find any problems on the code that i post.
Your help will be very appreciated.
__________________
To follow the path:
look to the master, follow the master, walk with the master, see through the master, become the master.


katsogiannisc@gmail.com


Larissa FC
katsogiannis is offline   Reply With Quote
Old 02-10-2011, 02:00 PM   #4 (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

You aren't doing anything different in didSelectRow if you are currently searching.
__________________
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
Old 02-10-2011, 02:38 PM   #5 (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
Thumbs up

Quote:
Originally Posted by BrianSlick View Post
You aren't doing anything different in didSelectRow if you are currently searching.
Oh man you are very very tricky guy.
i had a careful look under my didSelectRowAtIndexPath as you said and it clicked.

i add

if(searching)
cellValue = [copyListOfItems objectAtIndex:indexPath.row];


and problem solved.

Many many thanks....
Brilliant
__________________
To follow the path:
look to the master, follow the master, walk with the master, see through the master, become the master.


katsogiannisc@gmail.com


Larissa FC
katsogiannis is offline   Reply With Quote
Reply

Bookmarks

Tags
searchbar, 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: 373
14 members and 359 guests
blasterbr, buggen, Clouds, dre, EvilElf, jeroenkeij, jimmyon122, Mah6447, n00b, nyoe, pungs, Sami Gh, stanny, toon4413
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,667
Threads: 94,121
Posts: 402,900
Top Poster: BrianSlick (7,990)
Welcome to our newest member, host number one
Powered by vBadvanced CMPS v3.1.0

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