0

適切な .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がデータをリロードするところ、それはうまくいきません。また、奇妙なことに、検索表示に何かを入力すると、テーブル ビューが表示されます。検索バーがデータをリロードしないのは何が悪いのですか?

4

2 に答える 2

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;
}

それがあなたを助けることを願っています。

于 2013-08-20T03:26:24.263 に答える