2017-04-25 1 views
0

J'ai un UITableView et un UISearchBar. Lorsque l'application est chargée, je transmets une certaine liste à la source de la table. Lorsque j'entre une requête dans UISearchBar et que j'appuie sur le bouton de recherche, je lance un appel d'API pour aller chercher une nouvelle liste.Modifier la source de données UITableView sur le bouton de recherche UISearchBar, cliquez sur

Maintenant, je veux remplacer la source de la tableview avec la nouvelle liste I chercher à partir de l'API.

Je peux filtrer la liste passée initialement sur UISearchView Changement de texte, mais je veux fournir une liste complètement nouvelle à la table sur le clic de bouton de recherche.

Y at-il un moyen que je peux changer la source de la table avec la nouvelle liste en utilisant le UISearchView? ou dois-je créer une barre de recherche personnalisée?

J'ai essayé de transmettre une valeur nulle à la source de la table, puis de passer la nouvelle liste, mais cela ne fait rien.

Toute aide est appréciée

EDIT

MytableViewController

Initialize() 
     { 
      if (!string.IsNullOrEmpty(savedRelatedList)) 
       { 
        if (CrossConnectivity.Current.IsConnected) 
        { 

         loadingOverlay = new LoadingOverlay(UIScreen.MainScreen.Bounds, message); 
         this.View.Add(loadingOverlay); 

         //Fetch Related People 
         var relatedData = await O365Service.GetRelatedPeople(GRAPH_ACCESS_TOKEN); 
         if (relatedData != null) 
         { 
          relatedPeopleList = relatedData.Value.Where(d => !string.IsNullOrEmpty(d.userPrincipalName) && (!d.userPrincipalName.Equals(my_email))).ToList(); ; 
          if (relatedPeopleList != null && relatedPeopleList.Count > 0) 
          { 
           NSUserDefaults.StandardUserDefaults.SetString(JsonConvert.SerializeObject(relatedPeopleList.ToList()), "RelatedList"); 

          } 
          else 
          { 
           relatedPeopleList = null; 
          } 
         } 
         else 
         { 

         } 
         loadingOverlay.RemoveFromSuperview(); 
        } 
        else 
        { 
         DialogHelper.CreateAndShowDialog("Network Error", "Check your internet connectivity"); 

        } 
       } 

       relatedPeopleList = new List<PeopleRelated>(); 
       relatedPeopleList.Add(new PeopleRelated { displayName = "Test", }); 

       searchNewPeopleBar.CancelButtonClicked += delegate 
       { 
        searchNewPeopleBar.Text = ""; 
        isSearch = false; 
        peopleList = null; 
        tablePeopleSearch.ReloadData(); 
        searchNewPeopleBar.ResignFirstResponder(); 
       }; 

       var dataSource = new PeopleSearchSource(this); 
       tablePeopleSearch.Source = dataSource; 
       searchNewPeopleBar.SearchButtonClicked += SearchBar_SearchButtonClicked; 
     } 


     searchNewPeopleBar.CancelButtonClicked += delegate 
       { 
        searchNewPeopleBar.Text = ""; 
        isSearch = false; 
        peopleList = null; 
        tablePeopleSearch.ReloadData(); 
        searchNewPeopleBar.ResignFirstResponder(); 
       }; 

     private async void SearchBar_SearchButtonClicked(object sender, EventArgs e) 
     { 
      var searchText = searchNewPeopleBar.Text; 
      if(string.IsNullOrEmpty(searchText)) 
      { 
       isSearch = false; 
      } 
      isSearch = true; 

      //Make api call and get new list 
      tablePeopleSearch.ReloadData(); 
     } 

PeopleSearchSource

public override nfloat GetHeightForRow(UITableView tableView, Foundation.NSIndexPath indexPath) 
      { 
       return 150; 
      } 
      public override nint RowsInSection(UITableView tableview, nint section) 
      { 
       if(peopleHomeController.isSearch) 
       { 
        return peopleHomeController.peopleList.Count; 
       } 
       else 
       { 
        return peopleHomeController.relatedPeopleList.Count; 

       } 

      } 

      public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) 
      { 
       cell = peopleHomeController.tablePeopleSearch.DequeueReusableCell("people_search_cell") as PeopleSearchCell; 


       if(peopleHomeController.isSearch) 
       { 
        var data = peopleHomeController.peopleList.ElementAt(indexPath.Row); 
        cell.UpdateCell(data); 
       } 
       else 
       { 
        var data = peopleHomeController.relatedPeopleList.ElementAt(indexPath.Row); 
        cell.UpdateCell(data); 
       } 


       return cell; 
      } 
+0

peut vous comprendre le code objectif de c? – KKRocks

+0

@KKRocks Oui je peux :) – user3034944

+0

Utilisez des énumérations et manipulez la source de données en conséquence. –

Répondre

0

Vous devez gérer le drapeau délégué SearchBar.

délégué SearchBar

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{ 
    searchBar.text = @""; 
    isSearch = NO ; 
    [arrFilter removeAllObjects]; 
    [searchTblView reloadData]; 
    [searchBar resignFirstResponder]; 
} 

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{ 
    NSLog(@"search text :%@",searchBar.text); 
    NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet]; 
    NSString *strSearch = [searchBar.text stringByTrimmingCharactersInSet:whitespace]; 
    isSearch = YES; 
    if ([strSearch isEqualToString:@""]) { 
     isSearch = NO; 
    } 
    // call api here and reload tableview 
} 

Délégué TableView

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    if (isSearch) { 
     return arrFilter.count;; 
    } 
    return arrMain.count; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    NSString *identifier = @"searchCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 

    NSString *strname = [isSearch ? arrFilter :arrMain objectAtIndex:indexPath.row ]; // change your array here 
    UILabel *lblRecipeName = (UILabel *)[cell viewWithTag:101]; 
    lblRecipeName.text = strname; 

    return cell; 
} 
+0

Merci pour la réponse. Je devrais utiliser juste l'UISearchView et pas l'UISearchView avec le contrôleur d'affichage juste? – user3034944

+0

c'est seulement la vue de recherche. – KKRocks

+0

J'ai essayé cette solution. Mais en quelque sorte ma GetCell()/cellForRowAtIndexPath n'est pas appelée. numberOfRowsInSection() est appelé si – user3034944