UITableViewController与UISearchDisplayController不重新加载数据

时间:2013-08-20 03:06:34

标签: objective-c xcode uitableview uisearchdisplaycontroller

我有一个带有相应.XIB文件的ViewController类。这是ViewController代码:

ViewController.h:

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@interface ViewController : UIViewController
{    
    NSArray *news;
    NSMutableData *data;
}

@property (strong, nonatomic) IBOutlet UITableView *mainTableView;
@property (strong, nonatomic) IBOutlet UISearchBar *searchBar;

@property (strong, nonatomic) Results *result;

@end

ViewController.m:

#import "ViewController.h"
#import "Results.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize result, mainTableView, searchBar;

-(id) init
{
    self = [super initWithNibName:@"ViewController_iPhone" bundle:nil];

    if (self)
    {
        result = [[Results alloc] init];
        mainTableView = [[UITableView alloc] init];
        [self.mainTableView setDelegate:self];
    }

    return self;
}

- (void)viewDidLoad
{
    self.title = @"Search";
    [[UINavigationBar appearance] setTintColor:[UIColor blackColor]];

    [super viewDidLoad];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    NSString *query = searchBar.text;
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://samplesite.com/external/metasearchjson.php?query=%@", query]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection connectionWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    data = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
{
    [data appendData:theData];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    NSArray *responseDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:NULL];

    if ([responseDict isKindOfClass:[NSArray class]]) {
        news = responseDict;
        //[mainTableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
        [[self mainTableView] reloadData];
        NSLog(@"%@", mainTableView);
    } else {
        NSLog(@"JSON Error.");
    }
}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:[indexPath row] inSection:0]];
    NSString *url = _getString([[news objectAtIndex:[indexPath row]] objectForKey:@"link"]);
    [result getURL:url];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    [self.navigationController pushViewController:result animated:YES];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"The download could not complete - please make sure you're connected to either 3G or Wi-Fi." delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
    [errorView show];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

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

- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [news count];
}

NSString *_getString(id obj)
{
    return [obj isKindOfClass:[NSString class]] ? obj : nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];

    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"];
    }

    cell.detailTextLabel.text = _getString([[news objectAtIndex:indexPath.row] objectForKey:@"metaScore"]);
    cell.textLabel.text = _getString([[news objectAtIndex:indexPath.row] objectForKey:@"title"]);

    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

    return cell;
}

我的连接全部正常,代码成功将JSON数据放入UITableView。

我的问题是,表格视图没有重新加载!

我试图在没有UISearchDisplayController的情况下加载它,它工作正常。我在想它是某种覆盖。我的TableView重新加载数据的地方,这是行不通的。同样奇怪的是,如果您在搜索显示中输入内容,则会显示表格视图。我的错误是搜索栏没有重新加载数据?

2 个答案:

答案 0 :(得分:0)

在搜索显示委托中,您需要实现

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString

并返回YES。

答案 1 :(得分:0)

尝试制作后续步骤:

在ViewController.h中:

添加协议:

@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

添加属性:

@property (strong, nonatomic) NSArray* news;

然后在connectionDidFinishLoading中替换:method news = responseDict;如

self.news = responseDict;

然后在主线程中执行reloadData:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
...
    dispatch_async(dispatch_get_main_queue(), ^{
        [[self mainTableView] reloadData];
    });

在ViewController.m中插入行[self.mainTableView setDataSource:self]; :

-(id) init
{
    self = [super initWithNibName:@"ViewController_iPhone" bundle:nil];

    if (self)
    {
...
        [self.mainTableView setDelegate:self];
        [self.mainTableView setDataSource: self];
    }

    return self;
}
希望它可以帮到你。