【问题标题】:How to add a UISearchBar to two TableView in the same UIViewController如何将 UISearchBar 添加到同一 UIViewController 中的两个 TableView
【发布时间】:2013-04-10 22:52:30
【问题描述】:
  1. 我正在使用 JASidePanel - https://github.com/gotosleep/JASidePanels 来创建菜单效果。
  2. 我正在使用 xib。
  3. 我的类扩展了 UIViewController。
  4. searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText: 时不知道如何显示到另一个 tableView
  5. 您会看到第一个 tableView 是 Panel,过滤后的 tableView 是带有名称的 Thumb。
  6. 以下代码在需要添加第二个 tableview 时停止。

SidePanelViewController.h

@interface SidePanelViewController : UIViewController <UISearchBarDelegate, UISearchDisplayDelegate, UITableViewDataSource, UITableViewDelegate, MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate>
{
    IBOutlet UIImageView *_photoProfile;
    IBOutlet UILabel *_nameLabel;
    NSArray *_menuArray;
}

@property (nonatomic, readonly) IBOutlet UISearchBar *searchBar;
@property (nonatomic, strong) IBOutlet UITableView *tableView;
@property(nonatomic, strong) NSMutableArray *tableViewData;
@property(nonatomic, strong) NSMutableArray *originalTableViewData;
@property(nonatomic, strong) NSMutableArray *searchArray;
@property(nonatomic, strong) NSMutableDictionary *units;

SidePanelViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];


    // Do any additional setup after loading the view from its nib.
    _menuArray = [[NSArray alloc]initWithObjects:@"Home",@"Meu QR Sem Hora", @"Favoritas", @"Minha Pontuação", @"Minha Conta",@"Convidar Amigos", @"Ajuda", @"Fale Conosco",@"Privacidade e Termos", nil];

    _tableViewData = [[NSMutableArray alloc] initWithArray:_menuArray];
    // Don't show the scope bar or cancel button until editing begins
    [_searchBar setShowsScopeBar:NO];
    [_searchBar sizeToFit];

    //
    // Create a header view. Wrap it in a container to allow us to position
    // it better.
    //
    UIView *containerView =
    [[UIView alloc]
     initWithFrame:CGRectMake(0, 0, 320, 30)];
    UILabel *headerLabel =
    [[UILabel alloc]
     initWithFrame:CGRectMake(0, 0, 320, 30)];
    headerLabel.text = NSLocalizedString(@"Perfil", @"");
    headerLabel.textColor = [UIColor blackColor];
    headerLabel.shadowColor = [UIColor whiteColor];
    headerLabel.shadowOffset = CGSizeMake(0, 1);
    headerLabel.font = [UIFont boldSystemFontOfSize:22];
    headerLabel.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    [containerView addSubview:headerLabel];
    self.tableView.tableHeaderView = containerView;

    //SetName and Picture
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    _nameLabel.text = [defaults valueForKey:@"name"];
    _photoProfile.image = [UIImage imageWithData:[defaults objectForKey:@"Photo"]];

}

- (void)viewWillAppear:(BOOL)animated
{
    [_tableView reloadData];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    [self filterContentForSearchText:searchText];
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    [self.sidePanelController setCenterPanelHidden:YES animated:YES duration:0.2f];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
{
     [self.sidePanelController setCenterPanelHidden:NO animated:YES duration:0.2f];
        searchBar.text = @"";
    _tableViewData = [_originalTableViewData mutableCopy];
    [_tableView reloadData];
    [searchBar resignFirstResponder];

}

- (void) searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
    [self.sidePanelController setCenterPanelHidden:NO animated:YES duration:0.2f];
}

#pragma mark - ScrollView (UITableView) delegate methods
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [_searchBar resignFirstResponder];
    [self performSelector:@selector(enableCancelButton:) withObject:self.searchBar afterDelay:0.0];
}

// Used to re-enabled the cancel button when a user starts scrolling
- (void)enableCancelButton:(UISearchBar *)aSearchBar {
    for (id subview in [aSearchBar subviews]) {
        if ([subview isKindOfClass:[UIButton class]]) {
            [subview setEnabled:TRUE];
        }
    }
}

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

- (void)filterContentForSearchText:(NSString *)searchText {
    if (searchText && searchText.length) {
        [_tableViewData removeAllObjects];

        for (NSDictionary *dictionary in _originalTableViewData)
        {
            for (NSString *thisKey in [dictionary allKeys]) {
                if ([thisKey isEqualToString:@"SearchKey1"] ||
                    [thisKey isEqualToString:@"SearchKey2"] ) {

                    if ([[dictionary valueForKey:thisKey] rangeOfString:searchText
                                                                options:NSCaseInsensitiveSearch].location != NSNotFound) {
                        [_tableViewData addObject:dictionary];
                    } // for (NSString *thisKey in allKeys)

                } // if ([thisKey isEqualToString:@"SearchKey1"] || ...
            } // for (NSString *thisKey in [dictionary allKeys])
        } // for (NSDictionary *dictionary in originalTableViewData)

        [_tableView reloadData];

    } // if (query && query.length)
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

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

    return _menuArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndentifier = @"CustomMenu";

    CustomMenuCell *cell = (CustomMenuCell *) [_tableView dequeueReusableCellWithIdentifier:cellIndentifier];


    if(!cell){

        cell = (CustomMenuCell *) [[[NSBundle mainBundle] loadNibNamed:@"CustomMenuView" owner:nil options:nil] objectAtIndex:0];
    }

    NSString *tempString = [_menuArray objectAtIndex:indexPath.row];
    [cell configureMenu:tempString];


    cell.selectionStyle = UITableViewCellSelectionStyleGray;

    return cell;
}

【问题讨论】:

    标签: iphone ios uitableview uiviewcontroller uisearchbar


    【解决方案1】:

    不知道你是否解决了这个问题,但你是什么意思显示到另一个表视图?如果您的意思是它显示的与您发布的第二张图片相似,那么我会继续说,如果您使用“搜索栏和搜索显示”,搜索栏应该已经有一个?

    self.searchDisplayController.searchResultsTableView

    如果您确实使用了该搜索栏和显示,您可以使用 NSPredicate 并创建一个过滤器可变数组,该数组复制并根据您的 originalTableViewData 进行谓词。 希望这对您或其他仍然感兴趣的人有所帮助。

    【讨论】:

    • 我已经完成了这项工作,需要做很多工作,问题是您需要更改表格视图,检查新的行数,填充新内容,创建过滤器,检查如果搜索是否可聚焦,请使“X”按钮清除视图。
    猜你喜欢
    • 2017-01-04
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 2014-05-16
    • 1970-01-01
    • 1970-01-01
    • 2016-05-15
    • 1970-01-01
    相关资源
    最近更新 更多