-1

我无法使用搜索栏找到结果。请参阅附上我的代码:

// ViewController.m

#import "ViewController.h"

@implementation ViewController
@synthesize names;
@synthesize keys;


#pragma mark - View lifecycle

- (void)viewDidLoad
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"sortednames"ofType:@"plist"];
    NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfFile:path];
    self.names = dict;

    NSArray *array = [[names allKeys] sortedArrayUsingSelector:@selector(compare:)];
    self.keys = array;

     [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [keys count];
}
- (NSInteger)tableView:(UITableView *)tableView 
 numberOfRowsInSection:(NSInteger)section
{
    NSString *key = [keys objectAtIndex:section];
    NSArray *nameSection = [names objectForKey:key];
    return [nameSection count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger section = [indexPath section];
    NSUInteger row = [indexPath row];

    NSString *key = [keys objectAtIndex:section];
    NSArray *nameSection = [names objectForKey:key];

    static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                             SectionsTableIdentifier ];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                       reuseIdentifier: SectionsTableIdentifier ];
    }

    cell.textLabel.text = [nameSection objectAtIndex:row];
    return cell;
}
- (NSString *)tableView:(UITableView *)tableView 
titleForHeaderInSection:(NSInteger)section
{
    NSString *key = [keys objectAtIndex:section];
    return key;
}

http://i.stack.imgur.com/ZBzJZ.png http://i.stack.imgur.com/ktevQ.png

here is my "h" file:



//
    //  ViewController.h
    //  Sections
    //
    //  Created by t r on 3/17/12.
    //  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
    //

    #import <UIKit/UIKit.h>

    @interface ViewController : UIViewController 
        <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate>
    {
        NSDictionary *names;
        NSArray *keys;
    }

    @property (nonatomic, retain) NSDictionary *names;
    @property (nonatomic, retain) NSArray *keys;
    @end
4

1 回答 1

4

UIsearchBar 不会自动对 tabelview 内容进行搜索。您必须实现 UISearchbarDelegate 方法来检测输入的文本,然后根据使用搜索条件过滤的新数组重新加载表视图并在表视图委托方法中返回值。

于 2013-05-26T20:55:17.700 回答