搜索栏未在iPhone上修复

时间:2012-12-17 07:30:08

标签: iphone ios ios5 uisearchbar

我有两个问题:

  1. 我的iPhone屏幕上有一个搜索栏。当我滚动屏幕时,我的搜索栏也会滚动。我想解决这个问题或者让它变得绝对。
  2. 搜索不起作用如果我输入A然后我应该得到所有以字母A开头的单词。
  3. 我正在尝试的代码:

    - (void)viewDidLoad {
        [super viewDidLoad];
        const NSInteger searchBarHeight = 45;
        UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0,
        self.tableView.frame.size.width, searchBarHeight)];
        searchBar.delegate = self;
        self.tableView.tableHeaderView = searchBar;
        [searchBar release];
        UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"Refresh" 
        style:UIBarButtonItemStyleBordered target:self action:@selector(onAddContact:)];
        self.navigationItem.rightBarButtonItem = addButton;
        UILabel *label = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
        label.backgroundColor = [UIColor clearColor];
        label.font = [UIFont boldSystemFontOfSize:20.0];
        label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
        label.textAlignment = UITextAlignmentCenter;
        label.textColor = [UIColor whiteColor]; // change this color
        self.navigationItem.titleView = label;
        label.text = NSLocalizedString(@"All Contacts", @"");
    
        [label sizeToFit];
        content = [DataGenerator wordsFromLetters];
        indices = [[content valueForKey:@"headerTitle"] retain];
    }
    

2 个答案:

答案 0 :(得分:1)

您需要将搜索栏与表格视图分开。只需将searchBar添加到视图中,而不是将其添加到表格中。代码如下:

[self.view addSubview:searchBar];

然后添加表格视图。

对于UISearchDisplayController示例,请遵循以下教程: http://cocoabob.net/?p=67 要么 http://blog.mugunthkumar.com/coding/iphone-tutorial-uisearchdisplaycontroller-with-nspredicate/

也可以使用源代码。如果您不明白,请告诉我。我有一个项目,其中所有这些已经实施。

答案 1 :(得分:0)

此时当您滚动UITableView时,UISearchBar也会滚动{1}},因为您将UISearchBar添加为UITableView的标题视图,请参阅此行..

self.tableView.tableHeaderView = searchBar;

所以使用UITableView进行滚动,这里用于修复UISearchBar只是添加self.view的子视图,但是在UITableView以外的地方...

[yourSearchBar setFrame:CGRectMake(0, 0, 320, 44)];
[self.view addSubview:yourSearchBar];

有关详细信息,请参阅下面的UISearchBarUITableView

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText教程和演示的链接
  1. filtering-a-uitableview-with-a-uisearchbar-using-core-data
  2. connect-uitableview-with-uisearchbar //了解如何使用{{1}}方法
  3. 从本教程中使用搜索栏文本字段更改事件过滤数组
相关问题