1

iPhone用のシンプルなアプリを作ってみました。

その中に、テーブルビューとテーブルビューの詳細ビューがあり、最後に検索バーを使用してテーブルビューをフィルタリングします。

テーブルビューとディテールビューは正常に機能しますが、サーチバーは正常に機能しません。

検索ワードを入力すると、テーブルビュー全体が表示され、詳細ビューが検索モードで機能しません。

検索モードをキャンセルすると、詳細ビ​​ューが再び機能します。

明らかに私のフィルターコードは間違っていて、フィルターコード内に詳細ビューがありませんが、それを正しく行う方法がわかりません。

誰かが私を助けてくれることを願っています、私のコードは次のようになります:

私の*.hファイル:

#import <UIKit/UIKit.h>

@class DetailViewController;

@interface MasterViewController : UITableViewController <UISearchBarDelegate, UISearchDisplayDelegate>

{
    NSArray *originalData;
    NSMutableArray *searchData;

    UISearchBar *searchBar;
    UISearchDisplayController *searchDisplayController;

}

@property (strong, nonatomic) DetailViewController *detailViewController;

@end

私の*.mファイル:

#import "MasterViewController.h"

#import "DetailViewController.h"

@interface MasterViewController () {
    NSMutableArray *_objects;
}
@end

@implementation MasterViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Opslag", @"Opslag");
        _objects = [[NSMutableArray alloc] init];

        //title
        //detail

        NSDictionary * obj1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"this_title", @"title", @"this_detail", @"detail", nil];
        NSDictionary * obj2 = [[NSDictionary alloc] initWithObjectsAndKeys:@"some_title2", @"title", @"some_detail2", @"detail", nil];
        NSDictionary * obj3 = [[NSDictionary alloc] initWithObjectsAndKeys:@"some_title3", @"title", @"some_detail3", @"detail", nil];
        NSDictionary * obj4 = [[NSDictionary alloc] initWithObjectsAndKeys:@"some_title4", @"title", @"some_detail4", @"detail", nil];

        [_objects addObject:obj1];
        [_objects addObject:obj2];
        [_objects addObject:obj3];
        [_objects addObject:obj4];

        originalData = [[NSArray alloc] initWithObjects:obj1, obj2, obj3, nil];
        searchData = [[NSMutableArray alloc] init];


    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 160, 44)];
    searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];

    searchDisplayController.delegate = self;
    searchDisplayController.searchResultsDataSource = self;

    self.tableView.tableHeaderView = searchBar;

}

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



#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _objects.count;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    cell.textLabel.text = [[_objects objectAtIndex:indexPath.row] objectForKey:@"title"];

    cell.detailTextLabel.text = [[_objects objectAtIndex:indexPath.row] objectForKey:@"detail"];




    return cell;
}

#pragma mark - searchDisplayControllerDelegate

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [searchData removeAllObjects];

    NSArray *group;

    for(group in originalData)
    {
        NSMutableArray *newGroup = [[NSMutableArray alloc] init];
        NSString *element;

        for(element in group)
        {
            NSRange range = [element rangeOfString:searchString options:NSCaseInsensitiveSearch];

            if (range.length > 0) {
                [newGroup addObject:element];
            }
        }

        if ([newGroup count] > 0) {
            [searchData addObject:newGroup];
        }


    }

    return YES;
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!self.detailViewController) {
        self.detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
    }
    NSDictionary *object = _objects[indexPath.row];
    self.detailViewController.detailItem = [object objectForKey:@"detail"];
    self.detailViewController.titleItem = [object objectForKey:@"title"];
    [self.navigationController pushViewController:self.detailViewController animated:YES];
}

@end
4

1 に答える 1

0

cellForRowAtIndexPathメソッドは常に配列から選択します。_objectsこれは、検索の実行時に変更されることはありません。shouldReloadTableForSearchStringが呼び出されたときは、データを入力するだけです。これは、フィルタリングされた結果にsearchDataフィードバックされて表示されることはありませんcellForRowAtIndexPathnumberOfRowsInSection

これを処理する1つの方法は、次のように(テストされていない)次のことを実行することですcellForRowAtIndexPath

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    NSArray* sourceArray = tableView == self.searchDisplayController.searchResultsTableView ? searchData : _objects;

    cell.textLabel.text = [[sourceArray objectAtIndex:indexPath.row] objectForKey:@"title"];

    cell.detailTextLabel.text = [[sourceArray objectAtIndex:indexPath.row] objectForKey:@"detail"];

    return cell;
}

そしてまたあなたのnumberOfRowsInSection

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.searchDisplayController.searchResultsTableView){
        return searchData.count;
    }else
    {
       return _objects.count;
    }

}
于 2012-11-13T23:53:42.523 に答える