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 回答