单击 UISearchBar 搜索按钮更改 UITableView 数据源
Change UITableView data source on UISearchBar search button click
我有一个 UITableView
和一个 UISearchBar
。当应用程序加载时,我将某个列表传递给 table 源。当我在 UISearchBar
中输入查询并点击搜索按钮时,我会调用 API 来获取新列表。
现在我想用从 API 中获取的新列表替换 table 视图的源代码。
我可以过滤最初在 UISearchView
文本更改时传递的列表,但我想在单击搜索按钮时向 table 提供一个全新的列表。
有没有一种方法可以使用 UISearchView
将 table 源更改为新列表?还是我需要创建自定义搜索栏?
我曾尝试将空值传递给 table 源,然后传递新列表,但这没有任何作用。
感谢任何帮助
编辑
我的tableViewController
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;
}
您需要管理 searchBar Delegate 上的标志。
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
}
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;
}
我有一个 UITableView
和一个 UISearchBar
。当应用程序加载时,我将某个列表传递给 table 源。当我在 UISearchBar
中输入查询并点击搜索按钮时,我会调用 API 来获取新列表。
现在我想用从 API 中获取的新列表替换 table 视图的源代码。
我可以过滤最初在 UISearchView
文本更改时传递的列表,但我想在单击搜索按钮时向 table 提供一个全新的列表。
有没有一种方法可以使用 UISearchView
将 table 源更改为新列表?还是我需要创建自定义搜索栏?
我曾尝试将空值传递给 table 源,然后传递新列表,但这没有任何作用。
感谢任何帮助
编辑
我的tableViewController
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;
}
您需要管理 searchBar Delegate 上的标志。
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
}
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;
}