Ios UISearchBar搜索未清除旧单元格

Ios UISearchBar搜索未清除旧单元格,ios,objective-c,uitableview,uisearchbar,uisearchdisplaycontroller,Ios,Objective C,Uitableview,Uisearchbar,Uisearchdisplaycontroller,我试图在自定义的UITableViewController中实现UISearchBar,并以编程方式完成(不使用IB)。我让搜索功能工作并返回正确的字段,但它在完整列表单元格上显示搜索的单元格: 如您所见,新搜索的字段是可滚动和可选择的。只是没有移除旧的细胞 这是我的.h文件: @interface TestTableViewController : UITableViewController <UISearchBarDelegate, UISearchDisplayDelegate&

我试图在自定义的
UITableViewController
中实现
UISearchBar
,并以编程方式完成(不使用IB)。我让搜索功能工作并返回正确的字段,但它在完整列表单元格上显示搜索的单元格:

如您所见,新搜索的字段是可滚动和可选择的。只是没有移除旧的细胞

这是我的.h文件:

@interface TestTableViewController : UITableViewController <UISearchBarDelegate, UISearchDisplayDelegate>

@property (strong, nonatomic) NSArray *boundaries;

@end
以及如何调用表视图:

    TestTableViewController *tableViewController = [[TestTableViewController alloc] initWithStyle:UITableViewStylePlain];
    tableViewController.boundaries = [group.boundaries allObjects];
    tableViewController.contentSizeForViewInPopover = POPOVER_SIZE;

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:tableViewController];
    navController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    navController.navigationBar.barStyle = UIBarStyleBlack;

    self.myPopoverController = [[UIPopoverController alloc] initWithContentViewController:navController];
    self.myPopoverController.delegate = self;

    [self.myPopoverController presentPopoverFromRect:button.frame inView:button.superview permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

你知道我做错了什么或遗漏了什么吗?

问题是
UISearchDisplayController
正在使用另一个
UITableView
而不是视图控制器自己的。您可以通过在
-tableView:cellforrowatinexpath:
中记录
tableView
来验证这一点

您可以使用
UISearchBar
,而不使用
UISearchDisplayController
,以更好地控制搜索和显示逻辑

此外,如果应用程序在iOS 8之前不支持任何版本,请考虑使用。我没有试过,但它似乎给你更多的控制。检查iOS 8中已弃用的

UISearchDisplayController

试试这个

searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 41.0)];
    [self.view addSubview:searchBar];
    searchBar.delegate=self;

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    if (isSearching) {
        return [filteredContentList count];
    }
    else {
        return [titlearray count];
    }
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableCell";

    SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    if (isSearching)
    {
        cell.nameLabel.text = [filteredContentList objectAtIndex:indexPath.row];
        cell.thumbnailImageView.image =[filteredImgArray objectAtIndex:indexPath.row];
    }
    else
    {
        cell.thumbnailImageView.image = [imagearray objectAtIndex:indexPath.row];
        cell.nameLabel.text = [titlearray objectAtIndex:indexPath.row];
    }
    return cell;
}
- (void)searchTableList {
    NSString *searchString = searchBar.text;

    for (int i=0; i<titlearray.count; i++) {
        NSString *tempStr=[titlearray objectAtIndex:i];
        NSComparisonResult result = [tempStr compare:searchString options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchString length])];
        if (result == NSOrderedSame)
        {
            [filteredContentList addObject:tempStr];
            [filteredImgArray addObject:[imagearray objectAtIndex:i]];
        }
    }

}

#pragma mark - Search Implementation

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    isSearching = YES;
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    NSLog(@"Text change - %d",isSearching);

    //Remove all objects first.
    [filteredContentList removeAllObjects];
    [filteredImgArray removeAllObjects];

    if([searchText length] != 0) {
        isSearching = YES;
        [self searchTableList];
        //tblContentList.hidden=NO;
    }
    else {
        isSearching = NO;
       // tblContentList.hidden=YES;
    }
    [tblContentList reloadData];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    NSLog(@"Cancel clicked");
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    NSLog(@"Search Clicked");
    [self searchTableList];
}
searchBar=[[UISearchBar alloc]initWithFrame:CGRectMake(0.0,0.0320.0,41.0)];
[self.view addSubview:searchBar];
searchBar.delegate=self;
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
//返回节数。
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
//返回节中的行数。
如果(正在搜索){
返回[filteredContentList计数];
}
否则{
返回[titlearray计数];
}
}
-(布尔)应自动旋转指针面定向:(UIInterfaceOrientation)interfaceOrientation{
//对于支持的方向返回YES
返回(interfaceOrientation!=UIInterfaceOrientation肖像向上向下);
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*simpleTableIdentifier=@“SimpleTableCell”;
SimpleTableCell*单元格=(SimpleTableCell*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
如果(单元格==nil)
{
NSArray*nib=[[NSBundle mainBundle]loadNibNamed:@“SimpleTableCell”所有者:自选项:nil];
单元格=[nib objectAtIndex:0];
}
如果(正在搜索)
{
cell.namelab.text=[filteredContentList objectAtIndex:indexath.row];
cell.thumbnailImageView.image=[filteredImgArray对象索引:indexath.row];
}
其他的
{
cell.thumbnailImageView.image=[imagearray objectAtIndex:indexath.row];
cell.namelab.text=[titlearray objectAtIndex:indexPath.row];
}
返回单元;
}
-(作废)可搜索列表{
NSString*searchString=searchBar.text;

对于(inti=0;i您应该实现正确的数据源

为首次筛选的数据创建新的项目数组

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger count = ([filteredItems count] > 0) ? [filteredItems count] : [self.allItems count];

    return count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier: @"id"];

    MyCustomItem *item = ([filteredItems count] > 0) ? filteredItems[indexPath.row] : self.allItems[indexPath.row];

    [self configureCell:cell forItem:item];

    return cell;
}
配置搜索:

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{

    NSString *searchText = searchController.searchBar.text;

    if ([searchText length] == 0)
    {
        [filteredItems removeAllObjects];
        [self.tableView reloadData];
        return;
    }

    NSMutableArray *searchResults = [self.allItems mutableCopy];

    // SKIP ALL BODY OF SEARCHING

    filteredPeoples = searchResults;
    [self.tableView reloadData];
}

将非常有效。

IOS 8委托已被弃用,不确定这是否是问题所在。 方法

这里的[a link]8委托已被弃用,不确定这是否是问题所在。 方法

请尝试此属性

@property(nonatomic, assign) id< UISearchResultsUpdating > searchResultsUpdater
@property(非原子,赋值)id

另一个更好的链接[链接]

你是对的。进行搜索时,在
-tableView:cellForRowAtIndexPath:
中返回的
tableView
self.tableView
不同。这就是它的工作原理吗?如果是这样,使用
UISearchDisplayController
会使它更难使用。我需要清除
self.tableView
>并且只显示
UISearchDisplayController
tableView
。听起来使用
UISearchBar
委托方法并自己重新加载self.tableView会更容易。是的。
UISearchDisplayController
管理的表视图是
UITableView的子类。
我已经使用过
UIS直接使用earchBar
。委托方法足够清晰。好吧,那么解决方案是什么?我需要支持iOS 6+,所以
UISearchController
不可行。显然
UISearchDisplayController
应该在某种程度上正常工作。我做错了什么?直接使用
UISearchBar
。使视图控制器符合o
UISearchBarDelegate
并且您必须实现它的一些方法。我只需要使用
UISearchBar
就可以获得基本的操作行为,但是我喜欢
UISearchDisplayContoller
提供的一些功能,例如表视图变暗和使用范围的能力。类似的功能确实如此工作。我确实让搜索栏与
UISearchDisplayController
一起工作,但我希望能够将搜索控制器用于其作用域功能。您的代码对我来说运行正常。搜索结果和初始单元格集之间没有重叠。请注意,您可以为这些单元格指定不同的数据源和委托earchController。如果使用与原始版本相同的控件,则需要检查tableView。同一控制器对象用作两个不同表视图的数据源。我确实有一个不同的数据源用于过滤内容,
self.filteredBoundaries
,它是在
initWithStyle:
方法中创建的。当
UISearchDisplayController
delegate
shouldReloadTableForSearchString
被调用,我调用我的filter方法,删除
self.filteredBoundaries
中的所有对象,并使用
NSPredicate
获取新列表(有效),然后delegate返回true,然后重新加载数据。
@property(nonatomic, assign) id< UISearchResultsUpdating > searchResultsUpdater