2013-05-26 1 views
-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

가 UIsearchBar 자동으로 tabelview의 콘텐츠에 대한 검색을하지 않습니다. 입력 된 텍스트를 검색하기 위해 UISearchbarDelegate 메서드를 구현 한 다음 테이블 뷰를 다시로드하고 검색 조건으로 필터링 된 새 배열에 따라 테이블 뷰 대리자 메서드의 값을 반환해야합니다.

+0

코드는 어떻게됩니까? 괜찮아 보이 나 뭔가를 추가해야합니까? –

+0

NSArray의 propery NSArray * keysAndValues'에이 배열의 각 요소는 nameKey (NSString)와 nameArray (NSArray)라는 두 개의 키가있는 사전을 포함해야합니다. 섹션의 행 수와 인덱스 행의 행 수를 반환하는 위임 메서드에서 검색 조건에 따라 nameArray를 필터링하여 만든 하위 배열 – Kedar

+0

이 코드는 어떻게 표시되어야합니까? –