简体   繁体   English

如何在同一UIViewController中将UISearchBar添加到两个TableView

[英]How to add a UISearchBar to two TableView in the same UIViewController

  1. I am using JASidePanel - https://github.com/gotosleep/JASidePanels , to create the Menu effect. 我正在使用JASidePanel- https://github.com/gotosleep/JASidePanels来创建菜单效果。
  2. I am working with xib. 我正在使用xib。
  3. My class extends an UIViewController. 我的课程扩展了UIViewController。
  4. I dont know how to show to another tableView when searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText: 我不知道当searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText:时如何显示到另一个tableView searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText:
  5. You see that the first tableView is a Panel and the filtered one is a Thumb with a name. 您会看到第一个tableView是一个Panel,而过滤后的是一个带名称的Thumb。
  6. The following code stops when it needs to add the second tableview. 当需要添加第二个表视图时,以下代码停止。

SidePanelViewController.h 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 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;
}

在此处输入图片说明在此处输入图片说明

Don't know if you solved this or not, but what do you mean show to another table view? 不知道您是否解决了这个问题,但是显示到另一个表视图意味着什么? If you mean that it shows similar to the second image you posted, then I'm gonna go ahead and say that the search bar should already have one if youre using "Search Bar and Search Display"? 如果您的意思是显示的图片与您发布的第二张图片相似,那么我要说,如果您使用“搜索栏和搜索显示”,那么搜索栏应该已经有一个?

self.searchDisplayController.searchResultsTableView

If you did use that Search Bar and Display, you can have NSPredicate and make a filter mutable array that copies and is predicated from your originalTableViewData. 如果您确实使用过该搜索栏和显示,则可以使用NSPredicate并创建一个过滤器可变数组,该数组从原始TableViewData复制并基于谓词。 Hopefully this helps your or anyone else that is still interested. 希望这对您或其他仍然有兴趣的人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM