UISearchBar和TableView

问题描述:

嘿,任何人,即时尝试使工作一个UISearchView,与表视图。我使用TBXML获取表格的数据,从php解析一些xml。UISearchBar和TableView

当我设置为显示日志结果时,它给了我,但不添加到表查看我的结果。 什么即时做错了?

@synthesize tableData; 

@synthesize theSearchBar; 
@synthesize theTableView; 
@synthesize resultados; 

//View load 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.tableData =[[NSMutableArray alloc]init]; 
} 

- (void)viewDidAppear:(BOOL)animated { 
    [self.theSearchBar becomeFirstResponder]; 
    [super viewDidAppear:animated]; 
} 

// Search 

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { 
    [searchBar setShowsCancelButton:YES animated:YES]; 
    self.theTableView.allowsSelection = NO; 
    self.theTableView.scrollEnabled = NO; 
} 

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { 
    [email protected]""; 

    [searchBar setShowsCancelButton:NO animated:YES]; 
    [searchBar resignFirstResponder]; 
    self.theTableView.allowsSelection = YES; 
    self.theTableView.scrollEnabled = YES; 
} 
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { 




    NSString *endereco = [[NSString alloc] initWithFormat:@"http://localhost/icomm/test.php?nome=%@", searchBar.text]; 


    TBXML * tbxml = [[TBXML tbxmlWithURL:[NSURL URLWithString:endereco]] retain]; 
    TBXMLElement * rootXMLElement = tbxml.rootXMLElement; 

    TBXMLElement * item = [TBXML childElementNamed:@"item" parentElement:rootXMLElement]; 

    resultados = [[NSMutableArray alloc] init]; 


    while (item) { 

     NSString * produtoNome = [TBXML textForElement:item]; 

     NSLog(@"%@", produtoNome); 


     [resultados addObject:produtoNome]; 

     item = [TBXML nextSiblingNamed:@"item" searchFromElement:item 
         ]; 


    } 



    [searchBar setShowsCancelButton:NO animated:YES]; 
    [searchBar resignFirstResponder]; 
    self.theTableView.allowsSelection = YES; 
    self.theTableView.scrollEnabled = YES; 

    [self.tableData removeAllObjects]; 
    //[self.tableData addObjectsFromArray:resultados]; 
    [self.theTableView reloadData]; 
} 


// Table 


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

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

    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] 
       initWithStyle:UITableViewCellStyleDefault 
       reuseIdentifier:MyIdentifier] autorelease]; 
    } 

    NSString *nomeProd = [resultados objectAtIndex:indexPath.row]; 

    cell.textLabel.text = nomeProd; 

    return cell; 
} 


//Dealloc 

- (void)dealloc { 
    [theTableView release], theTableView = nil; 
    [theSearchBar release], theSearchBar = nil; 
    [tableData dealloc]; 
    [super dealloc]; 
} 

谢谢!

您需要将您的搜索结果存储在另一个数组中,并告诉您的数据源在搜索时显示搜索结果。