0

hi friend i get here some problem please me i want search string in table view by searchbar i create all function of searchbar then i call in viewdidload method but it not working why

i am creating NSMutableArray *tabledata; i am passing all value of app.journeylist in tabledata but it not wotking and application will be crash this is my controller class

//
//  TJourneylistController.m
//  Journey
//
//  Created by rocky on 3/17/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "TJourneylistController.h"
#import "TAddNewJourney.h"
#import "TJourneyListCell.h"
#import "JourneyAppDelegate.h"
#import "TJourneyTabBar.h"
#import "global.h"
#import "TPastJourneyTabBar.h"
#import "NewJourney.h"
#import "TStartnewjourney.h"
#import "TMapViewController.h"
#import "TShareController.h"
#import "TSpeedometerController.h"
#import "TReviewsController.h"
#define DATABASE_NAME @"journey"
@implementation TJourneylistController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    //self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(add_clicked:)];
    app = (JourneyAppDelegate *)[[UIApplication sharedApplication]delegate];
    sBar =[[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)]; 
    sBar.delegate=self;   
    [self.view addSubview:sBar];
    searcheddata =[[NSMutableArray alloc]init];
    tabledata =[[NSMutableArray alloc]init];     
    [tabledata addObject:app.journeyList];
    app.journeyList=[[NSMutableArray alloc]init]; 
    for(char c = 'A';c<='Z';c++)
        [app.journeyList addObject:[NSString stringWithFormat:@"%cTestString",c]];  //list = [app.journeyList retain];

}

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


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [tabledata count];


}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";


    TJourneyListCell *cell =(TJourneyListCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[TJourneyListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        NSLog(@"%@",cell);
    }
    NewJourney *newjoruneobject = [app.journeyList objectAtIndex:indexPath.row];
    switch (indexPath.section) {
        case 0:

            cell.namelbl.text = newjoruneobject.journeyname;
            cell.distancelbl.text = newjoruneobject.journeylocation;
            cell.infolbl.text     = newjoruneobject.journeydescription;
            cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
            break;
            case 1:
            cell.namelbl.text = @"Hello";

                break;

        default:
            break;
    }

    return cell;


}
#pragma mark UISearchBarDelegate 
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar 
{ 
    // only show the status bar’s cancel button while in edit mode 
    sBar.showsCancelButton = YES; 
    sBar.autocorrectionType = UITextAutocorrectionTypeNo; 
    // flush the previous search content 
    [tabledata removeAllObjects]; 
} 
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar 
{ 
    sBar.showsCancelButton = NO; 
}


- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 
{ 
    [tabledata removeAllObjects];// remove all data that belongs to previous search 
    if([searchText isEqualToString:@""]|| searchText==nil){ 
        [myTableView reloadData]; 
        return; 
    } 
    NSInteger counter = 0; 
    for(NSString *name in app.journeyList) 
    { 
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init]; 
        NSRange r = [name rangeOfString:searchText]; 
        if(r.location != NSNotFound) 
        { 
            if(r.location== 0)//that is we are checking only the start of the names. 
            { 
                [tabledata addObject:name];
            } 
        } 
        counter++; 
        [pool release]; 
    } 
    [myTableView reloadData];
}




- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar 
{ // if a valid search was entered but the user wanted to cancel, bring back the main list content 
    [tabledata removeAllObjects]; 
    [tabledata addObjectsFromArray:app.journeyList];
    @try
    { 
        [myTableView reloadData];
    }
    @catch(NSException *e)
    { 
    } 
    [sBar resignFirstResponder];
    sBar.text = @""; 
} 

// called when Search (in our case “Done”) button pressed 
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar 
{ 
    [searchBar resignFirstResponder]; 
}



#pragma mark -
#pragma mark Table view delegate



- (void)dealloc {
    [super dealloc];
    //[self.tableView release];
    [mLable1 release];
    //[mLable2 release];
    //[mLable3 release];
    //[mLable4 release];
}


@end
Amy Worrall
  • 16,250
  • 3
  • 42
  • 65
Rocky
  • 1,427
  • 1
  • 28
  • 65
  • -[__NSArrayM journeyname]: unrecognized selector sent to instance 0x5bd8720 2011-08-03 15:05:43.224 Journey[16225:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM journeyname]: unrecognized selector sent to instance 0x5bd8720' – Rocky Aug 03 '11 at 11:28
  • @parthbhatt please check error – Rocky Aug 03 '11 at 11:29
  • actually you are trying to call `journeyname` method on an object which is an array. So it has nothing to do with searching logic. Your app crashes because you are calling method on an array. For more detailed explanation, Post your code where the `journeyname` mehtod is being called. Hope this helps you – Parth Bhatt Aug 03 '11 at 13:04
  • For more info you can refer to this link http://stackoverflow.com/questions/4164816/tableview-uisearchbar-on-tab-bar-controller-crashes-while-searching – Parth Bhatt Aug 03 '11 at 13:07
  • Check out the following links. They may help you. 1. http://stackoverflow.com/questions/1302962/uisearchbar-sample-code 2. http://blog.webscale.co.in/?p=228 – iOS Aug 03 '11 at 11:14
  • I think your problem is similar to the one mentioned in this link. Please check it out. http://stackoverflow.com/questions/4164816/tableview-uisearchbar-on-tab-bar-controller-crashes-while-searching – Parth Bhatt Aug 03 '11 at 13:08

1 Answers1

2

Write this code into your .h file:

UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource

IBOutlet UISearchBar *sbArray;
IBOutlet UITableView *tvData;

Write this code into your .m file:

- (void)viewDidLoad
{
    journeyList = [[NSMutableArray alloc] init];
    FinalList = [[NSMutableArray alloc] init];

    for(char c = 'A';c<='Z';c++)
        [journeyList addObject:[NSString stringWithFormat:@"%cTestString",c]];

    for(char c = 'A';c<='Z';c++)
        [FinalList addObject:[NSString stringWithFormat:@"%cTestString",c]];

    [tvData reloadData];
    [super viewDidLoad];
}

 (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [FinalList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"anyCell"];

    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"anyCell"] autorelease];
    }

    cell.textLabel.text = [FinalList objectAtIndex:indexPath.row];

    return cell;
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)search
{
    [search resignFirstResponder];
}

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar 
{
    [searchBar resignFirstResponder];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    FinalList = [[NSMutableArray alloc] init];

    if([sbArray.text isEqualToString:@""]|| searchText == nil)
    { 
        [tvData reloadData]; 
        return; 
    } 
    NSInteger counter = 0; 
    for(NSString *name in journeyList) 
    { 
        NSRange r = [name rangeOfString:sbArray.text]; 
        if(r.location != NSNotFound) 
        { 
            if(r.location== 0)//that is we are checking only the start of the names. 
            { 
                [FinalList addObject:name];
            } 
        } 
        counter++; 
    } 
    NSLog(@"Counter :- '%d'",[FinalList count]);

    [tvData reloadData];
}
halfer
  • 19,824
  • 17
  • 99
  • 186
SJS
  • 2,647
  • 1
  • 17
  • 34