UITableView不会在NSMutableArray中显示数据

时间:2013-01-06 12:50:36

标签: objective-c uitableview uisearchbar

请帮我解决这个问题。 在过去的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仍然保留“无结果”。 请帮我解决这个错误。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

UISearchDisplayController显示一个tableView,其搜索结果覆盖在普通tableView之上,因此您需要确保正确设置searchDispllayController的searchResultsDelegate和searchResultsDataSource,以便它知道如何填充tableView。