Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c UITableView不显示NSMutableArray中的数据_Objective C_Uitableview_Uisearchbar - Fatal编程技术网

Objective c UITableView不显示NSMutableArray中的数据

Objective c UITableView不显示NSMutableArray中的数据,objective-c,uitableview,uisearchbar,Objective C,Uitableview,Uisearchbar,请帮我解决这个问题。 在过去的4天4夜里,我没有发现任何问题。 这是我在另一个项目中已经做过的简单工作。 但不同的是,这是我第一次将UISearchBar与显示控制器一起使用。 不管怎样,这是我的密码 mnuSearch.h #import <UIKit/UIKit.h> #import "constants.h" #import "JSON.h" #import "Initial.h" @interface mnuSearch : UIViewController<UITa

请帮我解决这个问题。 在过去的4天4夜里,我没有发现任何问题。 这是我在另一个项目中已经做过的简单工作。 但不同的是,这是我第一次将UISearchBar与显示控制器一起使用。 不管怎样,这是我的密码

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
我真的不明白为什么我的UITableView不返回任何数据。 当我在搜索框中键入一些关键字时,它会显示“无结果”。 当我在键盘上点击“搜索”时,我的应用程序从json获取数据。 “搜索新闻”有340个对象。 但UITableView仍然“没有结果”。 请帮我纠正这个错误。
提前感谢。

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

您添加了UISearchBarDelegate和UISearchDisplayDelegate,但您没有实现委派方法…我实现了。-(无效)searchBarSearchButtonClicked:(UISearchBar*)搜索栏。这并不意味着我的UITableView不应该返回任何数据,是吗?
#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