如何在UISearchBar结束编辑后重新加载UITableView

时间:2011-08-08 07:13:21

标签: objective-c iphone uisearchbar

我创建了一个UISearchBar。它工作但不是我想要的方式。如果我在UISearchbar上输入任何第一个字母,然后我点击SearchButton,它就不起作用但是当我按下下一个控制器然后我回来时,我在TableView中看到了我的搜索结果。我的TableView第一次没有刷新。

这是我的自定义单元类和我的控制器类

@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      

4 个答案:

答案 0 :(得分:3)

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar 
{ 
    searchBar.showsCancelButton = NO; 
    [myTableView reloadData];
}
在我到达这篇文章之前,我也遇到了这方面的困难。我查看了你的函数并决定在上面的函数中抛出重载数据,现在工作正常!希望这有帮助!

答案 1 :(得分:0)

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

答案 2 :(得分:0)

在搜索表消失后,您可以使用此委托方法重新加载表:

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar

答案 3 :(得分:0)

Swift版

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