02-10-2011, 11:30 AM
#1 (permalink )
ACMT
Join Date: Jun 2010
Location: Chandlers Ford, UK
Posts: 39
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
02-10-2011, 11:46 AM
#2 (permalink )
Emphasizing Fundamentals
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
See the table view link in my signature for assistance.
02-10-2011, 01:49 PM
#3 (permalink )
ACMT
Join Date: Jun 2010
Location: Chandlers Ford, UK
Posts: 39
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
02-10-2011, 02:00 PM
#4 (permalink )
Emphasizing Fundamentals
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
You aren't doing anything different in didSelectRow if you are currently searching.
02-10-2011, 02:38 PM
#5 (permalink )
ACMT
Join Date: Jun 2010
Location: Chandlers Ford, UK
Posts: 39
Quote:
Originally Posted by
BrianSlick
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
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» 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