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