gpt4 book ai didi

ios - 在 UISearchBar 处于事件状态时更新 UITableView

转载 作者:行者123 更新时间:2023-11-29 12:17:08 26 4
gpt4 key购买 nike

我正在编写一个 iOS 聊天应用程序,其中我必须显示带有搜索栏的用户列表。我每 2 分钟从服务器获取更新的用户列表。

我遇到的问题是

案例:1如果我激活了搜索栏并且在更新数据源时调用了重新加载表,现在如果我取消搜索,我将得不到原始表。仅显示过滤后的结果。

案例:2如果我的搜索栏处于事件状态并且我只是更新数据源而不是重新加载表,现在如果我取消搜索,它会循环到 cellforrowatindexpath 的次数与新数据源中的元素一样多,但它不会显示更新的元素。事实上,它混淆了元素,因为它考虑了新的索引路径但使用了旧数据。

乱七八糟的意思是,

  1. 假设旧数据源是 a1、a2、a3 和 a4,头像图像是 a1、a2、a3、a4。

  2. 新数据源是 a5、a1、a2、a3、a4,图像为 a5、a1、a2、a3、a4。

  3. 所以我得到的困惑 View 是a1 和 a5 图片,a2 与 a1 图片,a3 与 a4 图片没有图像的a4。

4 个单元而不是 5 个。

案例:3(正常工作)如果我激活了搜索栏并且我什至没有更新数据源,那么当我取消搜索时(如预期的那样)什么也不会发生。我必须重新加载表格才能获得更新的 View 。

我想要的是,即使搜索处于事件状态,它也应该更新表格,并且当我取消搜索时,我应该获得更新的表格 View 。我知道这可以通过单击取消按钮时重新加载表来实现,但我正试图为此找到一个优雅的解决方案。

此外,请告诉我为什么会这样工作背后的概念(内部工作原理)。

我的代码:

- (void)refreshUserList
{
if(dtclass.updatedUserList && [resultsArray count] ==0)
{
userArray = dtclass.getUpdatedUserList;
[self.tableView reloadData];
[dtclass setUpdatedUserList:NO];
}
else if(dtclass.updatedUserList && [resultsArray count] !=0)
{
//Uncomment for Case:1
/*userArray = dtclass.getUpdatedUserList;
[dtclass setUpdatedUserList:NO];
[self.tableView reloadData];*/

//Uncomment for Case:2
/*userArray = dtclass.getUpdatedUserList;
[dtclass setUpdatedUserList:NO];*/

//remove else if code for Case:3
}
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{

if (resultsArray.count != 0)
{
return [resultsArray count];
}

return userArray.count;
}


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

[tableView setSeparatorInset:UIEdgeInsetsMake(0, 70, 0, 0)];

GlobalListTableViewCell *cell = (GlobalListTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"GlobalListCell"];

if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"GlobalListTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
//custome class User
User *currentUser;

if(resultsArray.count !=0)
{
currentUser = [resultsArray objectAtIndex:indexPath.row];
}
else
{
currentUser = [userArray objectAtIndex:indexPath.row];
}

cell.nameLabel.text = currentUser.getName;
cell.timeLabel.text = @"5 pm";

dispatch_async(fetchImage, ^{

if([[currentUser getAvatarUrl] length]!=0)
{
[self loadImagesWithURL:[NSString stringWithFormat:@"%@%@",@"http:",[currentUser getAvatarUrl]] IndexPath:indexPath];
}
});


cell.avatarImage.image = [UIImage imageNamed:@"defaultUser.png"];
cell.messageLabel.text = @"No message";

return cell;
}

-(void) loadImagesWithURL:(NSString *)imageURL IndexPath:(NSIndexPath *)indexPath
{
NSURL *url = [NSURL URLWithString:imageURL];
NSData *data = [[NSData alloc ] initWithContentsOfURL:url];

UIImage *img = [UIImage imageWithData:data];

dispatch_async(dispatch_get_main_queue(), ^{

GlobalListTableViewCell *cell = (GlobalListTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
cell.avatarImage.image = img;

});
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath
{
return 60;
}


- (void)filterContentForSearchText:(NSString*)searchText scope: (NSString*)scope
{
resultsArray = [[NSArray alloc] init];
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF.getName contains[c] %@", searchText];

resultsArray = [userArray filteredArrayUsingPredicate:resultPredicate];
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope: [[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex: [self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];


return YES;
}

- (void)searchBarCancelButtonClicked:(UISearchBar *) aSearchBar {
aSearchBar.text = nil;
}

最佳答案

尝试使用此代码进行搜索..

- (void)filterContentForSearchText:(NSString*)searchText
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.getName contains[c] %@",searchText];
_filteredGarages = [NSMutableArray arrayWithArray:[_allGarages filteredArrayUsingPredicate:predicate]];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
[_filteredGarages removeAllObjects];
if([searchText length] != 0) {
[self filterContentForSearchText:searchText];
}
[self.tableView reloadData];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
isSearching = NO;
[self.tableView reloadData];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
if([searchBar.text isEqualToString:@""])
isSearching = NO;
else isSearching = YES;
[self.tableView reloadData];
}

-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
if([searchBar.text isEqualToString:@""])
isSearching = NO;
else isSearching = YES;
[self.tableView reloadData];
}

- (void)viewDidLoad {
[super viewDidLoad];
resultsArray = [[NSMutableArray alloc] init];
isSearching = NO;
userArray = [[NSMutableArray alloc] init];
userArray = [//GetYourData//];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [resultsArray count];
} else {
return [userArray count];
}
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Do you table view cell job here ...

if (tableView == self.searchDisplayController.searchResultsTableView) {
isSearching = YES;
//fill the cell using resultsArray
} else {
//fill the cell using userArray
}
}

关于ios - 在 UISearchBar 处于事件状态时更新 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31740574/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com