2017-04-25 74 views
0

我有一個UITableView和一個UISearchBar。當應用程序加載時,我將某個List傳遞給表源。當我在UISearchBar中輸入查詢並點擊搜索按鈕時,我會進行API調用以獲取新列表。更改UISearchBar搜索按鈕上的UITableView數據源點擊

現在我想用我從API獲取的新列表替換tableview的源代碼。

我可以過濾最初通過的列表UISearchView文本更改,但我想提供一個全新的列表到搜索按鈕單擊表上。

有沒有一種方法可以使用UISearchView更改表格源與新列表?還是我需要創建一個自定義搜索欄?

我試過將一個空值傳遞給表源,然後傳遞新的列表,但是這並沒有做任何事情。

任何幫助表示讚賞

編輯

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

你可以瞭解目標C代碼? – KKRocks

+0

@KKRocks是的,我可以:) – user3034944

+0

利用枚舉並相應地操作數據源。 –

回答

0

您需要在搜索欄委託管理的標誌。

搜索欄委託

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

的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

感謝您的答覆。我應該只使用UISearchView而不使用帶顯示控制器的UISearchView? – user3034944

+0

它只是搜索視圖。 – KKRocks

+0

我試過這個解決方案。但不知何故我的GetCell()/ cellForRowAtIndexPath沒有被調用。 numberOfRowsInSection()被調用,雖然 – user3034944