UISearch Controller显示数据不是已过滤数组的一部分

时间:2015-10-30 10:34:02

标签: ios objective-c uisearchbar uisearchcontroller

我的问题是这个。在我将过滤后的数组打印到控制台时,在我开始在搜索栏中键入时,为什么过滤后的表视图显示了房间。我认为可能是表格视图单元格被重用,因此将单元格标签文本设置为nil以确保文本在tableviewcellforindex:方法中重置,一切都无济于事。有人可以帮忙吗?

这是我的表视图控制器,它充当我的标准tableview和过滤的tableview的数据源和委托

#import <UIKit/UIKit.h>
#import "Rooms.h"

@interface RoomsTableViewController : UITableViewController
<UISearchControllerDelegate,UISearchResultsUpdating,UISearchBarDelegate,UITableViewDelegate>

@property (strong,nonatomic) NSMutableArray *roomList;
@property (strong,nonatomic) NSMutableArray *orderedRoomList;
@property (strong,nonatomic) NSMutableArray *filteredRooms;
@property (strong,nonatomic) UITableViewController *searchResultsController;
@property (strong,nonatomic) UISearchController *searchController;

@end


#import "RoomsTableViewController.h"

@interface RoomsTableViewController ()
@property BOOL searchControllerWasActive;
@property BOOL searchControllerSearchFieldWasFirstResponder;

@end

实施档案

@implementation RoomsTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

//sort the room list into filetred by alpha numeric A10 before C1 example this will eventually be done on the server

    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES comparator:^NSComparisonResult(id obj1, id obj2) {
        return [(NSString *)obj1 compare:(NSString *)obj2 options:NSNumericSearch];
    }];
    self.orderedRoomList = (NSMutableArray*)[self.roomList sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];


    self.searchResultsController= [[UITableViewController alloc]initWithStyle:UITableViewStylePlain];
    self.searchController = [[UISearchController alloc]initWithSearchResultsController:self.searchResultsController];
    self.searchController.searchResultsUpdater=self;
    [self.searchController.searchBar sizeToFit];
    self.tableView.tableHeaderView=self.searchController.searchBar;

    //set up the data source and delegate of this new table view to be this (roomsTableviewcontroller) view controller
    self.searchResultsController.tableView.delegate=self;
    self.searchResultsController.tableView.dataSource=self;
    self.searchController.delegate=self;
    self.searchController.dimsBackgroundDuringPresentation=NO;
    self.searchController.searchBar.delegate=self;

    self.definesPresentationContext=YES;


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    //restore the search controllers active state
    if (self.searchControllerWasActive) {
        self.searchController.active = self.searchControllerWasActive;
        _searchControllerWasActive=NO;
        if (self.searchControllerSearchFieldWasFirstResponder) {
            [self.searchController.searchBar becomeFirstResponder];
            _searchControllerSearchFieldWasFirstResponder=NO;

        }

    }
}

-(void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
}


#pragma mark - UISearchControllerDelegate

- (void)willPresentSearchController:(UISearchController *)searchController {
    // do something before the search controller is presented
    self.navigationController.navigationBar.translucent = YES;
}

-(void)willDismissSearchController:(UISearchController *)searchController
{
    self.navigationController.navigationBar.translucent = NO;
}




#pragma mark - Table view data source

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

    return 1;
}

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

    if (tableView==self.tableView) {
        return [self.orderedRoomList count];
    }else{
        return [self.filteredRooms count];
    }
}

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


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

    static NSString *cellReuseIdentifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier];

    if (cell==nil) {
        cell= [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier];
    }

Rooms *room = (tableView==self.tableView)? [self.orderedRoomList objectAtIndex:indexPath.row]: [self.filteredRooms objectAtIndex:indexPath.row];

    cell.textLabel.text=nil;

    NSString *labelString = [NSString stringWithFormat:@"%@ %@",room.name,room.roomDescription];
    cell.textLabel.text=labelString;

    return cell;

}

下面是模拟器中表格视图的照片和检查的屏幕打印,以查看过滤后的数组是否确实正常工作。您可以看到C3未列为已过滤数组的一部分,但仍显示在屏幕上。

enter image description here

0 个答案:

没有答案