0

我有一个带有适当 .XIB 文件的 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

视图控制器.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 在哪里重新加载数据,那是行不通的。同样奇怪的是,如果您在搜索显示中输入内容,则会显示表格视图。搜索栏不重新加载数据我做错了什么?

4

2 回答 2

0

尝试进行下一步:

在 ViewController.h 中:

添加协议:

@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

添加属性:

@property (strong, nonatomic) NSArray* news;

然后在connectionDidFinishLoading中替换:方法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;
}

希望它可以帮助你。

于 2013-08-20T03:26:24.263 回答
0

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

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

并返回是。

于 2013-08-20T03:12:12.950 回答