开发者

How to reload UITableView after the UISearchBar ends editing

开发者 https://www.devze.com 2023-03-26 08:25 出处:网络
I created a UISearchBar. It is working but not the way I want. If I enter any first letter on the UISearchbar and then I click on the SearchButton, it doesn\'twork but when I push the next controller

I created a UISearchBar. It is working but not the way I want. If I enter any first letter on the UISearchbar and then I click on the SearchButton, it doesn't work but when I push the next controller and I come back, then I see my search result in TableView. The first time my TableView does not refresh.

Here is my custom cell class and my controller class

@synthesize myTableView;
@synthesize tabledata;

#pragma mark -
#pragma mark Initialization
#pragma mark -
#pragma mark View lifecycle

-(void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    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];
    NSLog(@"*************:%&",list);
    list=[[NSMutableArray alloc]init]; 
    tabledata =[[NSMutableArray alloc]init]; 
    list = [app.journeyList retain];
    [tabledata addObjectsFromArray:list];
}

-(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 1;
}

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

    return [tabledata count];
}

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

{ 
    NSLog(@"*************:%&",list);
    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 = [tabledata objectAtIndex:indexPath.row]; 
    cell.namelbl.text = newjoruneobject.journeyname;  
    cell.distancelbl.text = newjoruneobject.journeylocation;  
    cell.infolbl.text = newjoruneobject.journeydescription;   
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 
    return cell; 
}

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

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar 
{ 
    searchBar.showsCancelButton = NO; 
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 开发者_开发技巧
{
    [tabledata removeAllObjects];// remove all data that belongs to previous search

    if([sBar.text length] != 0)//|| searchText==nil) 
    { 
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"journeyname contains[cd] %@", searchBar.text];
        [tabledata addObjectsFromArray:[list filteredArrayUsingPredicate: predicate]];
        [myTableView reloadData];
        return;
         }
    NSLog(@"Counter:-'%d'",[tabledata count]);
    [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:list];
    @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]; 
    myTableView.allowsSelection = YES;
    myTableView.scrollEnabled = YES;
    [myTableView reloadData];
}

- (void)dealloc {
    [super dealloc];
    [mLable1 release];
    [myTableView release], myTableView = nil;
    [tabledata dealloc];
}

@end      


- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar 
{ 
    searchBar.showsCancelButton = NO; 
    [myTableView reloadData];
}

I was having difficulties with this as well until I got to this post. I looked through your functions and decided to throw the reload data in the function above it works fine now! Hope this helps!


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [tableView reloadData];
}


You can use this delegate method to reload table, after search table disappears:

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar


Swift version

func searchBarTextDidEndEditing(searchBar: UISearchBar) {
    searchActive = false //Local variable used to manipulate your cells
    self.newsTableView.reloadData()
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号