0

この問題で私を助けてください。過去 4 日間と夜に問題は見つかりませんでした。これは、別のプロジェクトで既に行った簡単な作業です。しかし、異なるのは、UISearchBar を Display Controller で使用するのはこれが初めてだということです。とにかくここに私のコードがあります。

mnuSearch.h

#import <UIKit/UIKit.h>
#import "constants.h"
#import "JSON.h"
#import "Initial.h"

@interface mnuSearch : UIViewController<UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate> {

}

@property (retain) NSMutableArray *_arrSearchNews;

@property (nonatomic, retain) IBOutlet UISearchBar *txtSearchBox;
@property (nonatomic, retain) IBOutlet UITableView *tblNewsSearch;

@end

mnuSearch.m

#import "mnuSearch.h"

@interface mnuSearch ()

@end

@implementation mnuSearch

@synthesize _arrSearchNews;
@synthesize txtSearchBox, tblNewsSearch;

#pragma mark - When "search button" in keyboard is clicked
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [searchBar setShowsCancelButton:NO animated:YES];
    [searchBar resignFirstResponder];
    [self searchNews:searchBar.text];
    [self.tblNewsSearch reloadData];
}

#pragma mark - Search news
-(void)searchNews:(NSString*)query {
    // Retrieve searched news list
    NSString *url = [NSString stringWithFormat:@"http://mydomain.com/NewsSearch.ashx?a=%@&b=%@&c=%@", currentLocale, uniqueID, [Initial getUrlEncode:query]];
    NSURLRequest *_rqJsonDataList = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    NSData *_rpJsonDataList = [NSURLConnection sendSynchronousRequest:_rqJsonDataList returningResponse:nil error:nil];
    NSString *_strJsonDataList = [[NSString alloc] initWithData:_rpJsonDataList encoding:NSUTF8StringEncoding];
    NSDictionary *_nsDicJsonDataList = [_strJsonDataList JSONValue];
    NSString *_strStatusCode = [_nsDicJsonDataList objectForKey:@"StatusCode"];
    NSString *_strStatusMessage = [_nsDicJsonDataList objectForKey:@"StatusMessage"];

    // OK
    if ([_strStatusCode isEqualToString:@"OK"]) {
        NSArray *_arrList = [_nsDicJsonDataList objectForKey:@"Table1"];
        [self._arrSearchNews removeAllObjects];
        [self._arrSearchNews addObjectsFromArray:_arrList];
    }
    // Error
    else {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil
                                                   message:_strStatusMessage
                                                  delegate:nil
                                         cancelButtonTitle:nil
                                         otherButtonTitles:@"OK",nil];


        [alert show];
    }
}

#pragma mark - [UITableView] Number of Sections
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

#pragma mark - [UITableView] Number of Rows In Section
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    int _retValue = 0;

    _retValue = [_arrSearchNews count];

    return _retValue;
}

#pragma mark - [UITableView] Return table view cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"mnuSearchCell";

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

    // Set font
    UIFont *textLabelFont = [UIFont fontWithName:@"Helvetica" size:14];
    cell.textLabel.font = textLabelFont;
    UIFont *detailTextLabelFont = [UIFont fontWithName:@"Helvetica" size:12];
    cell.detailTextLabel.font = detailTextLabelFont;

    cell.imageView.image = nil;

    NSDictionary *_nsDic = [self._arrSearchNews objectAtIndex:indexPath.row];
    NSString *abc = [NSString stringWithFormat:@"[%@] %@", [_nsDic objectForKey:@"GUBUN_NM"], [_nsDic objectForKey:@"TSUBJECT"]];
    NSLog(@"%@", abc);    **//--> This returns text I wanted.**
    cell.textLabel.text = [NSString stringWithFormat:@"[%@] %@", [_nsDic objectForKey:@"GUBUN_NM"], [_nsDic objectForKey:@"TSUBJECT"]];
    cell.detailTextLabel.text = nil;

    return cell;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self._arrSearchNews = [[NSMutableArray alloc] init];
}

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

- (void)viewDidUnload {
    [self setTblNewsSearch:nil];
    [self setTxtSearchBox:nil];
    [super viewDidUnload];
}
@end

UITableView がデータを返さない理由が本当にわかりません。検索ボックスにいくつかのキーワードを入力すると、「結果がありません」と表示されます。キーボードで [検索] をクリックすると、アプリは json からデータを取得します。「_arrSearchNews」には 340 個のオブジェクトがあります。ただし、UITableView は「結果なし」のままです。このエラーについて教えてください。前もって感謝します。

4

1 に答える 1

0

UISearchDisplayController は、検索結果が通常の tableView の上にオーバーレイされた tableView を表示するため、searchDispllayController の searchResultsDelegate と searchResultsDataSource を適切に設定して、tableView に入力する方法を認識できるようにする必要があります。

于 2013-01-06T13:22:07.010 に答える