UITableViewController with UISearchDisplayController not reloading data
I have a ViewController class with an appropriate .XIB file. Here is the
ViewController code:
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;
}
My connections are all working, and the code successfully puts the JSON
data into the UITableView.
My problem is, the table view isn't reloading!
I have tried to just load it without a UISearchDisplayController, and it
works fine. I'm thinking it's some sort of override. Where my TableView
reloads data, that just doesn't work. What's also weird is that if you
type in something to the search display, the table view is displayed. What
am I doing wrong that the Search Bar doesn't reload the data?
No comments:
Post a Comment